Visualizing Colors in Images Using Histogram in Python
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-
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.
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.
cv2.calchist([imageObject], [channelValue], mask, [histSize], [low,high])
- 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.
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.