CLAHE Histogram Equalization – OpenCV


Histogram equalization is a technique used in image processing to enhance the contrast of an image by redistributing the pixel intensities in a way that maximizes the overall brightness and detail. The method works by adjusting the frequency distribution of the pixel values in an image, such that the resulting image has a uniform histogram. The uniform histogram ensures that each pixel in the image has an equal chance of appearing, resulting in a well−distributed image with enhanced contrast.

Histogram equalization is an important tool in image processing and is commonly used in various applications, including medical imaging, remote sensing, and computer vision. The technique is particularly useful in enhancing images that suffer from poor contrast or brightness, such as those captured under low light conditions or those that are overexposed.

Histogram equalization is an important tool in image processing and is commonly used in various applications, including medical imaging, remote sensing, and computer vision. The technique is particularly useful in enhancing images that suffer from poor contrast or brightness, such as those captured under low light conditions or those that are overexposed.

Despite its effectiveness in enhancing image contrast, histogram equalization can lead to some artifacts in the resulting image, such as over−amplification of noise and loss of detail. Therefore, various variations and improvements of the method have been developed over the years, such as adaptive histogram equalization, contrast−limited adaptive histogram equalization, and local histogram equalization, among others.

Now let's understand a little about CLAHE.

CLAHE

Contrast Limited Adaptive Histogram Equalization (CLAHE) is a variant of Histogram Equalization that has been widely used in image processing applications to improve image contrast while avoiding over−amplification of noise and preserving image details. The main idea behind CLAHE is to perform Histogram Equalization locally, in smaller regions of the image, instead of globally.

In traditional Histogram Equalization, the image is divided into small non−overlapping regions or sub−images, and each sub−image is individually equalized. This process can result in over−amplification of noise in the sub−images with low contrast, leading to a noisy and artificial−looking image. In contrast, CLAHE divides the image into overlapping regions and applies Histogram Equalization to each region separately. This allows for better contrast enhancement while preserving local details and avoiding over−amplification of noise.

CLAHE involves two main steps: contrast enhancement and limiting the contrast. In the first step, a local Histogram Equalization is applied to each small region of the image, resulting in increased contrast. In the second step, the contrast of the image is limited by applying a non−linear function to the histogram, such that the number of pixels with very high or very low intensities is reduced. The non−linear function is defined by a parameter called the clip limit, which determines the amount of contrast limitation to be applied.

The clip limit is an important parameter in CLAHE. It determines the maximum amount of contrast amplification that can be performed in each region before the contrast is limited. If the clip limit is set too high, the image may become over−enhanced and suffer from artificial−looking effects, such as halos around edges or an overall washed−out appearance. On the other hand, if the clip limit is set too low, the contrast enhancement may not be sufficient, and the image may appear dull.

CLAHE has several advantages over traditional Histogram Equalization. First, it can preserve local details and enhance contrast more effectively. Second, it can avoid over−amplification of noise, which is particularly important in medical imaging applications, where images may be noisy. Finally, it is a simple and computationally efficient algorithm, making it suitable for real−time applications.

In conclusion, CLAHE is a powerful technique for enhancing image contrast while preserving local details and avoiding over−amplification of noise. It has been widely used in various applications, including medical imaging, remote sensing, and computer vision. Its ability to adapt to local contrast variations and avoid over−amplification of noise makes it a valuable tool for enhancing images in many fields.

This tutorial will demonstrate the process of using Contrast Limited Adaptive Histogram Equalization (CLAHE) to equalize images.

The contrast of images can be enhanced using this algorithm. Additionally, CLAHE can be applied to color images, with better results achieved when applying the algorithm solely to the luminance channel of an HSV image rather than equalizing all the channels of a BGR image.

When using CLAHE, it is important to keep two parameters in mind: the clipLimit and the tileGridSize.

The clipLimit sets the threshold for contrast limiting and has a default value of 40. Meanwhile, the tileGridSize determines the number of tiles in each row and column when dividing the image for CLAHE application, and its default value is 8x8.

Now it's enough theory, let's explore how we can make use of CLAHE to do histogram equalization.

Before we jump into the code, we first need to make sure that opencv module is installed in our machine, and if not, then we need to run the command shown below.

Command

pip3 install opencv-python 

Consider the code shown below.

Example

import cv2
import numpy as np

# Reading the image from the present directory
image = cv2.imread("aircraft-ww2.jpeg")
# Resizing the image for compatibility
image = cv2.resize(image, (500, 600))

# The initial processing of the image
# image = cv2.medianBlur(image, 3)
image_bw = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# The declaration of CLAHE
# clipLimit -> Threshold for contrast limiting
clahe = cv2.createCLAHE(clipLimit = 5)
final_img = clahe.apply(image_bw) + 30

# Ordinary thresholding the same image
_, ordinary_img = cv2.threshold(image_bw, 155, 255, cv2.THRESH_BINARY)

# Showing all the three images
cv2.imshow("ordinary threshold", ordinary_img)
cv2.imshow("CLAHE image", final_img)
cv2.waitKey()

Explanation

This code performs a simple image processing task using the OpenCV library in Python.

First, it imports the necessary libraries: OpenCV and NumPy. It then reads an image named "aircraft−ww2.jpeg" from the current directory and resizes it to 500x600 for compatibility.

Next, the image is converted to grayscale using the cv2.cvtColor() function. The resulting image_bw variable stores the grayscale image.

Afterwards, the code applies Contrast Limited Adaptive Histogram Equalization (CLAHE) to the grayscale image using the cv2.createCLAHE() function. The clipLimit parameter is set to 5, which determines the threshold for contrast limiting. The resulting image is then adjusted by adding a value of 30 to increase its brightness.

In addition to the CLAHE image, the code also performs ordinary thresholding on the grayscale image using the cv2.threshold() function. The threshold value is set to 155, meaning that any pixel value above 155 is set to 255 (white), and any value below 155 is set to 0 (black). The resulting image is stored in the variable ordinary_img.

Finally, the code displays all three images using the cv2.imshow() function, with "ordinary threshold" representing the ordinary thresholding image, and "CLAHE image" representing the image enhanced by CLAHE. The code waits for a key press before closing the windows using cv2.waitKey().

To run the above code we need to run the command shown below.

Command

python3 main.py

Once we run the above command, we can expect the output to the same as the one shown below.

Output

On the left, we have the original image without the clahe, and the image on the right is the one where we used CLAHE.

Now let's make use of one more example.

Where we will change cliplimit and titleGridSize.

Consider the code shown below.

Example

import cv2
import numpy as np

# Reading the image from the present directory
image = cv2.imread("aircraft-ww2.jpeg")
# Resizing the image for compatibility
image = cv2.resize(image, (500, 600))

# The initial processing of the image
# Uncomment the line below to apply median filtering
# image = cv2.medianBlur(image, 3)
image_bw = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# The declaration of CLAHE
# clipLimit -> Threshold for contrast limiting
# tileGridSize -> Number of tiles in the row and column
clahe = cv2.createCLAHE(clipLimit=10, tileGridSize=(8, 8))
final_img = clahe.apply(image_bw)

# Adjust the brightness of the output
final_img = cv2.add(final_img, 50)

# Showing all the three images
cv2.imshow("Original image", image)
cv2.imshow("CLAHE image", final_img)
cv2.waitKey()

Explanation

First, we import the necessary libraries, OpenCV and NumPy.

We then read an image named "aircraft−ww2.jpeg" from the current directory using the cv2.imread() function and store it in the 'image' variable. We resize the image to 500 x 600 pixels to ensure compatibility with the display window.

Next, we convert the image to grayscale using the cv2.cvtColor() function and store it in a new variable named 'image_bw'.

Then, we create a Contrast Limited Adaptive Histogram Equalization (CLAHE) object using the cv2.createCLAHE() function. We set the 'clipLimit' parameter to 10, which sets the threshold for contrast limiting, and the 'tileGridSize' parameter to (8,8), which sets the number of tiles in the row and column.

We then apply the CLAHE algorithm to the grayscale image using the apply() method of the CLAHE object and store the result in a new variable named 'final_img'.

Finally, we adjust the brightness of the output image by adding a value of 50 to the 'final_img' variable using the cv2.add() function. We display the original image and the CLAHE−enhanced image side−by−side using the cv2.imshow() function and wait for a key press using the cv2.waitKey() function.

To run the above code we need to run the command shown below.

Command

python3 main.py

Once we run the above command, we can expect the output to the same as the one shown below.

Output

Conclusion

In conclusion, Contrast Limited Adaptive Histogram Equalization (CLAHE) is a powerful image enhancement technique that can improve the contrast and dynamic range of an image.

With the help of OpenCV, it is easy to implement CLAHE on grayscale and color images. By adjusting the clipLimit and tileGridSize parameters, we can fine−tune the performance of the CLAHE algorithm to suit our needs. Overall, CLAHE is a useful tool in image processing and can help to improve the visual quality of images in a variety of applications.

Updated on: 02-Aug-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements