A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/python/histograms-equalization-opencv/ below:

Histograms Equalization in OpenCV - GeeksforGeeks

Histograms Equalization in OpenCV

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?
  1. Enhanced Contrast: It improves the contrast of an image, making hidden details more visible, especially in images with low contrast such as those that are too dark or too bright.
  2. Improved Clarity: By adjusting the contrast, it helps clarify the image, making it easier to distinguish features.
  3. Efficient and Simple: The process is quick to apply and computationally efficient, requiring minimal resources.
Working of Histogram Equalization

Let's see various steps involved in this process:

  1. Calculate the Histogram: The first step is to create a histogram of the image which shows how often each pixel value occurs.
  2. Cumulative Distribution Function (CDF): Next, we calculate the cumulative distribution of pixel intensities. This helps us understand how the intensities are spread out across the image.
  3. Normalize the CDF: We normalize the CDF so that the pixel values can be mapped to a new range (usually 0-255). This ensures that the pixel intensities are more evenly spread out.
  4. Apply the Transformation: Finally, we use the normalized CDF to map the original pixel values to new ones, resulting in an image with improved contrast.
How to Perform Histogram Equalization in OpenCV?

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 OpenCV

Before 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:

!pip install opencv-python-headless

Step 2. Importing Libraries

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.

Python
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 Equalization
  1. Medical Imaging: It helps enhance X-ray or CT scan images, making subtle details more visible which helps doctors in diagnosing conditions.
  2. Satellite and Aerial Imaging: In satellite or aerial images, it improves the visibility of terrain and other features by adjusting contrast, helping in land surveying, environmental monitoring and mapping.
  3. Surveillance and Security: It enhances video footage from surveillance cameras, particularly in low-light conditions, making it easier to identify objects or people in dark or overexposed scenes.
Limitations of Histogram Equalization
  1. Noise Amplification: If an image contains a lot of noise, histogram equalization may amplify that noise along with the details.
  2. Over-enhancement: In some cases, it can result in an unnatural-looking image. If this happens, using CLAHE can help limit the over-enhancement.

For 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