OpenCV matchTemplate template matching principle and code implementation (C#)

  • Basic principle:
    Use the template image as the convolution kernel, scan the sample image from left to right, and top to bottom, calculate the correlation coefficient, and then find objects similar to the template.
    Insert image description here
  • matching method

1. Squared difference matching method (TM_SQDIFF) T is the template image, I is the matching image; at the (x, y) position of the matching image, x' to the right and y' downward frame the recognition area, and when
Insert image description here
the correlation is completely matched,
The calculated value is 0; the larger the matching value, the lower the correlation.

2. Normalized squared difference identification method (TM_SQDIFF_NORMED)

Insert image description here
The squared difference calculation is normalized so that the calculation result is 0~1.
When the image completely matches, the calculation result is 0; when the image does not match completely, the calculation result is 1.

3. Correlation matching method (TM_CCORR)

Insert image description here
The template is multiplied by the matching image. The larger the calculation result, the better the matching result;
this method will cause the calculation result to change due to changes in the global brightness of the image. For example, when the image becomes brighter, the gray value of each pixel will increase, which will lead to The calculation result becomes larger;

4. Normalized correlation matching method (TM_CCORR_NORMED)
Insert image description here
5. Correlation coefficient matching method (TM_CCOEFF)
Insert image description here
This method preprocesses the matrix of the template and the recognition area before performing correlation matching: subtracting the matrix mean from the matrix, this This method solves the impact caused by the brightness difference between the template image and the recognition image. It can be seen from the calculation result logic that the larger the calculation result, the better the matching result.

6. Normalized correlation coefficient matching method (TM_CCOEFF_NORMED)

Insert image description here

This method normalizes the correlation coefficient matching method. The calculation result is -1~1
for a complete match and -1 for a complete mismatch.

  • API
public static void MatchTemplate(InputArray image, InputArray templ, OutputArray result, TemplateMatchModes method, InputArray? mask = null);

image: recognition image
templ: template image
result: template matching result
method: matching method
mask: matching template mask, generally using the default value

  • Demo
Mat templatePic = Cv2.ImRead(@"C:\Users\Aron\Desktop\01\A.jpg");
Mat testPic = Cv2.ImRead(@"C:\Users\Aron\Desktop\01\B.jpg");


Mat matchResul = new Mat();


//归一化相关系数匹配法
Cv2.MatchTemplate(testPic, templatePic, matchResul, TemplateMatchModes.CCoeffNormed);

//最大最小匹配对象的位置
Point minLoc = new Point(0, 0);
Point maxLoc = new Point(0, 0);

//最大最小配置值
double minVal;
double maxVal;

//查找最大最小值的位置及匹配的值
Cv2.MinMaxLoc(matchResul, out minVal, out maxVal, out minLoc, out maxLoc);

//框选出识别对象
//归一化相关系数匹配方法取最大值
Scalar scalar = new Scalar(0, 0, 255);
Cv2.Rectangle(testPic,maxLoc, new Point(maxLoc.X+templatePic.Cols,maxLoc.Y+templatePic.Rows), scalar, 7);

//显示相关性计算结果
Cv2.PutText(testPic, Math.Round(maxVal,4).ToString(), new Point(maxLoc.X, maxLoc.Y -50), HersheyFonts.HersheyDuplex, 2, scalar, 8);


picBox_Display.Image = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(templatePic);
picBox_After.Image = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(testPic);

Insert image description here
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/127464728