Image Contrast Enhancement Using CLAHE

Parthiban Marimuthu Last Updated : 12 Oct, 2024
7 min read

This article was published as a part of the Data Science Blogathon.

Introduction

Contrast enhancement algorithms have evolved over the last few decades to meet the needs of its objectives. There are two main goals in enhancing an image’s contrast: (i) Improving its appearance for visual interpretation and (ii) facilitating/increasing the performance of subsequent tasks (e.g., image analysis, object detection, and image segmentation). Most contrast enhancement techniques rely on histogram modifications, which can be applied globally or locally. The Contrast Limited Adaptive Histogram Equalization (CLAHE) method overcomes the limitations of global approaches by enhancing local contrast.

CLAHE – Contrast Limited Adaptive Histogram Equalization is an Image Equalization method. CLAHE is a variation of Adaptive histogram equalization (AHE) that prevents contrast over-amplification.

Adaptive histogram equalization

Consider an image in which the pixel values are limited to a specified range. A brighter image, for example, will have all pixels restricted to high values. On the other hand, an excellent image will contain pixels from all sections of the image. So you need to stretch this histogram to either end, which is what Histogram Equalization does (in simple words). This usually increases the image’s contrast.

Image Contrast

Contrast Limited Adaptive Histogram Equalization (CLAHE)

CLAHE works on small areas of an image called tiles rather than the complete image. The surrounding tiles are blended using bilinear interpolation to remove the false boundaries. This algorithm can be used to improve image contrast.

CLAHE can also be applied to color images, often to the luminance channel. The results of equalizing only the luminance channel of an HSV image outperform equalizing all channels of a BGR image.

Let’s look at an example

CLAHE

The division may be seen in the above image, although it is not as clear as it could be. So, let’s look at the histogram and utilize equalization to stretch it to the threshold.

CLAHE Algorithm

CLAHE was initially used to improve low-contrast medical images.CLAHE differs from ordinary AHE in that it limits contrast. To address the issue of noise amplification, the CLAHE implemented a clipping limit. Before computing the Cumulative Distribution Function, the CLAHE limits the amplification by clipping the histogram at a predefined value (CDF). The CLAHE technique divides an input original image into non-overlapping contextual regions known as sub-images, tiles, or blocks.

The CLAHE is defined by two parameters: Block Size (BS) and Clip Limit (CL). These two parameters primarily govern improved image quality. When CL is increased, the image becomes brighter because the input image has a very low intensity and a larger CL makes its histogram flatter.

As the BS increases, the dynamic range expands, and the image contrast increases. The two parameters determined at the point of maximum entropy curvature produce subjectively good image quality when using image entropy.

Python Code:

# Import the necessary libraries for CLAHE
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread("testing_image.jpeg", 0)
equ = cv2.equalizeHist(img)
print(img)
print(equ)

First, I read my greyscale image and assigned it to the variable image. We can use cv2.equalizeHist to equalize histograms (Image).
Let’s have a look at the histogram of our test photograph. And you can see that it is tilted to the right.

plt.hist(img.flat, bins=100, range=(0, 255))
Image Contrast

Let’s have a look at the histogram of the equalized image. The histogram is also expanded to 255.

plt.hist(equ.flat, bins=100, range=(0, 255))
equalized image

The histogram equalized image is shown below.

histogram equalized image

The histogram e result is shown below. As you can see, the above image has a lot of noise because it considers the image’s global contrast rather than just the local contrast. As a result, performing global equalization on your image may not work very well. In such cases, we can use Adaptive Histogram Equalization, also known as CLAHE.
Contrast Limited AHE (CLAHE) is a variant of adaptive histogram equalization that limits contrast amplification to reduce noise amplification. CLAHE, in a nutshell, performs histogram equalization in small patches or small tiles with high accuracy and contrast limiting.

clahe = cv2.createCLAHE(clipLimit =2.0, tileGridSize=(8,8))
cl_img = clahe.apply(img)
Image Contrast

As shown in the image above, CLAHE produces significantly better results than the standard equalized image. However, there is still a lot of noise. Let’s see how thresholding works to get better results.
Use.tiff file format instead of.jpeg file format for better image results. Before we begin thresholding, we must first examine the CLAHE image’s histogram.

plt.hist(cl_img.flat, bins=100, range=(100, 255))
image equalizer

As shown in the histogram above, there is a dip between 160 and 200, and we can choose a close number to separate those two peaks. We can do the thresholding after we’ve decided on a close number (I’ve chosen 190).

ret, thresh1 = cv2.threshold(cl_img, 190, 150, cv2.THRESH_BINARY)
ret, thresh2 = cv2.threshold(cl_img, 190, 255, cv2.THRESH_BINARY_INV)

Ignore the first ret argument. We can get the thresholded image to variablethresh1 and variablethresh2. The image is the first argument in the above code section, followed by the threshold value we chose, a value for all the thresholded pixels, and finally, a method. I separated the variables THRESH BINARY and THRESH BINARY INV.

THRESH BINARY

The grey level in the first threshold image (thresh1) is 150, and the grey level in the second threshold image (thresh2) is 255. This is simply histogram-based thresholding.

Using the histogram, we determined that 190 is the best value in the preceding example. However, there is an easier way to find the best value with OTSU.

ret, thresh3 = cv2.threshold(cl_img, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)

We can automatically segment it using OTSU.

thresh3image

If you’re using a binary threshold, OTSU is the best way to find the best value. If necessary, K-means can be used.
That concludes the article. If your image is noisy, remember to Denise it first. You can then perform all of these exercises.

Suggestions for Histogram Equalization

When creating your image processing pipelines and determining that histogram equalization is required, I recommend beginning with simple histogram equalization using cv2.equalizeHist. However, if the results are poor and you prefer to increase the noise in the input image, you should try using adaptive histogram equalization via cv2.createCLAHE.

Difference Between Contrast Stretching and Histogram equalization

The goal of contrast stretching is to increase the difference between the maximum and minimum intensity values in an image. The remaining intensity values are distributed across this range.

Histogram equalization is the process of modifying the intensity values of all the pixels in an image so that the histogram is “flattened”.

There is a one-to-one relationship of intensity values between the source image and the target image in contrast stretching, which means that the original image can be restored from the contrast-stretched image.
However, once histogram equalization has been applied, there is no way to recover the original image.

Parameters

There are two parameters to remember when using CLAHE:

clipLimit – This parameter controls the contrast limiting threshold. The default setting is 40.
tileGridSize – Determines the number of tiles in each row and column. This is set to 88 by default. It is applied while the image is divided into tiles for CLAHE.

CLAHE is used for?

Contrast limited adaptive histogram equalization (CLAHE) is a technique for increasing the visibility of a foggy image or video. We used the CLAHE enhancement method in this blog to improve image quality in a real-time system.

Frequently Asked Questions

Q1. What is the function of Clahe in OpenCV?

A. CLAHE (Contrast Limited Adaptive Histogram Equalization) is a function in OpenCV, an open-source computer vision library. It enhances the contrast of an image by redistributing the intensity levels in local regions, using adaptive histogram equalization. Unlike traditional histogram equalization, CLAHE limits the amplification of intensity differences to avoid excessive noise amplification and over-enhancement in homogeneous areas. It is particularly useful for improving the visibility of details in images with varying lighting conditions or low contrast.

Q2. What are the steps of Clahe?

A. The steps involved in CLAHE (Contrast Limited Adaptive Histogram Equalization) are as follows:
1. Image Partitioning: The input image is divided into smaller, non-overlapping tiles or regions.
2. Histogram Calculation: A histogram is computed for each tile, representing the intensity distribution within that region.
3. Contrast Enhancement: The histogram of each tile is modified to increase the contrast, applying histogram equalization within each tile.
4. Clipping: To prevent over-amplification of noise, contrast-limited clipping is performed on the histogram, limiting the number of pixel values that can be amplified.
5. Interpolation: To avoid abrupt transitions at the tile boundaries, the enhanced tiles are interpolated to create the final output image.
By locally applying histogram equalization, CLAHE adapts to the varying contrast within different regions of the image, producing a more visually pleasing result compared to global histogram equalization.

Conclusion

In summary, CLAHE works well in the majority of situations. Then you can use the histogram to segment your image into these two phases, but if you don’t want to plot the histogram, you can use OTSU.

Key Takeaways 

  1. While carrying out AHE If the region being processed has a narrow intensity range, the noise in that region is amplified.
  2. A variant of AHE known as CLAHE is used to reduce the appearance of such noise.
  3. The amount of contrast enhancement for a given intensity is proportional to the slope of the CDF function at that level of intensity.

Thanks for reading!

The media shown in this article is not owned by Analytics Vidhya and is used at the Author’s discretion.

I'm Parthiban and I have more than one year experience in Machine Learning and Deep Learning. A Mechanical Engineering enthusiast with the passion to learn more about Machine Learning and emerging technology in the world. Study and transform data science prototypes and Design machine learning systems. Research and implement appropriate ML algorithms and tools. Develop machine learning applications and select appropriate datasets and data representation methods. Perform statistical analysis and fine-tuning using test results, and extend existing ML libraries and frameworks.

Responses From Readers

Clear

We use cookies essential for this site to function well. Please click to help us improve its usefulness with additional cookies. Learn about our use of cookies in our Privacy Policy & Cookies Policy.

Show details