Python OpenCV Histogram Equalization
In this article, you will learn about histogram equalisation using the Python OpenCV.
Whenever we saw low-quality, bit-fuzzy images, we felt some disappointment. Sometimes the image intensity is not clear, maybe the camera is facing away from the light or at night. Would we enhance the image for a better version? Python provides a way to do this.
Python provides the equalizeHist() function to easily perform Histogram Equalization on an image. It improves the contrast and brightness of an image in order to stretch out the intensity range. This functionality helps with face detection. It flattens the gray-level histogram of an image so that all intensities are as equally common as possible. We can easily improve the lighting of the low contrast image. Almost all camera systems and image editing tools use histogram equalization to make pictures look better or enhance the contrast of the image.
Syntax of equalizeHist()
cv2.equalizeHist(src[, dst])
Here, src is the source image and dst is the destination image of the same size and type as src.
OpenCV Histogram Equalization Programs
These are the step-by-step explanations of the histogram equalization program-
- First, we load the image using the imread() method.
- Next, convert the original image between RGB/BGR and YUV by using the colour 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 colour from the YUV family to BGR.
- Finally, display both the original image and equalized image using the cv2.imshow() function. We have used the hstack() method of the 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 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 applied 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 Articles
Python OpenCV Erosion and DilationExtract text from image using Python
Python project ideas for beginners
Python OpenCV Gaussian Blur Filtering
Python OpenCV Overview and Examples
Draw different shapes on image using Python OpenCV
OpenCV Logical Operators- Bitwise AND, OR, NOR, XOR
Python OpenCV Histogram of Grayscale Image
Python OpenCV Image Filtering
Python OpenCV Overlaying or Blending Two Images
Python OpenCV Histogram of Color Image
Arithmetic Operations on Images using Python OpenCV
Detect Specific Color From Image using Python OpenCV
Capture a video in Python OpenCV and save
Contour Detection using Python OpenCV
Python OpenCV ColorMap
Adaptive Thresholding in Python OpenCV
Geometric Transformation OpenCV Python
Python Turtle Graphics Overview