Last Updated : 14 Aug, 2025
Histogram Equalization (HE) is a technique used to improve image contrast by redistributing pixel intensity values across the entire range. It is especially effective in images where the foreground and background have similar brightness, making it hard to distinguish details. By enhancing areas with low contrast, it makes hidden features more visible, showing finer details in both dark and bright regions. OpenCV’s cv2.equalizeHist() function simplifies this process, improving the image’s contrast for clearer, more defined features.
Why Do We Use Histogram Equalization?Let's see various steps involved in this process:
OpenCV makes it easy to apply histogram equalization with the cv2.equalizeHist() function. This function works on grayscale images, so we need to convert a color image to grayscale first. Let's see step-by-step process involved in performing Histogram Equalization.
Step 1. Installing OpenCVBefore using OpenCV, we need to make sure it’s installed in the Colab environment. If OpenCV isn't already installed, we can install it using the following command:
Step 2. Importing Libraries!pip install opencv-python-headless
Here we will be importing OpenCV, Numpy and Matplotlib libraries for the implementation.
Python
import cv2
import numpy as np
import matplotlib.pyplot as plt
Step 3. Applying Histogram Equalization
Now first we load the image, for histogram equalization we typically work with grayscale images and then apply histogram equalization to improve its contrast and stack the original and equalized images for comparison. Here we will be using a random sample image which you can download from here, also you can use any image.
img = cv2.imread('/content/koala sample.jpeg', 0)
equ = cv2.equalizeHist(img)
res = np.hstack((img, equ))
Step 4. Displaying the Result
Finally, we display the stacked images using matplotlib.
Python
plt.figure(figsize=(10, 5))
plt.imshow(res, cmap='gray')
plt.title("Original vs Equalized Image")
plt.axis('off')
plt.show()
Output:
Output Real-Life Applications of Histogram EqualizationFor related article, refer to Analyze-image-using-histogram
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4