当前位置:网站首页>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 career development: those who can continue to do it have become great people. If they can't endure it, they will resign or change their careers
- 使用密钥对的形式连接阿里云服务器
- 2021-10-28
- Fundamentals of Electronic Technology (III)__ Chapter 6 combinational logic circuit
- LeetCode - 508. 出现次数最多的子树元素和 (二叉树的遍历)
- Stm32 NVIC interrupt priority management
- uniapp 实现微信小程序全局分享及自定义分享按钮样式
- Mobile phones are a kind of MCU, but the hardware it uses is not 51 chip
- (2)接口中新增的方法
- STM32 general timer output PWM control steering gear
猜你喜欢
uniapp 实现微信小程序全局分享及自定义分享按钮样式
03 fastjason solves circular references
Openeuler kernel technology sharing - Issue 1 - kdump basic principle, use and case introduction
Pymssql controls SQL for Chinese queries
嵌入式系统没有特别明确的定义
[untitled] proteus simulation of traffic lights based on 89C51 Single Chip Microcomputer
Interruption system of 51 single chip microcomputer
Open Euler Kernel Technology Sharing - Issue 1 - kdump Basic Principles, use and Case Introduction
要选择那种语言为单片机编写程序呢
我想各位朋友都应该知道学习的基本规律就是:从易到难
随机推荐
Of course, the most widely used 8-bit single chip microcomputer is also the single chip microcomputer that beginners are most easy to learn
Vscode markdown export PDF error
Simulate mouse click
内存数据库究竟是如何发挥内存优势的?
Stm32 NVIC interrupt priority management
El table X-axis direction (horizontal) scroll bar slides to the right by default
QT is a method of batch modifying the style of a certain type of control after naming the control
Not many people can finally bring their interests to college graduation
4G module at command communication package interface designed by charging pile
My notes on intelligent charging pile development (II): overview of system hardware circuit design
openEuler kernel 技术分享 - 第1期 - kdump 基本原理、使用及案例介绍
There is no specific definition of embedded system
4G module initialization of charge point design
RESNET code details
The third paper of information system project manager in soft examination
Circular queue related design and implementation reference 1
Comment la base de données mémoire joue - t - elle l'avantage de la mémoire?
Positive and negative sample division and architecture understanding in image classification and target detection
01 business structure of imitation station B project
[combinatorics] combinatorial existence theorem (three combinatorial existence theorems | finite poset decomposition theorem | Ramsey theorem | existence theorem of different representative systems |