Histograms - 2: Histogram Equalization

摘自https://docs.opencv.org/4.2.0/d5/daf/tutorial_py_histogram_equalization.html

摘自https://docs.opencv.org/4.2.0/d4/d1b/tutorial_histogram_equalization.html

Image Histogram is a graphical representation of the intensity distribution of an image.

Histogram Equalization is a method that improves the contrast in an image, in order to stretch out the intensity range.

Consider an image whose pixel values are confined to some specific range of values only. You need to stretch this histogram to either ends.

Histograms Equalization in OpenCV

OpenCV has a function to do this, cv.equalizeHist(). Its input is just grayscale image and output is our histogram equalized image.

equ = cv.equalizeHist(img)

Histogram equalization is good when histogram of the image is confined to a particular region. It won't work good in places where there is large intensity variations where histogram covers a large region, ie both bright and dark pixels are present.

CLAHE (Contrast Limited Adaptive Histogram Equalization)

Histogram equalization considers the global contrast of the image. In many cases, it is not a good idea:

It is true that the background contrast has improved after histogram equalization. But compare the face of statue in both images. We lost most of the information there due to over-brightness. It is because its histogram is not confined to a particular region(Try to plot histogram of input image, you will get more intuition).

To solve this problem, adaptive histogram equalization is used. In this, image is divided into small blocks called "tiles" (tileSize is 8x8 by default in OpenCV). Then each of these blocks are histogram equalized as usual. So in a small area, histogram would confine to a small region (unless there is noise). If noise is there, it will be amplified. To avoid this, contrast limiting is applied. If any histogram bin is above the specified contrast limit (by default 40 in OpenCV), those pixels are clipped and distributed uniformly to other bins before applying histogram equalization. After equalization, to remove artifacts in tile borders, bilinear interpolation is applied.

img = cv.imread('tsukuba_l.png',0)

# create a CLAHE object (Arguments are optional).
clahe = cv.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))
cl1 = clahe.apply(img)

猜你喜欢

转载自blog.csdn.net/Airfrozen/article/details/104439526