opencv学习_11 (模板匹配(包括单模板和多模板))

来源:互联网 发布:junos pulse mac 下载 编辑:程序博客网 时间:2024/05/20 01:09

模板匹配——在一幅图像中匹配与模板相似的单个或者多个目标

(1)目标匹配函数:

cvMatchTemplate( const CvArr* image, constCvArr* templ,

                              CvArr* result,int method );

Image  待搜索图像

Templ  模板图像

Result  匹配结果  用来存放通过以下方法计算出滑动窗口与模板的相似值

Method  计算匹配程度的方法

  关于匹配方法,使用不同的方法产生的结果的意义可能不太一样,有些返回的值越大表示匹配程度越好,而有些方法返回的值越小表示匹配程度越好

关于参数 method
CV_TM_SQDIFF平方差匹配法:该方法采用平方差来进行匹配;最好的匹配值为0;匹配越差,匹配值越大。
CV_TM_CCORR相关匹配法:该方法采用乘法操作;数值越大表明匹配程度越好。
CV_TM_CCOEFF相关系数匹配法:1表示完美的匹配;-1表示最差的匹配。
CV_TM_SQDIFF_NORMED归一化平方差匹配法
CV_TM_CCORR_NORMED归一化相关匹配法
CV_TM_CCOEFF_NORMED归一化相关系数匹配法

 

(2):接着就是要找最值以及最值对应的坐标

cvMinMaxLoc()寻找一个矩阵中最大最小值以及相应的坐标

cvMinMaxLoc( constCvArr* arr, double* min_val, double* max_val,

                          CvPoint* min_locCV_DEFAULT(NULL),

                          CvPoint* max_locCV_DEFAULT(NULL),

                          const CvArr* mask CV_DEFAULT(NULL) );

单目标匹配结果:

代码:

[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. #include <iostream>  
  2. #include "cv.h"  
  3. #include "cxcore.h"  
  4. #include "highgui.h"  
  5. using namespace std;  
  6. int main()  
  7. {  
  8.     IplImage *src = cvLoadImage("E:\\study_opencv_video\\lesson16_1\\images\\src.jpg", 0);  
  9.     IplImage *srcResult = cvLoadImage("E:\\study_opencv_video\\lesson16_1\\images\\src.jpg", 3);  //用来显示  
  10.     IplImage *templat = cvLoadImage("E:\\study_opencv_video\\lesson16_1\\images\\template.png", 0);  
  11.     IplImage *result;  
  12.     if(!src || !templat)  
  13.     {  
  14.         cout << "打开图像失败"<< endl;  
  15.         return 0;  
  16.     }  
  17.     int srcW, srcH, templatW, templatH, resultH, resultW;  
  18.     srcW = src->width;  
  19.     srcH = src->height;  
  20.     templatW = templat->width;  
  21.     templatH = templat->height;  
  22.     if(srcW < templatW || srcH < templatH)  
  23.     {  
  24.         cout <<"模板不能比原图像小" << endl;  
  25.         return 0;  
  26.     }  
  27.     resultW = srcW - templatW + 1;  
  28.     resultH = srcH - templatH + 1;  
  29.     result = cvCreateImage(cvSize(resultW, resultH), 32, 1);      
  30.     cvMatchTemplate(src, templat, result, CV_TM_SQDIFF);     
  31.     double minValue, maxValue;  
  32.     CvPoint minLoc, maxLoc;  
  33.     cvMinMaxLoc(result, &minValue, &maxValue, &minLoc, &maxLoc);    
  34.     cvRectangle(srcResult, minLoc, cvPoint(minLoc.x + templatW, minLoc.y+ templatH), cvScalar(0,0,255));  
  35.     cvNamedWindow("srcResult", 0);  
  36.     cvNamedWindow("templat", 0);  
  37.     cvShowImage("srcResult", srcResult);  
  38.     cvShowImage("templat", templat);  
  39.     cvWaitKey(0);  
  40.     cvReleaseImage(&result);  
  41.     cvReleaseImage(&templat);  
  42.     cvReleaseImage(&srcResult);  
  43.     cvReleaseImage(&src);  
  44.     return 0;  
  45. }  

(3)多目标匹配:

结果:

代码:

[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. #include <iostream>  
  2. #include "cv.h"  
  3. #include "cxcore.h"  
  4. #include "highgui.h"  
  5. using namespace std;  
  6.   
  7. CvPoint getNextMinLoc(IplImage *result, CvPoint minLoc, int maxVaule, int templatW, int templatH)  
  8. {  
  9.   
  10.     // 先将第一个最小值点附近两倍模板宽度和高度的都设置为最大值防止产生干扰  
  11.     int startX = minLoc.x - templatW;  
  12.     int startY = minLoc.y - templatH;  
  13.     int endX = minLoc.x + templatW;  
  14.     int endY = minLoc.y + templatH;  
  15.     if(startX < 0 || startY < 0)  
  16.     {  
  17.         startX = 0;  
  18.         startY = 0;  
  19.     }  
  20.     if(endX > result->width - 1 || endY > result->height - 1)  
  21.     {  
  22.         endX = result->width - 1;  
  23.         endY = result->height - 1;  
  24.     }  
  25.     int y, x;  
  26.     for(y = startY; y < endY; y++)  
  27.     {  
  28.         for(x = startX; x < endX; x++)  
  29.         {  
  30.             cvSetReal2D(result, y, x, maxVaule);  
  31.         }  
  32.     }  
  33.     // 然后得到下一个最小值并且返回  
  34.     double new_minVaule, new_maxValue;  
  35.     CvPoint new_minLoc, new_maxLoc;  
  36.     cvMinMaxLoc(result, &new_minVaule, &new_maxValue, &new_minLoc, &new_maxLoc);  
  37.     return new_minLoc;  
  38.   
  39. }  
  40. int main()  
  41. {  
  42.     IplImage *src = cvLoadImage("E:\\study_opencv_video\\lesson16_1\\images\\src.jpg", 0);  
  43.     IplImage *srcResult = cvLoadImage("E:\\study_opencv_video\\lesson16_1\\images\\src.jpg", 3);  //用来显示  
  44.     IplImage *templat = cvLoadImage("E:\\study_opencv_video\\lesson16_1\\images\\template.png", 0);  
  45.     IplImage *result;  // 用来存放结果  
  46.     if(!src || !templat)  
  47.     {  
  48.         cout << "打开图片失败" << endl;  
  49.         return 0;  
  50.     }  
  51.     int srcW, srcH, templatW, templatH, resultH, resultW;  
  52.     srcW = src->width;  
  53.     srcH = src->height;  
  54.     templatW = templat->width;  
  55.     templatH = templat->height;  
  56.     if(srcW < templatW || srcH < templatH)  
  57.     {  
  58.         cout << "模板不能比原图小" << endl;  
  59.         return 0;  
  60.     }  
  61.     resultW = srcW - templatW + 1;  
  62.     resultH = srcH - templatH + 1;  
  63.     result = cvCreateImage(cvSize(resultW, resultH), 32, 1);    //  匹配方法计算的结果最小值为float  
  64.     cvMatchTemplate(src, templat, result, CV_TM_SQDIFF);     
  65.     double minValue, maxValue;  
  66.     CvPoint minLoc, maxLoc;  
  67.     cvMinMaxLoc(result, &minValue, &maxValue, &minLoc, &maxLoc);    
  68.     cvRectangle(srcResult, minLoc, cvPoint(minLoc.x + templatW, minLoc.y+ templatH), cvScalar(0,0,255));  
  69.     CvPoint new_minLoc;  
  70.   
  71.     // 计算下一个最小值  
  72.     new_minLoc = getNextMinLoc(result, minLoc, maxValue, templatW, templatH);  
  73.     cvRectangle(srcResult, new_minLoc, cvPoint(new_minLoc.x + templatW, new_minLoc.y+ templatH), cvScalar(0,0,255));  
  74.     // 再下一个  
  75.     new_minLoc = getNextMinLoc(result, new_minLoc, maxValue, templatW, templatH);  
  76.     cvRectangle(srcResult, new_minLoc, cvPoint(new_minLoc.x + templatW, new_minLoc.y+ templatH), cvScalar(0,0,255));  
  77.     cvNamedWindow("srcResult", 0);  
  78.     cvNamedWindow("templat", 0);  
  79.     cvShowImage("srcResult", srcResult);  
  80.     cvShowImage("templat", templat);  
  81.     cvWaitKey(0);  
  82.     cvReleaseImage(&result);  
  83.     cvReleaseImage(&templat);  
  84.     cvReleaseImage(&srcResult);  
  85.     cvReleaseImage(&src);  
  86.     return 0;  
  87. }  

作者:小村长  出处:http://blog.csdn.net/lu597203933 欢迎转载或分享,但请务必声明文章出处。 (新浪微博:小村长zack, 欢迎交流!)
0 0
原创粉丝点击