美文网首页
模板匹配

模板匹配

作者: zjh3029 | 来源:发表于2017-10-29 20:20 被阅读0次
#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;
}

相关文章

网友评论

      本文标题:模板匹配

      本文链接:https://www.haomeiwen.com/subject/hdiypxtx.html