CLAHE Histogram Equalization - OpenCV
CLAHE (Contrast Limited Adaptive Histogram Equalization) is used to improve the contrast of images. In traditional methods, contrast of whole image changes but CLAHE works by dividing the image into smaller parts and adjust the contrast in each part separately. This helps in avoiding the image getting too bright or too dark in some areas. In this article, we’ll see how to use CLAHE in Python with OpenCV.
When applying CLAHE, there are two parameters to remember:
- clipLimit: This parameter sets the threshold for contrast limiting. By default value is 40.
- tileGridSize: It is used to divide the image into grids for applying CLAHE. It sets the number of rows and columns. By default this is 8x8.
We process each tile using adaptive histogram equalization, which adjusts pixel intensities based on the local distribution of pixel values. After processing the tiles, it combines them using bilinear interpolation to remove visible boundaries between the tiles. This step ensures that the transition between adjacent tiles is consistent and preserves the natural look of the image while enhancing contrast in local areas.
Now lets see implement this using python.
Input image:

- image_resized = cv2.resize(image, (500, 600)): Resizes the image to 500x600 pixels.
- image_bw = cv2.cvtColor(image_resized, cv2.COLOR_BGR2GRAY): Converts resized image to grayscale.
- clahe = cv2.createCLAHE(clipLimit=5): Creates a CLAHE object with a contrast limit of 5.
- clahe_img = np.clip(clahe.apply(image_bw) + 30, 0, 255).astype(np.uint8): Applies CLAHE to the image and adds 30 to the result which helps in clipping values to the valid range.
- _, threshold_img = cv2.threshold(image_bw, 155, 255, cv2.THRESH_BINARY): Applies binary thresholding to the grayscale image at a threshold of 155.
import cv2
import numpy as np
import os
def display_image(title, image):
try:
from google.colab.patches import cv2_imshow
cv2_imshow(image)
except ImportError:
cv2.imshow(title, image)
cv2.waitKey(0)
cv2.destroyAllWindows()
image_path = "image.jpg"
image = cv2.imread(image_path)
image_resized = cv2.resize(image, (500, 600))
image_bw = cv2.cvtColor(image_resized, cv2.COLOR_BGR2GRAY)
clahe = cv2.createCLAHE(clipLimit=5)
clahe_img = np.clip(clahe.apply(image_bw) + 30, 0, 255).astype(np.uint8)
_, threshold_img = cv2.threshold(image_bw, 155, 255, cv2.THRESH_BINARY)
display_image("Ordinary Threshold", threshold_img)
display_image("CLAHE Image", clahe_img)
Output:


We can see that by using CLAHE our image got much better and readable compared to ordinary thresholding. This technique is also used in medical images and satellite photos.