Histogram equalization for digital image processing (python)

1. Concept

       Histogram equalization is mainly used to enhance the contrast of images with a small dynamic range. Histogram equalization is carried out by means of gray level statistical histogram and gray level cumulative histogram. The gray level statistical histogram reflects the number of pixels with different gray levels in the image; the gray level cumulative histogram reflects the number of pixels in the image whose gray level is less than or equal to a certain value.

1.1 Basic idea:

Convert the histogram of the original image into a form of uniform distribution, increase the dynamic range of the pixel gray value, and enhance the overall contrast of the image.

1.2 Algorithm steps:

(1) List the gray level k of the original image , k =0,1,2,..., L -1 , L is the number of gray levels;

(2) List the normalized expression form sk of the gray value of the kth level of the original image ;

(3) Count the number of pixels n k of each gray level , k = 0,1,2,..., L -1 ;

(4) Obtain the normalized probability expression form of the gray statistical histogram: p s ( s k )= n k / N ;

(5) Calculate the grayscale cumulative histogram based on the cumulative distribution function:

(6) Carry out rounding and expansion, and calculate the normalized expression form t k of the gray value corresponding to each gray level of the output image after mapping :

t k = INT(( L -1) E ( s k )+0.5)/255

Among them, INT is a rounding function;

(7) Determine the mapping relationship s kt k ;

(8) Number n k of pixels of each gray level after statistical mapping ;

(9) Obtain the normalized probability expression form of the new gray level statistical histogram: p t ( t k )= n k / N , where N is the number of pixels in the output image, that is, the number of pixels in the original image.

The operation method of specific steps is shown in Table 1-1.

Table 1-1   Histogram equalization calculation list

 2. Realize histogram equalization with python

In python, the cv2.equalizeHist() function can be used to equalize the histogram of the image; the cv2.calcHist() function can be used to count the number of pixels in each gray level of the image. Function parameters:

 cv2.calcHist(image, channels, mask, histSize, ranges, accumulate)

The meaning of each parameter in the function is as follows:

1. image: read in the image, this parameter needs to be enclosed in "[ ]".

2. channels: channel number, if the read-in image is a single-channel grayscale image, the value of this parameter is [0]; for the read-in color image, its value can be [0], [1], [2] Corresponding to channels B, G, R respectively. The parameter needs to be enclosed in "[ ]".

3. mask: Mask image. When counting the histogram of a certain area of ​​the image, this value needs to be set; when counting the histogram of the entire image, this value is set to None.

4. histSize: BINS value, this parameter needs to be enclosed in "[ ]".

5. ranges: pixel value range, for example, the pixel value range of 8-bit grayscale image is [0, 256].

6. accumulate: cumulative flag, the default is False. If it is set to True, the histogram will not be cleared at the beginning of the calculation, and the calculation is the cumulative result of multiple histograms, which is used to calculate the histogram for a group of images. This parameter allows computing a single histogram from multiple objects, or updating the histogram in real time. This parameter is optional and generally does not need to be set.

An example of image equalization in python is as follows:

Summarize

1. Advantages of histogram equalization:

Ability to enhance the contrast of the entire image.

2. Disadvantages of histogram equalization:

( 1 ) The enhancement effect is not easy to control, and the result of processing always gets a global uniform histogram.

( 2 ) The dynamic range of the equalized image is expanded, essentially expanding the quantization interval, but the quantization level (gray level) is reduced instead, causing some details to disappear.

( 3 ) For some images, if the histogram has a peak, the contrast may be overly enhanced after processing.

( 4 ) leads to false contours. Pixels with different gray levels may become the same after processing, forming an area of ​​the same gray level, and there are obvious boundaries between each area, resulting in false contours.

Guess you like

Origin blog.csdn.net/hu_666666/article/details/127306483