Prerequisites: OpenCV Python Program to analyze an image using Histogram
Histogram of a digital image with intensity levels in the range of 0 to L-1 is a discrete function –
h(rk) = nk where rk = kth intensity value and no = number of pixels in the image with rk intensity value.
If the image has M rows and N columns, then the total number of pixels in the image is MN. The normalized histogram is obtained as follows:
p(rk) = nk/MN
Sometimes if the image is a dark, light, or low contrast, then the histogram of that image might not have pixels with minimum intensity (0) or maximum intensity(255) or both respectively. In such cases, the histogram of that image can be stretched by applying the following transformation.
Let a= maximum intensity level in the image Let b= minimum intensity level in the image Let rk= pixel value in the original image Let sk= pixel value in the stretched image constant= (255-0)/(a-b) Then sk= constant*rk
Algorithm for Histogram plotting
- Read the image
- Obtain the size of the image. Let m=rows and n=columns
- For all the intensity levels rk= 0 to 255
- Traverse all the rows ‘m’ and columns ‘n’
- Count the number of pixels for each intensity level
- Plot the graph of intensity values versus number of pixels in each intensity value
Algorithm to stretch the Histogram
- Find the minimum intensity value in the image say ‘a’ and maximum intensity value say ‘b’
- obtain a constant c= (255-0)/(a-b)
- Multiply this constant with each pixel in the image to obtain histogram stretching.
It is observed that the dark input image improves after contrast stretching.
Below is the implementation.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Plotting Histogram in Python using Matplotlib
- Mahotas - Image Stretching
- Python program to count upper and lower case characters without using inbuilt functions
- Python | Permutation of a given string using inbuilt function
- MoviePy – Displaying a Frame of Video Clip using inbuilt display method
- OpenCV Python Program to analyze an image using Histogram
- Histogram using Plotly in Python
- Plot 2-D Histogram in Python using Matplotlib
- Compute the histogram of a set of data using NumPy in Python
- How To Make Histogram with Median Line using Altair in Python?
- sciPy stats.histogram() function | Python
- Inbuilt Data Structures in Python
- Arcade inbuilt functions to draw point(s) in Python3
- Arcade inbuilt functions to draw polygon in Python3
- Python PIL | Image.histogram()
- NumPy.histogram() Method in Python
- Box plot and Histogram exploration on Iris data
- CLAHE Histogram Eqalization - OpenCV
- Histogram in Pygal
- Mahotas – Full Histogram of Image
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.