In the previous blog, we discussed Histogram Equalization which considers the global contrast of an image. This means that the same transformation function is used to transform all the image pixels. This approach works well for most cases but when the image contains regions that are significantly lighter or darker than most of the image, the contrast in those regions will not be sufficiently enhanced. See the face of the statue in the image below
And sometimes we want to enhance details over small areas in an image rather than the whole image. This problem can be solved if we use a transformation function that is derived from the neighborhood of every pixel in the image. This is what Adaptive Histogram Equalization (AHE) do.
In Adaptive Histogram Equalization (AHE), the image is divided into small blocks called “tiles” (e.g. 64 tiles (8×8) is a common choice). Then each of these blocks is histogram equalized as we did earlier. Finally, we stitch these blocks together using bilinear interpolation.
But this method has a problem. If the pixel values are more or less constant in a block with some noise then the AHE tends to over-amplify the noise. To avoid this, contrast limiting is applied and the method is known as Contrast Limited Adaptive Histogram Equalization (CLAHE).
In CLAHE, we clip the histogram at a predefined value before computing the CDF and are distributed uniformly to other bins before applying histogram equalization as shown in the figure below.
Since the transformation function used in the Histogram Equalization is proportional to the CDF so clipping results in limiting the slope of the CDF and therefore of the transformation function. This way it prevents the noise from being overamplified.
Since for each pixel we are calculating the transformation function from its neighborhood, this is a computationally expensive process. To overcome this, we only compute the transformation function for each block’s center pixel and all the remaining pixels are transformed wrt. these center pixels using interpolation (bilinear or linear depending on the pixel location).
Another good approach is using Sliding Window Adaptive Histogram Equalization (SWAHE) where we slide the window one pixel at a time and incrementally update the histogram for each pixel.
So, let’s summarise the algorithm for CLAHE
CLAHE Algorithm
- Divide the image into blocks or tiles (8×8 is common)
- Plot the histogram and check whether to clip or not.
- CDF and transformation function is then computed for each of the blocks. This transformation function is only appropriate for the block’s center pixel.
- All the remaining pixels are transformed wrt. these center pixels using interpolation.
I hope you understood Adaptive Histogram Equalization and its variants. Now let’s see how to do this using OpenCV-Python
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import numpy as np import cv2 # Load the image in greyscale img = cv2.imread('D:/downloads/original_Contrast.PNG',0) # create a CLAHE object (Arguments are optional). clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8)) out = clahe.apply(img) # Display the images side by side using cv2.hconcat out1 = cv2.hconcat([img,out]) cv2.imshow('a',out1) cv2.waitKey(0) |
The output looks like this
Compare the CLAHE output image with the Histogram Equalized image and see the difference.
Note: To apply CLAHE on color(RGB) images, first, convert them into colorspaces where you have separate color and greyscale components like HSV or LAB and then apply CLAHE on the greyscale component like L or V. After that again convert it into RGB.
I hope this information will help you. Hope you enjoy reading.
If you have any doubt/suggestion please feel free to ask and I will do my best to help or improve myself. Good-bye until next time.