Histogram correlation and application

Histogram correlation and application

1) Histogram equalization

Histogram equalization is an important application of grayscale transformation. It is a method to enhance image contrast by stretching the distribution range of pixel intensity. It is widely used in image processing.

image enhancement processing.
insert image description here
Histogram equalization—cv2.equalizeHist()
insert image description here

⚫src: Input the original image, Mat class object is enough, it needs to be an 8-bit single-channel image
⚫dst: The result image after equalization, it needs to be the same size and type as the original image

import cv2
import numpy as np
img = cv2 .imread('02 .png' , 0)
equ = cv2.equalizeHist (img)
res = np.hstack((img,egu)) #将2张图片合并一起
cv2 .imshow('res',res)
cv2 .imshow( 'result',equ)
cv2 .imwrite ('1 .bmp ' ,equ)
cv2 .waitKey (0)
cv2.destroyAllwindows ()

insert image description here
insert image description here

1-1 CLAHE adaptive histogram equalization

When the histogram is not concentrated in a certain area, using histogram equalization will lose information and the effect is not good

To solve this problem, we need to use adaptive histogram equalization. In this case, the entire image will be divided into many small blocks, which are called "tiles" (the default size of tiles in OpenCV is 8x8), and then histogram equalization is performed on each small block (with Similar to the previous) so in each area, the histogram will be concentrated in a small area (unless there is noise interference. If there is noise, the noise will be amplified. To avoid this situation, use contrast limitation. For each small block, if the bin in the histogram exceeds the upper limit of the contrast, evenly disperse the pixels in it to other bins, and then perform histogram equalization. Finally, in order to remove each small block The "artificial" (caused by the algorithm) boundary between the two, and then use the bilinear difference to stitch the small blocks.
insert image description here
insert image description here

2) Histogram comparison

Histogram comparison is to compare the similarity of the histograms of two images according to certain standards, and determine the similarity of the images. Opencv provides a comparison histogram phase

The function of similarity is: cv2.compareHist()
insert image description here
⚫ H1: Histogram 1 to be compared
⚫ H2: Histogram 2 to be compared

⚫ method: There are four methods for histogram comparison:

◆cv2.HISTCMP_CORREL, —correlation method (the larger the value, the higher the matching degree)

◆cv2.HISTCMP_CHISQR, — Chi-square measurement method (the smaller the value, the higher the matching degree)

◆cv2.HISTCMP_INTERSECT, —Histogram intersection method (the larger the value, the higher the matching degree)

◆cv2.HISTCMP_BHATTACHARYYA , —Bhattacharyya measurement method (small)
insert image description here
insert image description here

insert image description here

3) Back projection

Back projection is a way to record how the pixels in a given image adapt to the pixel distribution of the histogram model. In simple terms, the so-called back projection is to first calculate a certain

A histogram model of a feature, and then use the model to find the way that feature exists in the image.

For example, you have a skin color histogram (Hue-Saturation histogram), you can use it to find the skin color area in the image, the specific principle:

Suppose you have obtained a skin color histogram (Hue-Saturation) through the following figure, and the histogram next to it is the model histogram (representing the skin tone of the palm). You can

To grab the histogram of the area where the palm is located through the mask operation:
insert image description here

All we have to do is use the model histogram (representing the skin tone of the palm) to detect skin regions in the test image. The following are the detection steps:
① For each pixel (p(I,j)) in the test image, obtain the hue data and find the bin position of the hue (hij, si, ) in the histogram.
②Query the corresponding bin in the model histogram - - and read the value of the bin.
③ Store this value in the new image (BackProjection). You can also normalize the model histogram first, so that the output of the test image can be displayed on the screen.
④By using the above steps for each pixel in the test image, we get the following BackProjection result map:
insert image description here
⑤Using the language of statistics, the value stored in BackProjection represents the probability that the pixel in the test image belongs to the skin area. Take the above picture as an example, the brighter area has a higher probability of being a skin area (it is true), while the darker area indicates a lower probability (note that the shadows inside and around the palm affect the detection accuracy).

The role of back projection:
The back projection application finds the point or area that best matches the specific image (template image) in the input image (large), that is, the
location where the template image appears in the input image.

Calculate the back projection—cv2.calcBackProject()
insert image description here
⚫images: input array or array set, need to be the same depth and size
⚫nimages: input the number of arrays, that is, the number of images
⚫channels: channel index to be counted
⚫hist: input Histogram
⚫backProject: target backprojection array, which needs to be a single channel, and has the same size and depth as image[0]
⚫ranges: indicates the boundary array of each dimension of each dimension array (value range)
⚫uniform: Whether the histogram is uniformly marked, the default value is true

insert image description here

Guess you like

Origin blog.csdn.net/weixin_40911806/article/details/130052576