OpenCV provides the cv2.calcHist() function in order to calculate the histogram of one or more arrays. Therefore, this function can be applied to single-channel images (for example, grayscale images) and to multi-channel images (for example, BGR images).
In this section, we are going to see how to calculate histograms for grayscale images. The signature for this function is as follows:
cv2.calcHist(images, channels, mask, histSize, ranges[, hist[, accumulate]])
To this, the following applies:
- images: It represents the source image of type uint8 or float32 provided as a list (example, [gray_img]).
- channels: It represents the index of the channel for which we calculate the histogram provided as a list (for example, [0] for grayscale images, or [0],[1],[2] for multi-channel images to calculate the histogram for the first, second, or third channel, respectively).
- mask: It represents a mask image to calculate the histogram of a specific region of the image defined by the mask. If this parameter is equal to None, the histogram will be calculated with no mask and the full image will be used.
- histSize: It represents the number of bins provided as a list (for example, [256]).
- ranges: It represents the range of intensity values we want to measure (for example, [0,256]).