Histogram Processing
Histogram analysis, global and adaptive equalization, and local histogram techniques.
Learning Objectives
- ✓Compute and visualize an image histogram
- ✓Apply global histogram equalization
- ✓Use CLAHE for contrast-limited adaptive equalization
- ✓Understand local histogram equalization
Histogram Representation
A histogram shows the frequency of each intensity value (0–255) in an image. A well-distributed histogram indicates good contrast. cv2.calcHist() computes it; matplotlib can plot it.
import cv2
import matplotlib.pyplot as plt
img = cv2.imread('brain.jpg', cv2.IMREAD_GRAYSCALE)
hist = cv2.calcHist([img], [0], None, [256], [0, 256])
plt.figure()
plt.subplot(1,2,1), plt.imshow(img, cmap='gray'), plt.title('Image'), plt.axis('off')
plt.subplot(1,2,2), plt.plot(hist), plt.title('Histogram'), plt.xlim([0, 256])
plt.tight_layout()
plt.show()Histogram Equalization
Equalization redistributes intensity values so the histogram is more uniform, improving global contrast. cv2.equalizeHist() does this in one call. Works best when the background and foreground are both dark or both light.
img_eq = cv2.equalizeHist(img)
plt.subplot(1,2,1), plt.imshow(img, cmap='gray'), plt.title('Original')
plt.subplot(1,2,2), plt.imshow(img_eq, cmap='gray'), plt.title('Equalized')
plt.show()CLAHE
Contrast Limited Adaptive Histogram Equalization (CLAHE) divides the image into small tiles and equalizes each tile separately. The "clip limit" prevents over-amplification of noise. Better than global equalization for images with varying lighting.
clip_limits = [1.0, 2.0, 4.0, 8.0]
for clip in clip_limits:
clahe = cv2.createCLAHE(clipLimit=clip, tileGridSize=(8, 8))
img_clahe = clahe.apply(img)Local Histogram Equalization
Local equalization applies equalization to a sliding neighborhood window around each pixel. Computationally expensive but preserves fine local detail better than CLAHE in some cases.
from skimage.filters.rank import equalize as rank_equalize
from skimage.morphology import disk
selem = disk(15)
img_local_eq = rank_equalize(img, selem)