OpenCV matchTemplate模板匹配

1.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 归一化相关系数匹配法


通过cvMinMaxLoc获取最后的最佳匹配结果

 

1.2matchTemplate官方说明文档

Compares a template against overlappedimage regions.

C++: void matchTemplate(InputArray image,InputArray temp, OutputArray result, int method)

Python: cv2.matchTemplate(image,templ, method[, result]) → result

C: void cvMatchTemplate(constCvArr* image, const CvArr* templ, CvArr* result, int method)

Python: cv.MatchTemplate(image, templ,result, method) → None

Parameters:

image –Image where the search is running. It must be 8-bit or 32-bit floating-point.

templ –Searched template. It must be not greater than the source image and have thesame data type.

result –Map of comparison results. It must be single-channel 32-bit floating-point. If image is  and templ is  , then result is  .

method –Parameter specifying the comparison method (see below).

 

 

The function slides through image , compares the overlapped patches of size  against templ using the specified method and stores the comparison resultsin result . Here are the formulae forthe available comparison methods (  denotes image,  template,  result ). The summation is done over template and/or the image patch:  *

method=CV_TM_SQDIFF

method=CV_TM_SQDIFF_NORMED

method=CV_TM_CCORR


method=CV_TM_CCORR_NORMED


method=CV_TM_CCOEFF


where

method=CV_TM_CCOEFF_NORMED

After the function finishes the comparison,the best matches can be found as global minimums (when CV_TM_SQDIFF was used) or maximums (when CV_TM_CCORR or CV_TM_CCOEFF wasused) using theminMaxLoc() function. Incase of a color image, template summation in the numerator and each sum in thedenominator is done over all of the channels and separate mean values are usedfor each channel. That is, the function can take a color template and a colorimage. The result will still be a single-channel image, which is easier toanalyze.

1.3范例

// findimage.cpp : Defines the entry pointfor the console application.
//
 
#include "stdafx.h"
 
#include "cv.h"
#include "highgui.h"
#include "cxcore.h"
 
//www.opencvchina.com
 
int main(int argc, char* argv[])
{
       IplImage*src,*templat,*result,*show;
       intsrcW,templatW,srcH,templatH,resultW,resultH;
      
       //加载源图像
       src= cvLoadImage("./images/src.jpg" , CV_LOAD_IMAGE_GRAYSCALE);
 
       //用于显示结果
       show= cvLoadImage("./images/src.jpg");
 
       //加载模板图像
       templat= cvLoadImage("./images/template.png" , CV_LOAD_IMAGE_GRAYSCALE);
 
       if(!src|| !templat)
       {
              printf("打开图片失败");
              return0;
       }
 
       srcW= src->width;
       srcH= src->height;
 
       templatW= templat->width;
       templatH= templat->height;
 
       if(srcW<templatW|| srcH<templatH)
       {
              printf("模板不能比原图小");
              return0;
       }
 
       //计算结果矩阵的大小
       resultW= srcW - templatW + 1;
       resultH= srcH - templatH + 1;
 
       //创建存放结果的空间
       result= cvCreateImage(cvSize(resultW,resultH),32,1);
 
       doubleminVal,maxVal;
       CvPointminLoc,maxLoc;
 
       //调用模板匹配函数
       cvMatchTemplate(src,templat,result,CV_TM_SQDIFF);
 
       //查找最相似的值及其所在坐标
       cvMinMaxLoc(result,&minVal,&maxVal,&minLoc,&maxLoc,NULL);
 
       printf("minVal  %f  maxVal %f\n ",minVal,maxVal);
 
       //绘制结果
     cvRectangle(show,minLoc,cvPoint(minLoc.x+templat->width,minLoc.y+templat->height),CV_RGB(0,255,0),1);
 
       //显示结果
       cvNamedWindow("show");
       cvNamedWindow("tem");
       cvShowImage("show",show);
       cvShowImage("tem", templat);
       cvWaitKey(0);
 
      
       return0;
}


 

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值