In this blog, we will discuss image histogram which
What is an Image Histogram?
An image histogram tells us how the intensity values are distributed in an image. In this we plot the intensity values on the x-axis and the no. of pixels corresponding to intensity values on the y-axis. See the figure below.
This is called 1D histogram because we are taking only one feature into our consideration, i.e. greyscale intensity value of the pixel. In the next blog, we will discuss 2D histograms.
Now, let’s understand some terminologies associated with histogram
Tonal range refers to the region where most of the intensity values are present (See above figure). The left side represents the black and dark areas known as shadows, the middle represents medium grey or midtones and the right side represents light and pure white areas known as Highlights.
So, for a
Now, let’s see how to plot the histogram for an image using OpenCV and matplotlib.
OpenCV: To calculate the image histogram, OpenCV provides the following function
cv2.calcHist(image, channel, mask, bins, range)
- image : input image, should be passed in a list. e.g. [image]
- channel : index of the channel. for greyscale pass as [0], and for color image pass the desired channel as [0], [1], [2].
- mask : provide if you want to calculate histogram for specific region otherwise pass None.
- bins : No. of bins to use for each channel, should be passed as [256]
- range : range of intensity values. For 8-bit pass as [0,256]
This returns a numpy.ndarray with shape (n_bins,1) which can then be plotted using matplotlib. Below is the code for this
1 2 3 4 5 6 7 8 9 10 |
import cv2 import matplotlib.pyplot as plt # Load the image image = cv2.imread('hist.jpg',0) # Calculate histogram using cv2.calcHist() hist = cv2.calcHist([image], [0], None, [256], [0,256]) # Display the histogram plt.plot(hist) |
Matplotlib: Unlike OpenCV, matplotlib directly finds the histogram and plots it using plt.hist()
1 2 |
plt.hist(image.flatten(), 256, [0,256]) plt.show() |
For a color image, we can show each channel individually or we can first convert it into greyscale and then calculate the histogram. So, a color histogram can be expressed as “Three Intensity(Greyscale) Histograms”, each of which shows the brightness distribution of each individual Red/Green/Blue color channel. Below figure summarizes this.
So, always see the histogram of the image before doing any other pre-processing operation. Hope you enjoy reading.
If you have any doubt/suggestion please feel free to ask and I will do my best to help or improve myself. Good-bye until next time.