Image Histogram

bogotobogo.com site search:

Image Histogram

"An image histogram is a type of histogram that acts as a graphical representation of the tonal distribution in a digital image. It plots the number of pixels for each tonal value. By looking at the histogram for a specific image a viewer will be able to judge the entire tonal distribution at a glance." - Image histogram.

Histogram is a graphical representation of the intensity distribution of an image.

Before using that function, we need to understand some terminologies related with histograms.

bins :The histogram above shows the number of pixels for every pixel value, from 0 to 255. In fact, we used 256 values (bins) to show the above histogram. It could be 8, 16, 32 etc. OpenCV uses histSize to refer to bins.

dims : It is the number of parameters for which we collect the data. In our case, we collect data based on intensity value. So, in our case, it is 1.

range : It is the range of intensity values we want to measure. Normally, it is [0,256], ie all intensity values.

calcHist()

OpenCV comes with an in-built cv2.calcHist() function for histogram. So, it's time to look into the specific parameters related to the cv2.calcHist() function.

images: source image of type uint8 or float32. it should be given in as a list, ie, [gray_img].

channels: it is also given in as a list []. It the index of channel for which we calculate histogram. For example, if input is grayscale image, its value is [0]. For color image, you can pass [0],[1] or [2] to calculate histogram of blue,green or red channel, respectively.

mask: mask image. To find histogram of full image, it is set as None. However, if we want to get histogram of specific region of image, we should create a mask image for that and give it as mask.

histSize: this represents our BIN count. Need to be given in []. For full scale, we pass [256].

ranges: Normally, it is [0,256].

NumPy - np.histogram()

NumPy also provides us a function for histogram, np.histogram(). So, we can use NumPy fucntion instead of OpenCV function: