当前位置:网站首页>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
边栏推荐
- 内存数据库究竟是如何发挥内存优势的?
- My 4G smart charging pile gateway design and development related articles
- Working mode of 80C51 Serial Port
- STM32 general timer output PWM control steering gear
- Serial port programming
- You need to use MySQL in the opening experiment. How can you forget the basic select statement? Remedy is coming~
- ADS simulation design of class AB RF power amplifier
- Application of external interrupts
- Stm32f407 key interrupt
- 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
猜你喜欢

嵌入式系统没有特别明确的定义

In third tier cities and counties, it is difficult to get 10K after graduation

应用最广泛的8位单片机当然也是初学者们最容易上手学习的单片机

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

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

内存数据库究竟是如何发挥内存优势的?

干单片机这一行的时候根本没想过这么多,只想着先挣钱养活自己

Mobile phones are a kind of MCU, but the hardware it uses is not 51 chip

嵌入式本来就很坑,相对于互联网来说那个坑多得简直是难走

JS foundation - prototype prototype chain and macro task / micro task / event mechanism
随机推荐
Which language should I choose to program for single chip microcomputer
Retinaface: single stage dense face localization in the wild
2020-08-23
Stm32 NVIC interrupt priority management
My 4G smart charging pile gateway design and development related articles
The third paper of information system project manager in soft examination
It is difficult to quantify the extent to which a single-chip computer can find a job
YOLO_ V1 summary
Basic knowledge of communication interface
I didn't think so much when I was in the field of single chip microcomputer. I just wanted to earn money to support myself first
Gif image analysis drawing RGB to YUV table lookup method to reduce CPU occupancy
Swing transformer details-1
对于新入行的同学,如果你完全没有接触单片机,建议51单片机入门
Getting started with JMX, MBean, mxbean, mbeanserver
Wireshark use
Serial communication based on 51 single chip microcomputer
Synchronization control between tasks
[untitled] proteus simulation of traffic lights based on 89C51 Single Chip Microcomputer
The data read by pandas is saved to the MySQL database
yocto 技術分享第四期:自定義增加軟件包支持