Aligning histograms in OpenCV

Histogram alignment — it is a method of processing the contrast of an image using an image histogram.

This method usually increases the overall contrast of many images, especially when the usable image data is represented by similar contrast values. By this setting, intensities can be better distributed over the histogram. This allows areas of lower local contrast to receive higher contrast. Histogram flattening does this by effectively distributing the most frequent intensity values. This method is useful for background and foreground images that are bright or dark.

OpenCV has a function for this, cv2.equalizeHist () . Its input is just a grayscale image, and the output is — histogram aligned image.

Input image:

Below is the Python3 code that implements histogram alignment:

# Opencv import

import cv2

# Numpy import

import numpy as np

# read image using imread

img = cv2.imread ( ` F: do_nawab .png` , 0 )

# create histogram alignment
# images using cv2.equalizeHist ()

equ = cv2.equalizeHist (img)

# stacking images side by side

res = np.hstack ((img, equ))

# show input images versus display

cv2.imshow ( `image` , res)


cv2.waitKey ( 0 )

cv2.destroyAllWindows ()