Comparing the similarity principle of two images through histogram and OpenCV implementation (C#)

principle

H 1 H_1 H1 H 2 H_2 H2array of histograms computed for the two images, H ˉ \bar{H}Hˉ is the array mean

Correlation comparison
Insert image description here
It can be seen from the formula that the closer the correlation comparison calculation result is to 1, the more similar the two images are.

Histogram intersection method
Insert image description here
takes the minimum values ​​at the same position in the array and sums them. This method does not normalize the calculation results.
For example, the template image is A and the comparison images are B and C; the larger the calculation results of B and C, the more similar they are to A.

Chi-square calculation
Insert image description here
It can be seen from the formula that the closer the chi-square calculation result is to 0, the more similar the two images are.

Bhattacharyya distance calculation
Insert image description here
: According to the formula, the closer the calculation result is to 0, the more similar the two images are.

API

public static double CompareHist(InputArray h1, InputArray h2, HistCompMethods method);

Parameters:
InputArray h1: Image 1 histogram
InputArray h2: Image 2 histogram
HistCompMethods method: Histogram comparison method
Return value: Calculation result

Case presentation

Insert image description here

Mat mat_A = Cv2.ImRead(@"C:\Users\Aron\Desktop\01\A.jpg", ImreadModes.AnyColor);
Mat mat_B = Cv2.ImRead(@"C:\Users\Aron\Desktop\01\B.jpg", ImreadModes.AnyColor);

Mat[] mat_As;
Mat[] mat_Bs;
Cv2.Split(mat_A, out mat_As);//拆分通道              
Cv2.Split(mat_B, out mat_Bs);//拆分通道

int[] histSize = {
    
     256 };//直方图数组大小
Rangef[] histRange = {
    
     new Rangef(0, 256) }; //直方图的像素范围    

//直方图输出数组
Mat hist_A = new Mat();
Mat hist_B = new Mat();

bool uniform = true, accumulate = false;
Cv2.CalcHist(mat_As, new int[] {
    
     0,1,2 }, null, hist_A, 1, histSize, histRange, uniform, accumulate);
Cv2.CalcHist(mat_Bs, new int[] {
    
     0,1,2 }, null, hist_B, 1, histSize, histRange, uniform, accumulate);

//归一化,排除图像分辨率不一致的影响
Cv2.Normalize(hist_A, hist_A, 0, 1, NormTypes.MinMax, -1, null);
Cv2.Normalize(hist_B, hist_B, 0, 1, NormTypes.MinMax, -1, null);

var result = Cv2.CompareHist(hist_A, hist_B, HistCompMethods.Correl);//相关性比较

//绘制文字
Point p1 = new Point(50, 80);
Scalar scalar = new Scalar(0, 0, 255);
Cv2.PutText(mat_B, "correlation:" + Math.Round(result,6).ToString(), p1, HersheyFonts.HersheyDuplex, 2, scalar, 4);

picBox_Display.Image = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(mat_A);
picBox_After.Image = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(mat_B);

Insert image description here
Insert image description here
Insert image description here
As shown in the test results above, using the correlation comparison method, the calculated value of similar images is close to 1, and the calculated value of dissimilar images is close to 0.

It should be noted that when the histogram compares the similarity of images, the spatial position of the pixel is not considered in the calculation. As shown in the figure below, when the image is rotated, the calculation result remains unchanged; similarly, in some extreme cases, there may be two The image histograms are completely consistent, but the two images are completely dissimilar. Therefore, the histogram comparison of image similarity is only suitable for image comparison where the environment and characteristic object types are fixed.

Insert image description here

Additional notes:
The OpenCV library used in .NET in this case is OpenCvSharp4

OpenCv library for .NET environment

Guess you like

Origin blog.csdn.net/weixin_40671962/article/details/127416506
Recommended