opencv--模板匹配


1
#include "stdafx.h" 2 #include <windows.h> 3 #include <baseapi.h> 4 #include <opencv2opencv.hpp> 5 6 using namespace cv; 7 using namespace std; 8 #pragma comment(lib,"libtesseract302d.lib") 9 10 int main() 11 { 12 IplImage *src = cvLoadImage("C:\Users\Bite07\Desktop\1.jpg", 0); 13 IplImage *srcResult = cvLoadImage("C:\Users\Bite07\Desktop\1.jpg", 3); //用来显示 14 IplImage *templat = cvLoadImage("C:\Users\Bite07\Desktop\4.jpg", 0); 15 IplImage *result; // 用来存放结果 16 17 if(!src || !templat) 18 { 19 cout << "打开图像失败"<< endl; 20 return 0; 21 } 22 int srcW, srcH, templatW, templatH, resultH, resultW; 23 srcW = src->width; 24 srcH = src->height; 25 templatW = templat->width; 26 templatH = templat->height; 27 if(srcW < templatW || srcH < templatH) 28 { 29 cout <<"模板不能比原图像大" << endl; 30 return 0; 31 } 32 resultW = srcW - templatW + 1; 33 resultH = srcH - templatH + 1; 34 result = cvCreateImage(cvSize(resultW, resultH), 32, 1); 35 36 cvMatchTemplate(src, templat, result, CV_TM_SQDIFF); 37 double minValue, maxValue; 38 CvPoint minLoc, maxLoc; 39 40 cvMinMaxLoc(result, &minValue, &maxValue, &minLoc, &maxLoc); 41 cvRectangle(srcResult, minLoc, cvPoint(minLoc.x + templatW, minLoc.y+ templatH), cvScalar(0,0,255)); 42 43 cvNamedWindow("srcResult", 0); 44 cvNamedWindow("templat", 0); 45 cvShowImage("srcResult", srcResult); 46 cvShowImage("templat", templat); 47 cvWaitKey(0); 48 cvReleaseImage(&result); 49 cvReleaseImage(&templat); 50 cvReleaseImage(&srcResult); 51 cvReleaseImage(&src); 52 return 0; 53 }

参考至:http://blog.csdn.net/lu597203933/article/details/14548523

原文地址:https://www.cnblogs.com/bent/p/4152757.html