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

来源:互联网 发布:node 编辑:程序博客网 时间:2024/04/30 14:29
#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" )#endifusing namespace std;using namespace cv;//get the index of the min valueint 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.3for (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;}


0 0
原创粉丝点击