opencv模板匹配matchTemplate 您所在的位置:网站首页 opencv模板匹配相似度 opencv模板匹配matchTemplate

opencv模板匹配matchTemplate

2023-06-07 02:42| 来源: 网络整理| 查看: 265

模板匹配的原理

 

模板匹配可以说是一种最简单的模式识别方法,它的实现主要是通过模板图像在被匹配图像中的平移,在被匹配图像中逐个区域寻找和模板图像相似的区域,如果存在某区域的相似度大于一定的阈值,则表明该区域和模板图像是相匹配的。 但是模板匹配这种方式具有很大的自身局限性:

首先它利用一个规定好的模板进行匹配,这就导致了想要匹配出来的结果必须在大小和角度上和模板图像一模一样,一旦原图像中的匹配目标发生旋转或大小变化,就会导致匹配准确率急剧下降。

而且模板图像在原图像中进行移动的时候,只能够平行地上、下、左、右移动,对于有经过投影变换的图像,这种模板匹配寻找目标的方式可以说是完全无效的。

同时,这种模板匹配方式对于图像的光照变换是十分敏感的,如果是对同一物体在不同光照环境下拍摄,并进行模板匹配,是很难得出令人满意的效果,

模板匹配只适用于光照条件影响很小的匹配任务,工作条件限制比较严格,这也很大的限制了它的应用性。但是对于一些光照不变的图像,在经过预处理之后,还是可以使用模板匹配的方式来寻找匹配目标的。

在 OpenCV 中,提供了相应的函数完成这个操作。

matchTemplate 函数:在模板和输入图像之间寻找匹配,获得匹配结果图像 minMaxLoc 函数:在给定的矩阵中寻找最大和最小值,并给出它们的位置

openCV 提供了 6 种计算两幅图像相似度的方法。

差值平方和匹配 CV_TM_SQDIFF标准化差值平方和匹配 CV_TM_SQDIFF_NORMED相关匹配 CV_TM_CCORR标准相关匹配 CV_TM_CCORR_NORMED相关匹配 CV_TM_CCOEFF标准相关匹配 CV_TM_CCOEFF_NORMED

注意:在opencv4中所有方法的名称取消了CV_的前缀:

标志参数作用TM_SQDIFF平方差匹配法TM_SQDIFF_NORMED归一化平方差匹配法TM_CCORR相关匹配法TM_CCORR_NORMED归一化相关匹配法TM_CCOEFF系数匹配法TM_CCOEFF_NORMED归一化相关系数匹配法

matchTemplate()函数原型 CV_EXPORTS_W void matchTemplate( InputArray image, InputArray templ, OutputArray result, int method, InputArray mask = noArray() );

@brief Compares a template against overlapped image regions.将模板与重叠图像区域进行比较。

The function slides through image , compares the overlapped patches of size \f$w \times h\f$ against templ using the specified method and stores the comparison results in result . #TemplateMatchModes describes the formulae for the available comparison methods ( \f$I\f$ denotes image, \f$T\f$ template, \f$R\f$ result, \f$M\f$ the optional mask ). The summation is done over template and/or the image patch: \f$x' = 0...w-1, y' = 0...h-1\f$

After the function finishes the comparison, the best matches can be found as global minimums (when #TM_SQDIFF was used) or maximums (when #TM_CCORR or #TM_CCOEFF was used) using the #minMaxLoc function. In case of a color image, template summation in the numerator and each sum in the denominator is done over all of the channels and separate mean values are used for each channel. That is, the function can take a color template and a color image. The result will still be a single-channel image, which is easier to analyze.

@param image Image where the search is running. It must be 8-bit or 32-bit floating-point. @param templ Searched template. It must be not greater than the source image and have the same data type. @param result Map of comparison results. It must be single-channel 32-bit floating-point. If image is \f$W \times H\f$ and templ is \f$w \times h\f$, then result is  \f$(W-w+1) \times (H-h+1)\f$. @param method Parameter specifying the comparison method, see #TemplateMatchModes @param mask Optional mask. It must have the same size as templ. It must either have the same number             of channels as template or only one channel, which is then used for all template and             image channels. If the data type is #CV_8U, the mask is interpreted as a binary mask,             meaning only elements where mask is nonzero are used and are kept unchanged independent             of the actual mask value (weight equals 1). For data tpye #CV_32F, the mask values are             used as weights. The exact formulas are documented in #TemplateMatchModes. 第一个参数image:输入的包含匹配目标的图像; 第二个参数templ:进行匹配所用的模板图像; 第三个参数result:输出的匹配结果,为浮点型的Mat对象,其高度是find_image.rows - tem_image.rows + 1 ,宽度是find_image.cols - tem_image.cols + 1; 第四个参数method:使用的计算像素相似度的方式:

(1)CV_TM_SQDIFF(平方差匹配)、CV_TM_SQDIFF_NORMED(标准方差匹配) 这类方法采用原图与待匹配子图像素的平方差来进行累加求和,计算所得数值越小,说明匹配度越高。

(2)CV_TM_CCORR(乘数匹配)、CV_TM_CCORR_NORMED(标准乘数匹配) 这类方法采用原图与待匹配子图对应像素的乘积进行累加求和,与第一类方法相反,数值越大表示匹配度越高。

(3)CV_TM_CCOEFF(相关匹配)、CV_TM_CCOEFF_NORMED(标准相关匹配) 这类方法把原图像素对其均值的相对值与待匹配子图像素对其均值的相对值进行比较,计算数值越接近1,表示匹配度越高。

第五个参数mask:使用的掩膜,没有掩膜的话不用输入直接使用默认值就可以。匹配模板的掩码,必须与模板图像具有相同的数据类型和尺寸,默认情况下不设置,目前仅支持在TM_SQDIFF和TM_CCORR_NORMED这两种匹配方法时使用。

minMaxLoc()函数原型

minMaxLoc函数找到最小值和最大值元素值以及它们的位置。极值在整个数组中搜索出来的,或者,如果掩膜不是一个空数组,那么将在一个特殊的数组中搜寻。

该函数不能用于多通道数组。如果你需要在所有通道中找到最小或者最大值,那么需要先使用Mat::reshape,将它重构成一个单通道数组。或者可以通过使用extractImageCOI,minxChannels或者split函数提取特殊通道的极值。

normalize(src, dst, alpha, beta norm_type, mask) 归一个一个数组的范数或者值

CV_EXPORTS_W void minMaxLoc(InputArray src, CV_OUT double* minVal, CV_OUT double* maxVal = 0, CV_OUT Point* minLoc = 0, CV_OUT Point* maxLoc = 0, InputArray mask = noArray());

Finds the global minimum and maximum in an array.

在一个数组中找到全局最小值和全局最大值

The function cv::minMaxLoc finds the minimum and maximum element values and their positions. The extremums are searched across the whole array or, if mask is not an empty array, in the specified array region.

用于寻找最大值和最小值元素值及其位置。该极值检测遍历整个矩阵,当掩码部位空时,遍历指定的特殊区域。

The function do not work with multi-channel arrays. If you need to find minimum or maximum elements across all the channels, use Mat::reshape first to reinterpret the array as single-channel. Or you may extract the particular channel using either extractImageCOI , or mixChannels , or split .

该函数不适用于多通道矩阵,如果你需要寻找所以通道的全局最大最小值,可首先使用Mat::reshape()重新诠释为但通道矩阵。如果你要检测特殊的同道可使用一下函数 ImageCOT(),minChannels(),spilt().

特别注意,返回的极值为double型。 

@param src input single-channel array. 输入一个单通道数组 @param minVal pointer to the returned minimum value; NULL is used if not required.指向返回的最小值的指针。NULL为不要求。  @param maxVal pointer to the returned maximum value; NULL is used if not required.指向返回的最大值的指针。NULL为不要求。  @param minLoc pointer to the returned minimum location (in 2D case); NULL is used if not required.指向返回最小值的位置(2d情况下),NULL为不要求  @param maxLoc pointer to the returned maximum location (in 2D case); NULL is used if not required.指向返回最大值的位置(2d情况下),NULL为不要求  @param mask optional mask used to select a sub-array.用于指定下级矩阵的操作掩码。 

#include #include using namespace cv; using namespace std; int main() { Mat img = imread("animals.png"); Mat temp = imread("template.png"); if (img.empty() || temp.empty()) { cout


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有