CLAHE - Contrast Limiting Adaptive Histogram Equalization
Contrast Limit AHE is a variant of the AHE method that used clipping to preserve local contrast. Implementation wise, the method used the clipLimit to identify any pixels above that limit and distribute them equally to the rest of the block pixels before the equalization is performed. In otherwords, clipLimit is a threhold for contrast preservation of the local block size of the image.
OpenCV proves an easy way to implement CLAHE. To perform CLAHE operation, we call the createCLAHE method which takes on two arguments:
- 1. clipLimit: Threhold for local contrast preservation
- 2. tileGridSize: The size of tile to compute histogram equalization.
Example
import cv2
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
%config InlineBackend.figure_format = 'retina'
img = cv2.imread('eclipse.png')
# plot the image
plt.imshow(img)
Implementation of Equalization and Contrast Limiting Equalization
#Ordinary Equalization
def luminosity_equalization(input_img):
lab_img = cv2.cvtColor(input_img, cv2.COLOR_BGR2Lab)
l, a, b = cv2.split(lab_img)
eq_l = cv2.equalizeHist(l)
merged_img = cv2.merge([eq_l, a, b])
eq_img = cv2.cvtColor(merged_img, cv2.COLOR_LAB2BGR)
return eq_img
# CLAHE Equalization
def clahe_equalization(input_img, clip_limit):
lab_img = cv2.cvtColor(input_img, cv2.COLOR_BGR2Lab)
l, a, b = cv2.split(lab_img)
# Create clahe
clahe = cv2.createCLAHE(clipLimit=clip_limit, tileGridSize=(8,8))
eq_l = clahe.apply(l)
# Equalized Image
eq_image = cv2.cvtColor(cv2.merge([eq_l, a, b]), cv2.COLOR_Lab2BGR)
return eq_image
eq_image = luminosity_equalization(img) clahe_image = clahe_equalization(img, clip_limit=4.0)
Visualizing Normal Equalization and CLA Histogram Equalization
fig = plt.figure(figsize=(20, 7))
fig.add_subplot(131)
plt.imshow(img)
plt.title('Original Image')
fig.add_subplot(132)
plt.imshow(eq_img)
plt.title('Histogram Equalized')
fig.add_subplot(133)
plt.imshow(clahe_image)
plt.title('CLAHE Equalized')