opencv 模板匹配, 已解决模板过大程序不工作的bug

#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <math.h>

#ifdef _DEBUG
#pragma comment ( lib,"opencv_highgui244d.lib" )
#pragma comment ( lib,"opencv_core244d.lib" )
#pragma comment ( lib,"opencv_imgproc244d.lib" )
#else
#pragma comment ( lib,"opencv_highgui244.lib" )
#pragma comment ( lib,"opencv_core244.lib" )
#pragma comment ( lib,"opencv_imgproc244.lib" )
#endif

using namespace std;
using namespace cv;


//get the index of the min value
int app(vector<double> minV)           
{
int t=0;
for (int i = 1; i < minV.size();i++)
{
if (minV[i] < minV[t]) t = i;
}


return t;
}

int main( )
{
Mat img = imread("src1.jpg");       //原图像
Mat temp = imread("template1.png");     //模板
Mat result;

vector<double> minV;
vector<Point> minL;


vector<Mat> down_temp;
down_temp.push_back(temp);

//匹配4次,每次模板缩小1/1.3
for (int i=0;i<4;i++)
{
	long begin = GetTickCount();
	Mat temp1;

	cout<<down_temp[i].cols<<endl;
	cout<<down_temp[i].rows<<endl;

	int result_cols =  img.cols - down_temp[i].cols + 1;
	int result_rows = img.rows - down_temp[i].rows + 1;
	result.create( result_cols, result_rows, CV_32FC1 );
	matchTemplate( img, down_temp[i], result, CV_TM_SQDIFF );

	double minVal; 
	double maxVal; 
	Point  minLoc; 
	Point  maxLoc;
	Point  matchLoc;

	minMaxLoc( result, &minVal, &maxVal, &minLoc, &maxLoc);

	minVal=minVal/(down_temp[i].cols*down_temp[i].rows);
	cout<<minVal<<endl;
	minV.push_back(minVal);
	minL.push_back(minLoc);
	resize( down_temp[i], temp1, Size( down_temp[i].cols/1.3, down_temp[i].rows/1.3) );
	down_temp.push_back(temp1);

	cout<< " calculate time : " << GetTickCount()-begin<<endl;
}


int location;

location = app(minV); 

rectangle( img, minL[location], Point( minL[location].x + down_temp[location].cols , minL[location].y + down_temp[location].rows ), Scalar::all(0), 2, 8, 0 );

imshow("结果",img);

waitKey();


return 0;
}


原文地址:https://www.cnblogs.com/luoyinjie/p/7219330.html