How to improve the image contrast using histogram equalization

Key takeaways:

  1. Histogram equalization is a technique in image processing used to enhance the contrast of an image by redistributing its intensity values. It helps in making the image clearer and more detailed, improving visibility between intensity levels.

  2. The types of histogram equalization are:

    1. Global histogram equalization

    2. Contrast limited adaptive histogram equalization (CLAHE)

    3. Adaptive histogram equalization (AHE)

  3. It can be used in different applications. like medical imaging, surveillance, machine learning and computer vision.

  4. While histogram equalization improves contrast, it can amplify noise in certain images. More advanced techniques like contrast stretching may be preferred in such cases.

Histogram equalization in image processing enhances the contrast and improves the visual quality of an image. Histogram equalization is to spread out the intensity valuesIntensity value refers to each pixel's value in an Image. of an image’s histogram to cover a wider range, resulting in a more balanced distribution of pixel intensities.

It makes the image appear clearer and more detailed, enhancing the differences between different intensity levels.

Types of histogram equalization

There are different variations derived from the primary histogram equalization method. These variations aim to address certain limitations or improve specific aspects of the equalization process. Some of the notable types of histogram equalization include:

  • Global histogram equalization: It is the standard histogram equalization technique. It operates on the entire image and enhances the global contrast by spreading the intensity values.

  • Contrast limited adaptive histogram equalization (CLAHE): CLAHE is an adaptive version of histogram equalization. It divides the image into smaller blocks or tiles and applies histogram equalization to each block individually. It avoids over-amplifying noiseWhen histogram equalization is applied to an image, it stretches the range of pixel values to enhance contrast. If there are minor changes or spikes in pixel values as a result of noise, those variations can also be stretched. This can amplify and bring out noise in the output image, resulting in undesired effects. and small intensity variations in flat regions while enhancing local contrast. CLAHE is particularly useful for images with varying lighting conditions.

  • Adaptive histogram equalization (AHE): Similar to CLAHE, AHE is an adaptive method that works on small regions of an image. However, unlike CLAHE, AHE does not include a contrast-limiting mechanismIt is a technique used to control the extent to which the contrast of an image is enhanced or adjusted.. As a result, AHE can amplify noise in some cases.

Applications

Histogram equalization has different applications in image processing and computer vision due to its ability to enhance the contrast and improve the visual quality of images. Here are some common applications:

  • Medical imaging: Histogram equalization can improve the visibility of structures in medical images, such as CT scans, X-rays, and MRI images. Poor contrast may obscure essential details.

  • Enhancement for surveillance: Surveillance cameras capture scenes with varying lighting conditions. Histogram equalization can help improve the visibility of objects and people in well-lit and poorly-lit areas.

  • Machine Learning and Computer Vision: Machine learning and Computer vision algorithms usually use high-contrast images. Histogram equalization can contribute to better feature extraction and pattern recognition.

Example code

Press "Run" to see how we can improve the image contrast using histogram equalization.

import cv2
import numpy as np
import matplotlib.pyplot as plt
def hist_equalization(img):
# Convert the input image to grayscale
grayscale = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Calculating the histogram of the grayscale image
hist, _ = np.histogram(img, bins=256, range=(0, 256))
# Calculating the cumulative distribution function (CDF)
cdf = hist.cumsum()
cdf_normalized = cdf * 255 / cdf[-1]
# Apply histogram equalization using the CDF
equalized_img = cdf_normalized[grayscale]
# Convert the equalized image back to uint8 type
equalized_img = equalized_img.astype(np.uint8)
return equalized_img
# Load an image using OpenCV
input_img = cv2.imread('/input.jpeg')
# Apply histogram equalization
output_img = hist_equalization(input_img)
f, axarr = plt.subplots(1,2, figsize=(12, 12))

Explanation

Let's analyze the code step by step:

  • Line 9: Computes the histogram of the input image array, dividing it into specified bins with intensity range, and returns histogram values and bin edges.

    • np.histogram() takes three arguments, that are described below:

      • img, refers to the image whose histogram will be calculated.

      • bin, specifies the number of bins (categories) into which the range of intensity values will be divided.

      • range, defines the range of values that will be considered when calculating the histogram.

  • Line 12: Computes the histogram's cumulative distribution function (CDF).

  • Line 13: Normalizes the CDF to a 0-255 scale. It's an essential step to ensure the intensity values remain within the valid range.

  • Line 16: Uses the normalized CDF to transform the intensity values of the grayscale image, resulting in the equalized image.

  • Line 19: Converts the data type of the equalized image to unsigned 8-bit integers (uint8).

  • Line 23: Uses OpenCV's cv2.imread() function to load an image from the file system into memory.

  • Line 25: Applies histogram equalization to the loaded image (input_img), transforming its pixel values to enhance the contrast or dynamic range.

  • Line 26: Creates a figure with subplots to display multiple images side by side using matplotlib.

In summary, this code loads an image, performs histogram equalization on the grayscale version of the image, and displays both the original and equalized images for visual comparison. The equalization process enhances the image's contrast, making it visually appealing.

Conclusion

While histogram equalization is useful, it may not always be suitable for all images and applications. It can amplify noise; in some instances, more advanced techniques like contrast stretching are preferred to address these limitations.

Frequently asked questions

Haven’t found what you were looking for? Contact Us


How can image contrast be improved?

Image contrast can be improved using techniques such as histogram equalization, contrast stretching, or applying filters that enhance edges. Additionally, adjusting brightness and using image processing algorithms can enhance the visibility of details in an image.


What is the histogram of a good contrast image?

The histogram of a good contrast image typically shows a wide spread of pixel intensity values across the entire range (0 to 255 for 8-bit images). This distribution indicates that both dark and light areas are well represented, avoiding clustering of pixel values at the extremes.


How does histogram equalization make image intensity changes?

Histogram equalization transforms the intensity values of an image by redistributing them based on the cumulative distribution function (CDF) of the pixel intensities. This process spreads out the most frequent intensity values, allowing for better contrast and visibility of features in the image.


What are two ways in which contrast can be improved?

Two ways to improve contrast include:

  • Histogram equalization: Adjusts the intensity distribution of the entire image to enhance contrast.
  • Contrast stretching: Increases the range of intensity values in the image by stretching the pixel values to cover a wider spectrum.

Does histogram equalization increase contrast?

Yes, histogram equalization increases contrast by redistributing pixel intensity values across the available range, which enhances the visibility of details and differences in the image.


Free Resources

Copyright ©2025 Educative, Inc. All rights reserved