#include <opencv2\opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
int main()
{
VideoCapture cap(0);
if (!cap.isOpened())
return -1;
Mat matcher = imread("1.jpg");
double minVal = -1;
double maxVal;
Point minLoc, maxLoc, matchLoc;
for (Mat img;waitKey(1)!=27;)
{
cap >> img;
int result_cols = img.cols - matcher.cols+1;
int result_rows = img.rows - matcher.rows+1;
Mat result = Mat(result_cols, result_rows, CV_32FC1);
matchTemplate(img, matcher, result, CV_TM_SQDIFF_NORMED);
normalize(result, result, 0, 1, NORM_MINMAX, -1, Mat());
cout << "匹配度:" << minVal << endl;
minMaxLoc(result, &minVal, &maxVal, &minLoc, &maxLoc, Mat());
cout << "匹配度:" << minVal << endl;
matchLoc = minLoc;
rectangle(img, Rect(matchLoc, Size(matcher.cols, matcher.rows)), Scalar(0, 0, 255),3,8);
imshow("img", img);
}
return 0;
}
模板匹配
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- 理论 模板匹配是在一个大图里搜索和找模板图像位置的方法。OpenCV有个函数cv2.matchTemplate()...