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.
Hi, thankyou so much it was really helpful. Can you please guide me a bit as I am a beginner in image processing stuff. As these histograms will tell us the intensity profile so can we do the backprojection as a next step from here?
Thanks
Hi Thanks for reading this blog.
For histogram backprojection you can follow these two blogs:
https://theailearner.com/2019/04/18/histogram-backprojection/
https://docs.opencv.org/master/dc/df6/tutorial_py_histogram_backprojection.html
Thanks again for your response.
The thing is I am looking for something very simple maybe. I am able to project the image horizontally which is just the sum of all pixel values along the rows. Now I just want to back-project it on the same image plane so that the sum values that I got via projection can be assigned to all the pixels of a given row.
I don’t know if you understood my question. Let’s say if I started with one point object in my original image, the backprojected image should show a horizontal line, if that helps.
Hi, Below is the code where I have first created a one-point object image, find its horizontal projection and then back project.
Hope this helps.
Thanks heaps 🙂 , really appreciate your help!
Your blogs are really very helpful. Thank you
You know what, it is really really clearr!!thanks.