Learning OpenCV template matching

Concepts and principles template matching will not say, "OpenCV3 Programming Getting Started" section 9.5 of this book presentations, but relatively simple, as Kan Bukan have to understand it NBCS hee hee, after all, I also see a smattering of knowledge. So this time it is mainly devoted to the book's sample programs 9.5.3, and several parts of the interpreted code, because I am also a beginner, it all depends on personal understanding, so to talk about is not necessarily very deep, we do not push too tight boots boots!

The sample program

Originally posted that code is not necessary, but afraid that some people are too lazy chatter not want to read, or books are not pleasant child to force too tight, or faith to do good, to talk about things with good intentions of Miyoshi spirit, posted below the entire bar code. As I knock myself, so God and the order book is not the same horse, but overall there is no any problem boots boots!

#include<opencv2/highgui.hpp>
#include<opencv2/imgproc.hpp>
using namespace cv;

Mat g_srcImage, g_templateImage, g_resultImage;
int g_nMatchMethod;
int g_nMaxTrackbarNum = 5;

void on_Matching(int, void*)//回调函数
{
	Mat srcImage;
	g_srcImage.copyTo(srcImage);
	//初始化用于结果输出的矩阵
	int resultImage_rows = g_srcImage.rows - g_templateImage.rows + 1;
	int resultImage_cols = g_srcImage.cols - g_templateImage.cols + 1;
	g_resultImage.create(resultImage_rows, resultImage_cols, CV_32FC1);
	//进行匹配和标准化
	matchTemplate(g_srcImage, g_templateImage, g_resultImage, g_nMatchMethod);
	normalize(g_resultImage, g_resultImage, 0, 1, NORM_MINMAX, -1, Mat());//归一化数据
	//通过函数minMaxLoc定位最匹配的位置
	double minValue, maxValue;
	Point minLocation, maxLocation, matchLocation;
	minMaxLoc(g_resultImage, &minValue, &maxValue, &minLocation, &maxLocation, Mat());
	//使用方法SQDIFF和SQDIFF_NORMED,越小的数值有着更高的匹配结果,而其余的方法,数值越大匹配效果越好
	if (g_nMatchMethod == TM_SQDIFF || g_nMatchMethod == CV_TM_SQDIFF_NORMED)
		matchLocation = minLocation;
	else
		matchLocation = maxLocation;
	//绘制出矩形,并显示最终结果
	rectangle(srcImage, matchLocation, Point(matchLocation.x + g_templateImage.cols, matchLocation.y + g_templateImage.rows), Scalar(0, 0, 255), 2, 8, 0);
	rectangle(g_resultImage, matchLocation, Point(matchLocation.x + g_templateImage.cols, matchLocation.y + g_templateImage.rows), Scalar(0, 0, 255), 2, 8, 0);

	imshow("【原始图片】", srcImage);
	imshow("【效果窗口】", g_resultImage);
}

int main()
{
	g_srcImage = imread("linlin.jpg");
	g_templateImage = imread("linlinface.PNG");

	namedWindow("【原始图片】", CV_WINDOW_AUTOSIZE);
	namedWindow("【效果窗口】", CV_WINDOW_AUTOSIZE);

	createTrackbar("方法", "【原始图片】", &g_nMatchMethod, g_nMaxTrackbarNum, on_Matching);
	on_Matching(0, 0);

	waitKey(0);
	return 0;
}

Code explained

int resultImage_rows = g_srcImage.rows - g_templateImage.rows + 1;
int resultImage_cols = g_srcImage.cols - g_templateImage.cols + 1;

These two lines are mainly seeking Mat g_resultImage variable length and width, i.e. size, corresponding to the variable is matchTemplate () function are Result third parameter, the result of the comparison map image. As for why such a demand, the book must be mentioned (W-w + 1) x (H-h + 1), the principle is what the book did not mention vomit! Personal understanding, may be associated with the principle of template matching. Template matching is achieved by a template image, the moving image input block on the entire image to find the optimal matching similar. As for the effect of mapping it, in fact, in order to facilitate comparison, so to reduce the size to match the enlarged part, to facilitate comparison of different methods of matching results. Of course, I understand that everything boots boots, as is not the case I Qingzheziqing. Of course, welcome children's shoes to know in the comments Message thick square!

matchTemplate(g_srcImage, g_templateImage, g_resultImage, g_nMatchMethod);
normalize(g_resultImage, g_resultImage, 0, 1, NORM_MINMAX, -1, Mat());

The first line is to call matchTemplate () function, nothing to say, mainly normalize in the second row () function, an essay written in more detail, we can see: OpenCV function Introduction (1) - normalize by this the article is concerned, it should be the scope of normalized data, use image Watch can see the image pixel values g_resultImage after normalization in (0,1), fits normalize () function in 0,1 upper and lower limits, the effect is as follows:
Here Insert Picture Description

rectangle(srcImage, matchLocation, Point(matchLocation.x + g_templateImage.cols, matchLocation.y + g_templateImage.rows), Scalar(0, 0, 255), 2, 8, 0);
rectangle(g_resultImage, matchLocation, Point(matchLocation.x + g_templateImage.cols, matchLocation.y + g_templateImage.rows), Scalar(0, 0, 255), 2, 8, 0);

Then the code is to draw a rectangle, and the original image are plotted mapped, regions just in fact match. A brief argument rectangle () function, facilitate the use of thick!
The first parameter, the rectangle to be drawn image;
a second parameter, the rectangle of the upper-left coordinates, coordinate point is obtained matched positions;
third parameter, the lower right corner of the rectangular coordinates, the code is cross-left corner ordinate together with the length and width of the template image is obtained, easy to understand, because the rectangle as a template and to provoke;
the fourth parameter, while the color of thick rectangle, BGR boots boots the color of your own tune;
fifth parameter, thickness line wide;
sixth parameter, lineType line;
seventh parameter, int shift = 0 ??, I did not understand, should be related to the coordinates of the point, choose the default values like chatter.

g_templateImage = imread(“linlinface.PNG”);

In this book I did not explain vomit, really plain lazy dead chatter! Simply use the code might ignorant people forced thick, What is that map? In fact, a closer look of the book would still be able to understand, is the template image thick, that is, you want to match part, you cut a figure like the chatter from the original, so easy boots boots!

Experimental results

Other nothing to say, for everyone to see the effect of it! Selection of the universe is love prostitute days hee hee, the result is not for everyone to read maps, meaning all the same, the results are as follows:
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
you can see, only the second method (correlation matching method) is not the essence, without success to match days beauty, prime real hard, sooner or later be sucked. Other methods are successfully matched, the difference is not large, they are afraid of God poured Bale hee hee!

Published 17 original articles · won praise 3 · Views 1810

Guess you like

Origin blog.csdn.net/weixin_43350361/article/details/88774947