当前位置:网站首页>Opencv notes 17 template matching
Opencv notes 17 template matching
2022-07-03 10:03:00 【Σίσυφος one thousand and nine hundred】
One 、 Template matching
Template matching (TemplateMatching) Is to find and template images in an image (template) The most similar area , This method has simple principle and fast calculation speed , It can be applied to target recognition , Target tracking and other fields .
Two 、 principle
1、cv::TM_SQDIFF: This method uses square difference for matching , Therefore, the best matching result is 0 It's about , The larger the value, the worse the matching result .
2、cv::TM_SQDIFF_NORMED: The method uses the normalized square difference for matching , The best match is also when the result is 0 It's about .
3、cv::TM_CCORR: Correlation matching method , This method uses the convolution results of source image and template image to match , therefore , The best match position is at the maximum value , The smaller the value, the worse the matching result .
4、cv::TM_CCORR_NORMED: Normalized correlation matching method , Similar to the correlation matching method , The best match position is also at the maximum value .
5、cv::TM_CCOEFF: Correlation coefficient matching method , This method uses the difference between the source image and its mean 、 The correlation between the difference between the template and its mean is matched , The best match result is when the value is equal to 1 It's about , The worst match results in a value equal to -1 It's about , The value is equal to 0 Direct means that the two are not related .
6、cv::TM_CCOEFF_NORMED: Normalized correlation coefficient matching method , A positive value indicates a good match , A negative value indicates a poor match , Also, the greater the value , The matching effect is good .
3、 ... and 、API
MatchTemplate(
InputArrray image, // The source image
InputArray templ, // Template image
OutputArray result,// Output results It has to be a single channel 32 Bit floating point , Suppose the source image WxH, Template image w*h, The result must be W-w+1,H-h+1 Size
int method // Match method , The meaning of the results produced by different methods may be different , The larger the returned value, the better the matching degree , And the smaller the value returned by some methods, the better the matching degree
About parameters method:
CV_TM_SQDIFF Square error matching : This method uses square difference to match ; The best match value is 0; Match the worse , The larger the match value .
CV_TM_CCORR Correlation matching : The method adopts multiplication operation ; The larger the number, the better the match .
CV_TM_CCOEFF Correlation coefficient matching method :1 A perfect match ;-1 Represents the worst match .
CV_TM_SQDIFF_NORMED Normalized square error matching method
CV_TM_CCORR_NORMED Normalized correlation matching method
CV_TM_CCOEFF_NORMED Normalized correlation coefficient matching method
);
Four 、 Code shows

#if 1 // Template matching There are six ways
Mat src, tempalte, result;
int matchMethod;
int maxTrackbarType = 5;
void on_matching(int, void*)
{
Mat srcImage;
src.copyTo(srcImage);
int result_cols = src.cols - tempalte.cols + 1;
int result_rows = src.rows - tempalte.rows + 1;
cout << "---- result_rows----: " << result_rows << endl;
cout << "---- result_cols----: " << result_cols << endl;
result.create(result_cols, result_rows, CV_32FC1);
// Begin to match
matchTemplate(src, tempalte, result, matchMethod);
normalize(result, result, 0, 2, NORM_MINMAX, -1, Mat());
double minValue, maxValue;
Point minLocation, maxLocation, matchLocation;
minMaxLoc(result, &minValue, &maxValue, &minLocation, &maxLocation);
if (matchMethod == TM_SQDIFF || matchMethod == CV_TM_SQDIFF_NORMED)
{
matchLocation = minLocation;
}
else
{
matchLocation = maxLocation;
}
rectangle(srcImage, matchLocation, Point(matchLocation.x + tempalte.cols, matchLocation.y + tempalte.rows), Scalar(0, 0, 255), 2, 8, 0);
rectangle(result, matchLocation, Point(matchLocation.x + tempalte.cols, matchLocation.y + tempalte.rows), Scalar(0, 0, 255), 2, 8, 0);
imshow(" The original picture ", srcImage);
imshow(" design sketch ", result);
}
int main()
{
src = imread("C:\\Users\\19473\\Desktop\\opencv_images\\609.png");
if (!src.data)
{
cout << " Failed to read the original graph " << endl;
return -1;
}
cout << " Original target image " << src.rows << " , " << src.cols << endl;
tempalte = imread("C:\\Users\\19473\\Desktop\\opencv_images\\610.png");
if (!tempalte.data)
{
cout << " Template map reading failed " << endl;
return -1;
}
cout << " Target target image " << tempalte.rows << " , " << tempalte.cols << endl;
imshow(" Templates ", tempalte);
namedWindow(" The original picture ", CV_WINDOW_AUTOSIZE);
namedWindow(" design sketch ", CV_WINDOW_AUTOSIZE);
createTrackbar(" Method ", " The original picture ", &matchMethod, maxTrackbarType, on_matching);
on_matching(0, NULL);
waitKey(0);
return 0;
}
#endif
边栏推荐
- 对于新入行的同学,如果你完全没有接触单片机,建议51单片机入门
- CEF download, compile project
- 2312、卖木头块 | 面试官与狂徒张三的那些事(leetcode,附思维导图 + 全部解法)
- Fundamentals of Electronic Technology (III)_ Chapter 2 principle of amplification circuit__ Crystal triode and field effect triode
- 手机都算是单片机的一种,只不过它用的硬件不是51的芯片
- Idea remote breakpoint debugging jar package project
- Not many people can finally bring their interests to college graduation
- 4G module IMEI of charging pile design
- MySQL root user needs sudo login
- 4G module board level control interface designed by charging pile
猜你喜欢

Timer and counter of 51 single chip microcomputer

LeetCode - 919. 完全二叉树插入器 (数组)

STM32 interrupt switch

Crash工具基本使用及实战分享

Leetcode 300 最长上升子序列

Comment la base de données mémoire joue - t - elle l'avantage de la mémoire?

When you need to use some functions of STM32, but 51 can't realize them, 32 naturally doesn't need to learn

要选择那种语言为单片机编写程序呢

单片机职业发展:能做下去的都成牛人了,熬不动就辞职或者改行了

My notes on intelligent charging pile development (II): overview of system hardware circuit design
随机推荐
Simulate mouse click
(1) 什么是Lambda表达式
没有多少人能够最终把自己的兴趣带到大学毕业上
Happy Dragon Boat Festival—— Zongzi written by canvas~~~~~
4G module at command communication package interface designed by charging pile
The 4G module designed by the charging pile obtains NTP time through mqtt based on 4G network
单片机学到什么程度能找到工作,这个标准不好量化
2021-10-28
MySQL root user needs sudo login
Open Euler Kernel Technology Sharing - Issue 1 - kdump Basic Principles, use and Case Introduction
Which language should I choose to program for single chip microcomputer
在三线城市、在县城,很难毕业就拿到10K
应用最广泛的8位单片机当然也是初学者们最容易上手学习的单片机
Sending and interrupt receiving of STM32 serial port
Synchronization control between tasks
My 4G smart charging pile gateway design and development related articles
QT is a method of batch modifying the style of a certain type of control after naming the control
el-table X轴方向(横向)滚动条默认滑到右边
yocto 技术分享第四期:自定义增加软件包支持
CEF download, compile project