OPENCV – PYTHON | Image Histogram Equalization | Gray & Color Histograms | Brightness & Contrast
Image Histogram Equalization is a versatile technique employed in image processing to enhance the overall brightness and contrast of an image. By redistributing pixel intensities, this method can make images more visually appealing and improve feature visibility. In this article, we delve into the world of Image Histogram Equalization using OpenCV in Python, exploring both grayscale and color histograms, and understanding how to apply histogram equalization for optimal brightness and contrast enhancement.
Flow of Article:
- Understanding Histogram Equalisation
- Understanding the mathematics behind it!
- Coding Grayscale Histogram equalisation
- Coding Color histograms and their equalisation
Video Explanation:
Understanding Image Histogram Equalization:
Image Histogram Equalization is based on the idea of adjusting the cumulative distribution function (CDF) of pixel intensities to achieve a more uniform distribution. This results in a transformed image with enhanced contrast, making it visually more appealing.
Mathematical Explanation:
For a grayscale image, the transformation function TT can be defined as:
Coding Grayscale Histogram Equalisation
import cv2
import numpy as np
import matplotlib.pyplot as plt
# Load grayscale image
img_gray = cv2.imread(‘image.jpg’, cv2.IMREAD_GRAYSCALE)
# Apply histogram equalization
equ_gray = cv2.equalizeHist(img_gray)
# Display original and equalized images
plt.figure(figsize=(10, 5))
plt.subplot(1, 2, 1)
plt.imshow(img_gray, cmap=’gray’)
plt.title(‘Original Grayscale Image’)
plt.subplot(1, 2, 2)
plt.imshow(equ_gray, cmap=’gray’)
plt.title(‘Equalized Grayscale Image’)
plt.show()
OpenCV Code for Color Histogram Equalization
import cv2
import numpy as np
import matplotlib.pyplot as plt
# Load color image
img_color = cv2.imread(‘image.jpg’, cv2.IMREAD_COLOR)
# Convert to YUV color space
img_yuv = cv2.cvtColor(img_color, cv2.COLOR_BGR2YUV)
# Equalize the intensity channel (Y)
img_yuv[:,:,0] = cv2.equalizeHist(img_yuv[:,:,0])
# Convert back to BGR
equ_color = cv2.cvtColor(img_yuv, cv2.COLOR_YUV2BGR)
# Display original and equalized images
plt.figure(figsize=(10, 5))
plt.subplot(1, 2, 1)
plt.imshow(cv2.cvtColor(img_color, cv2.COLOR_BGR2RGB))
plt.title(‘Original Color Image’)
plt.subplot(1, 2, 2)
plt.imshow(cv2.cvtColor(equ_color, cv2.COLOR_BGR2RGB))
plt.title(‘Equalized Color Image’)
plt.show()
In the color histogram equalization example, the image is first converted to the YUV color space, where the Y channel represents intensity. The histogram equalization is then applied to the Y channel, and the image is converted back to BGR.
Applications and Use Cases:
Medical Imaging:
- Image histogram equalization enhances the contrast of medical images, aiding in the identification of abnormalities and improving diagnostic accuracy.
Satellite Image Processing:
- Satellite images often suffer from variations in lighting conditions. Histogram equalization is valuable for improving feature visibility in satellite imagery.
Photography and Digital Imaging:
- In photography and digital imaging, histogram equalization can be employed to improve the overall visual appeal of images, especially in scenarios with challenging lighting conditions.
Computer Vision and Object Detection:
- Enhanced contrast through histogram equalization is beneficial for computer vision tasks, such as object detection, where well-defined features are crucial.
Historical Image Restoration:
- Historical images with degraded contrast can be revitalized using histogram equalization, bringing out details that may have been obscured over time.
By mastering the techniques of image histogram equalization, practitioners can significantly improve the quality and visual interpretability of images across a wide range of applications.
Histograms, Image processing, infoaryan, Python tutorials, aryan verma blog, opencv tutorials, histogram equalization, image histograms, advance image processing by aryan verma, aryan verma blog, infoaryan