您的位置:首页 > 运维架构

opencv 单目标模板匹配(只适用于模板与目标尺度相同)

2016-11-29 14:42 489 查看
#include <iostream>
#include "opencv/cv.h"
#include "opencv/cxcore.h"
#include "opencv/highgui.h"
using namespace std;

#pragma comment ( lib,"opencv_highgui244.lib" )
#pragma comment ( lib,"opencv_core244.lib" )
#pragma comment ( lib,"opencv_imgproc244.lib" )

int main()
{
IplImage *src = cvLoadImage("src.jpg", 0);
IplImage *srcResult = cvLoadImage("src.jpg", 3);  //用来显示
IplImage *templat = cvLoadImage("template1.png", 0);
IplImage *result;
if(!src || !templat)
{
cout << "打开图像失败"<< endl;
return 0;
}
int srcW, srcH, templatW, templatH, resultH, resultW;
srcW = src->width;
srcH = src->height;
templatW = templat->width;
templatH = templat->height;
if(srcW < templatW || srcH < templatH)
{
cout <<"原图不能比模板小" << endl;
return 0;
}
resultW = srcW - templatW + 1;
resultH = srcH - templatH + 1;
result = cvCreateImage(cvSize(resultW, resultH), 32, 1);

//the 3rd parameter
cvMatchTemplate(src, templat, result, CV_TM_SQDIFF);
double minValue, maxValue;
CvPoint minLoc, maxLoc;
cvMinMaxLoc(result, &minValue, &maxValue, &minLoc, &maxLoc);
cvRectangle(srcResult, minLoc, cvPoint(minLoc.x + templatW, minLoc.y+ templatH), cvScalar(0,0,255));
cvNamedWindow("srcResult", 0);
cvNamedWindow("templat", 0);
cvShowImage("srcResult", srcResult);
cvShowImage("templat", templat);
cvWaitKey(0);
cvReleaseImage(&result);
cvReleaseImage(&templat);
cvReleaseImage(&srcResult);
cvReleaseImage(&src);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: