Python OpenCV Histogram Equalization
In this article, you will learn about the Histogram Equalization using Python OpenCV.
Whenever, we saw low-quality, bit-fuzzy images we felt some dis-appointed. Sometimes the image intensity has not clear, may be the camera facing away from the light or at night. Would we enhance the image to a better version? Python provides a way to do this.
Python provides equalizeHist() function to easily perform Histogram Equalization of an image. It improves the contrast and brightness of an image, in order to stretch out the intensity range. This functionality helps in face detection. It flattens the graylevel histogram of an image so that all intensities are as equally common as possible. We can easily improve lightning of the low contrast image. Almost all camera system and image editing tools use histogram equalization to make pictures look better or to enhance the contrast of the image.
Syntax of equalizeHist()
Here, src is the source image and dst is destination image of the same size and type as src.
OpenCV Histogram Equalization Programs
These are the step by step explanation of the histogram equalization program -
- First, we load the image using imread() method,
- Next, convert the original image in between RGB/BGR and YUV by using color conversion method cv2.cvtColor(image, flag). The flag parameter determines the type of conversion.
- Next, perform the histogram equalization using the OpenCV function cv2.equalizeHist().
- Again, convert the image color from YUV family to BGR.
- At last, display both the original image and equalized image using cv2.imshow() function. We have used the hstack() method of numpy module to stack both image values.
Example1 of Python Histogram Equalization
import cv2 import numpy as np # load the source image img = cv2.imread('nature_org.jpg') # convert it to grayscale img_yuv = cv2.cvtColor(img,cv2.COLOR_BGR2YUV) # apply histogram equalization img_yuv[:,:,0] = cv2.equalizeHist(img_yuv[:,:,0]) hist_eq = cv2.cvtColor(img_yuv, cv2.COLOR_YUV2BGR) # display both images (original and equalized) cv2.imshow("equalizeHist", np.hstack((img, hist_eq))) cv2.waitKey(0)
The above code returns output something like this. On the left is the original image and on the right is the histogram image after histogram equalization.
Example2 of Python Histogram Equalization
In the second example, we have taken a grayscale image and apply the histogram equalization function.
import cv2 import numpy as np # load the source image img = cv2.imread('veg.png') # convert it to grayscale img_yuv = cv2.cvtColor(img,cv2.COLOR_BGR2YUV) # apply histogram equalization img_yuv[:,:,0] = cv2.equalizeHist(img_yuv[:,:,0]) hist_eq = cv2.cvtColor(img_yuv, cv2.COLOR_YUV2BGR) # display both images (original and equalized) cv2.imshow("equalizeHist", np.hstack((img, hist_eq))) cv2.waitKey(0)
The above code returns the following output -
Related ArticlesPython OpenCV Histograms of Grayscale Image
Python OpenCV ColorMap
Python OpenCV Gaussian Blur Filtering
Python OpenCV Overview and Examples
Draw different shapes on image using Python OpenCV
How to convert MySQL query result to JSON in Python
How to display data from MongoDB in HTML table using Python
CRUD operations in Python using MongoDB connector
Write Python Pandas Dataframe to CSV
Quick Introduction to Python Pandas
Python Pandas DataFrame
Python3 Tkinter Messagebox
Python get visitor information by IP address