欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

模板匹配

程序员文章站 2022-04-01 09:29:33
...
#include<opencv2/opencv.hpp>
#include<iostream>
#include<math.h>
using namespace std;
using namespace cv;
int main()
{
	Mat src1 = imread("D:\\test1.bmp", IMREAD_GRAYSCALE);
	Mat src2 = imread("D:\\test2.bmp", IMREAD_GRAYSCALE);
	if (src1.empty()|| src2.empty())
	{
		cout << " image read error" << endl;
		return -1;
	}
	//去除原图黑边得到64*64图像
	Mat img1 = src1(Range(130, 194), Range(320, 384));
	Mat img2 = src2(Range(130, 194), Range(320, 384));
	int rows1 = img1.rows;
	int cols1 = img1.cols;
	int rows2 = img2.rows;
	int cols2 = img2.cols;
	//取第一幅图片的中间区域templ作为模板,大小为41*41
	Mat templ = img1(Range(rows1 / 2 - 20, rows1 / 2 + 21), Range(cols1 / 2 - 20, cols1 / 2 + 21));
	int result_cols = img2.cols - templ.cols + 1;
	int result_rows = img2.rows - templ.rows + 1;
	Mat result(result_rows, result_cols, CV_32FC1); //result    输出结果,必须是单通道32位浮点数
	//进行模板匹配
	matchTemplate(img2, templ, result, 3);//result    输出结果,必须是单通道32位浮点数
	Point minLoc;
	Point maxLoc;
	double min, max;
	minMaxLoc(result, &min, &max, &minLoc, &maxLoc);
	cout << "max="<<max << endl;
	cout << "maxLoc="<<maxLoc << endl;
	system("pause");
	return 0;
}