您的当前位置:首页正文

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

来源:帮我找美食网
opencv单⽬标模板匹配(只适⽤于模板与⽬标尺度相同)

#include #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\

IplImage *srcResult = cvLoadImage(\"src.jpg\⽤来显⽰ IplImage *templat = cvLoadImage(\"template1.png\ 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\ cvNamedWindow(\"templat\

cvShowImage(\"srcResult\ cvShowImage(\"templat\ cvWaitKey(0);

cvReleaseImage(&result); cvReleaseImage(&templat); cvReleaseImage(&srcResult); cvReleaseImage(&src); return 0; }

因篇幅问题不能全部显示,请点此查看更多更全内容

Top