首页 > 代码库 > opencv--模板匹配
opencv--模板匹配
1 #include "stdafx.h" 2 #include <windows.h> 3 #include <baseapi.h> 4 #include <opencv2\opencv.hpp> 5 6 using namespace cv; 7 using namespace std; 8 #pragma comment(lib,"libtesseract302d.lib") 9 10 int main() 11 { 12 IplImage *src = http://www.mamicode.com/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
opencv--模板匹配
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。