#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;
}
网友评论