当前位置:网站首页>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
边栏推荐
- Swing transformer details-2
- LeetCode - 508. 出现次数最多的子树元素和 (二叉树的遍历)
- 当你需要使用STM32某些功能,而51实现不了时, 那32自然不需要学
- ADS simulation design of class AB RF power amplifier
- Application of external interrupts
- 03 FastJson 解决循环引用
- Basic knowledge of MySQL database (an introduction to systematization)
- Gpiof6, 7, 8 configuration
- Interruption system of 51 single chip microcomputer
- The third paper of information system project manager in soft examination
猜你喜欢

Stm32f407 key interrupt

Serial communication based on 51 single chip microcomputer

Happy Dragon Boat Festival—— Zongzi written by canvas~~~~~

內存數據庫究竟是如何發揮內存優勢的?

Development of intelligent charging pile (I): overview of the overall design of the system

2.Elment Ui 日期选择器 格式化问题

2021-10-28

Leetcode 300 最长上升子序列

要選擇那種語言為單片機編寫程序呢

YOLO_ V1 summary
随机推荐
Runtime. getRuntime(). GC () and runtime getRuntime(). The difference between runfinalization()
2021-11-11 standard thread library
Basic knowledge of MySQL database (an introduction to systematization)
There is no specific definition of embedded system
For new students, if you have no contact with single-chip microcomputer, it is recommended to get started with 51 single-chip microcomputer
01仿B站项目业务架构
My openwrt learning notes (V): choice of openwrt development hardware platform - mt7688
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
RESNET code details
LeetCode - 508. 出现次数最多的子树元素和 (二叉树的遍历)
Blue Bridge Cup for migrant workers majoring in electronic information engineering
Oracle database SQL statement execution plan, statement tracking and optimization instance
新系列单片机还延续了STM32产品家族的低电压和节能两大优势
Toolbutton property settings
(2) New methods in the interface
Seven sorting of ten thousand words by hand (code + dynamic diagram demonstration)
2020-08-23
My 4G smart charging pile gateway design and development related articles
[combinatorics] combinatorial existence theorem (three combinatorial existence theorems | finite poset decomposition theorem | Ramsey theorem | existence theorem of different representative systems |
Yocto Technology Sharing Phase 4: Custom add package support