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(srcWwidth,minLoc.y+templat->height),CV_RGB(0,255,0),1);
 
       //显示结果
       cvNamedWindow("show");
       cvNamedWindow("tem");
       cvShowImage("show",show);
       cvShowImage("tem", templat);
       cvWaitKey(0);
 
      
       return0;
}


 

你可能感兴趣的:(图像匹配,opencv,模板匹配)