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; }
相关文章推荐
- 要点初见:OpenCV3中基于模板匹配的目标跟踪思路
- opencv如何用模板匹配寻找目标
- OpenCV3模板匹配实现目标识别与跟踪
- 尺度不变的模板匹配方法(opencv源码)
- 使用Opencv中matchTemplate模板匹配方法跟踪移动目标
- opencv学习-模板匹配算法(单图像模板匹配和基于模板匹配的目标跟踪)
- OpenCV中文网站例程——单目标模板匹配
- [转]opencv如何用模板匹配寻找目标
- opencv 模板匹配目标检测
- opencv——实现目标模板匹配
- 目标跟踪之——多尺度模板匹配
- 使用Opencv中matchTemplate模板匹配方法跟踪移动目标
- opencv如何用模板匹配寻找目标
- 多目标模板匹配
- opencv中的模板匹配
- Python OpenCV学习笔记之:图像模板匹配
- VS2010+Opencv2.3.1,例程实现 笔记之模板匹配
- 尝试用OpenCV的模板匹配来定位和检测
- python-opencv 直方图目标区域匹配