当前位置:网站首页>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
边栏推荐
- SCM is now overwhelming, a wide variety, so that developers are overwhelmed
- Gif image analysis drawing RGB to YUV table lookup method to reduce CPU occupancy
- Google browser plug-in recommendation
- 2020-08-23
- Adaptiveavgpool1d internal implementation
- Embedded systems are inherently flawed. Compared with the Internet, there are so many holes that it is simply difficult to walk away from
- Blue Bridge Cup for migrant workers majoring in electronic information engineering
- 2.Elment Ui 日期选择器 格式化问题
- Basic knowledge of MySQL database (an introduction to systematization)
- JS foundation - prototype prototype chain and macro task / micro task / event mechanism
猜你喜欢
03 fastjason solves circular references
Yocto Technology Sharing Phase 4: Custom add package support
新系列单片机还延续了STM32产品家族的低电压和节能两大优势
My notes on the development of intelligent charging pile (III): overview of the overall design of the system software
LeetCode - 508. 出现次数最多的子树元素和 (二叉树的遍历)
要選擇那種語言為單片機編寫程序呢
Getting started with JMX, MBean, mxbean, mbeanserver
Windows下MySQL的安装和删除
Retinaface: single stage dense face localization in the wild
单片机现在可谓是铺天盖地,种类繁多,让开发者们应接不暇
随机推荐
01 business structure of imitation station B project
单片机现在可谓是铺天盖地,种类繁多,让开发者们应接不暇
LeetCode - 460 LFU 缓存(设计 - 哈希表+双向链表 哈希表+平衡二叉树(TreeSet))*
Problems encountered when MySQL saves CSV files
It is difficult to quantify the extent to which a single-chip computer can find a job
01仿B站项目业务架构
Installation and removal of MySQL under Windows
单片机职业发展:能做下去的都成牛人了,熬不动就辞职或者改行了
Openeuler kernel technology sharing - Issue 1 - kdump basic principle, use and case introduction
When you need to use some functions of STM32, but 51 can't realize them, 32 naturally doesn't need to learn
STM32 running lantern experiment - library function version
(1) 什么是Lambda表达式
03 fastjason solves circular references
JS foundation - prototype prototype chain and macro task / micro task / event mechanism
Comment la base de données mémoire joue - t - elle l'avantage de la mémoire?
Windows下MySQL的安装和删除
Swing transformer details-2
Google browser plug-in recommendation
LeetCode 面试题 17.20. 连续中值(大顶堆+小顶堆)
uniapp 实现微信小程序全局分享及自定义分享按钮样式