Visualizing Colors in Images Using Histogram in Python
Last Updated :
03 Jan, 2023
In this article, we will discuss how to visualize colors in an image using histogram in Python.
An image consists of various colors and we know that any color is a combination of Red, Green, Blue. So Image consists of Red, Green, Blue colors. So using Histogram we can visualize how much proportion we are having RGB colors in a picture.
Histogram actually provides how frequently various colors occur in an image but not the location of color in an image. To visualize colors in the image we need to follow the below steps-

Stepwise Implementation
Step 1: Import Necessary Modules
To this Concept mainly we need 2 modules.
- cv2- It is used to load the image and get the RGB data from the image.
- matplotlib- Used to plot the histograms.
Step 2: Load Image
To load an image we need to use imread() method which is in the cv2 module. It accepts the image name as a parameter.
Syntax :
cv2.imread('imageName')
Step 3: Get RGB Data from Image
To get the RGB colors from the image, the cv2 module provides calchist method that accepts the image object, channel to get a specific color, mask, histogram size, and range.
Syntax:
cv2.calchist([imageObject], [channelValue], mask, [histSize], [low,high])
Parameters:
- imageObject- Variable name in which image is loaded.
- channelValue- It accepts 0,1,2 values. It helps us to get the required color.
- 0 indicates blue, 1 indicates red, 2 indicates green.
Step 4: Plot histograms
matplotlib provides the hist method which is used to draw the histogram on specified data.
Syntax:
matplotlib.hist(data,color="value")
Example:
As per the above steps, First imported the required modules, and next we loaded an image using imread() method and using calcHist() method to get the RGB colors from Image and then plot the Histograms using the RGB data.
Python3
# import necessary packages
import cv2
import matplotlib.pyplot as plt
# load image
imageObj = cv2.imread('SampleGFG.jpg')
# to avoid grid lines
plt.axis("off")
plt.title("Original Image")
plt.imshow(cv2.cvtColor(imageObj, cv2.COLOR_BGR2RGB))
plt.show()
# Get RGB data from image
blue_color = cv2.calcHist([imageObj], [0], None, [256], [0, 256])
red_color = cv2.calcHist([imageObj], [1], None, [256], [0, 256])
green_color = cv2.calcHist([imageObj], [2], None, [256], [0, 256])
# Separate Histograms for each color
plt.subplot(3, 1, 1)
plt.title("histogram of Blue")
plt.hist(blue_color, color="blue")
plt.subplot(3, 1, 2)
plt.title("histogram of Green")
plt.hist(green_color, color="green")
plt.subplot(3, 1, 3)
plt.title("histogram of Red")
plt.hist(red_color, color="red")
# for clear view
plt.tight_layout()
plt.show()
# combined histogram
plt.title("Histogram of all RGB Colors")
plt.hist(blue_color, color="blue")
plt.hist(green_color, color="green")
plt.hist(red_color, color="red")
plt.show()
Output


Similar Reads
Python | Visualizing image in different color spaces OpenCV (Open Source Computer Vision) is a computer vision library that contains various functions to perform operations on pictures or videos. It was originally developed by Intel but was later maintained by Willow Garage and is now maintained by Itseez. This library is cross-platform that is it is
4 min read
Find most used colors in image using Python Prerequisite: PIL PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. It was developed by Fredrik Lundh and several other contributors. Pillow is the friendly PIL fork and an easy-to-use library developed by Alex Clark and other contributors. Weâl
2 min read
Color Identification in Images using Python - OpenCV An open-source library in Python, OpenCV is basically used for image and video processing. Not only supported by any system, such as Windows, Linux, Mac, etc. but also it can be run in any programming language like Python, C++, Java, etc. OpenCV also allows you to identify color in images. Donât you
3 min read
Circular Visualization of Dataset using hishiryo Python Among various ways to represent data, circular representation of data is one of the ways to render data points and do a corresponding analysis of the same. This article talks about ways to achieve the said visualization of data for further analytical purposes. hishiryo: This tool helps in the genera
2 min read
Histogram matching with OpenCV, scikit-image, and Python Histogram matching is used for normalizing the representation of images, it can be used for feature matching, especially when the pictures are from diverse sources or under varied conditions (depending on the light, etc). each image has a number of channels, each channel is matched individually. His
3 min read