Updated April 19, 2023
Introduction to OpenCV Histogram
A histogram of an image can be considered as the graph or plot which gives us an understanding of the distribution of intensity in an image whose x-axis is pixel values and a y-axis is a corresponding number of pixels in the image and by plotting the histogram of an image, we can understand the brightness, contrast, intensity distribution in the image, etc. and the feature of a histogram is available in all the tools used for image processing and we have built-in functions called calcHist() function in OpenCV to plot the histograms of the image which returns a 256*1 array as the output.
The syntax to define calcHist() function in OpenCV is as follows:
calcHist(sourceimage, channels, mask, histSize, ranges )
where the source image is the image whose histogram is to be calculated whose value is specified in square brackets,
a channel is an index for which the histogram is calculated. The value of the channel is [0] for the grayscale image. The value of the channel is [0], [1], [2] for blue, green, red color respectively and the value is specified in square brackets,
mask specifies the region of the image whose histogram need not have to found. The value of mask id None if we wish to not mask any region of the image,
histSize represents the BIN count specified in square brackets. The histogram of a given image can be divided into 16 subparts and the sum of all the pixels in the subpart is called BIN represented as histSize and
range specify the intensity values range.
Working of calcHist() function in OpenCV
- A histogram of an image can be considered as the graph or plot which gives us an understanding of the distribution of intensity in an image whose x-axis is pixel values and the y-axis is a corresponding number of pixels in the image.
- The histogram of an image can be calculated using calcHist() function in OpenCV.
- The calcHist() function takes five parameters namely source image. Channel, mask, histSize, and range.
- The parameter source image is the image whose histogram is to be calculated whose value is specified in square brackets.
- The parameter channel is the index for which the histogram is calculated. The value of the channel is [0] for the grayscale image. The value of the channel is [0], [1], [2] for blue, green, red colors respectively and the value is specified in square brackets.
- The parameter mask specifies the region of the image whose histogram need not have to found. The value of mask id None if we wish to not mask any region of the image.
- The parameter histSize represents the BIN count specified in square brackets. The histogram of a given image can be divided into 16 subparts and the sum of all the pixels in subpart is called BIN represented as histSize.
- The parameter range specifies the intensity values range whose value is [0,256] normally.
- The calcHist() function returns a 256*1 array in which each value is a pixel value and corresponds to the pixel values in the given image.
Example #1
OpenCV program in python to demonstrate calcHist() function using which we calculate the histogram of a given image and plot the histogram of the given image to display as the output on the screen:
Code:
#importing the modules numpy, cv2 and matplotlib
import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt
#reading the image whose histogram is to be calculated
imgread = cv.imread('C:/Users/admin/Desktop/car.jpg')
#specifying the colors which are iterated to be passed as values of colors to the calcHist() function to calculate the histogram of an image and plot the histogram of an image
color = ('b','g','r')
for k,color in enumerate(color):
histogram = cv.calcHist([imgread],[k],None,[256],[0,256])
plt.plot(histogram,color = color)
plt.xlim([0,256])
plt.show()
The output of the given program is shown in the snapshot below:
In the above program, we are importing the modules cv2, numpy, and matplotlib. Then we are reading the image whose histogram is to be calculated using the imread() function. Then we specifying the colors which are iterated to be passed as values of colors to the calcHist() function to calculate the histogram of an image and plot the histogram of an image to be displayed as the output on the screen. The output is shown in the snapshot above.
Example #2
OpenCV program in python to demonstrate calcHist() function using which we calculate the histogram of a given image and plot the histogram of the given image to display as the output on the screen:
Code:
#importing the modules numpy, cv2 and matplotlib
import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt
#reading the image whose histogram is to be calculated
imgread = cv.imread('C:/Users/admin/Desktop/plane.jpg')
#specifying the colors which are iterated to be passed as values of colors to the calcHist() function to calculate the histogram of an image and plot the histogram of an image
color = ('b','g','r')
for k,color in enumerate(color):
histogram = cv.calcHist([imgread],[k],None,[256],[0,256])
plt.plot(histogram,color = color)
plt.xlim([0,256])
plt.show()
The output of the given program is shown in the snapshot below:
In the above program, we are importing the modules cv2, numpy, and matplotlib. Then we are reading the image whose histogram is to be calculated using the imread() function. Then we specifying the colors which are iterated to be passed as values of colors to the calcHist() function to calculate the histogram of an image and plot the histogram of an image to be displayed as the output on the screen. The output is shown in the snapshot above.
Conclusion
In this article, we have learned the concept of calcHist() function in OpenCV through definition, syntax, and working of calcHist() function in OpenCV with corresponding programming examples and their outputs to demonstrate them.
Recommended Articles
We hope that this EDUCBA information on “OpenCV Histogram” was beneficial to you. You can view EDUCBA’s recommended articles for more information.