OpenCV  3.3.1
Open Source Computer Vision
Enumerations | Functions
Object Detection


enum  cv::TemplateMatchModes {
  cv::TM_SQDIFF = 0,
  cv::TM_CCORR = 2,
  cv::TM_CCORR_NORMED = 3,
  cv::TM_CCOEFF = 4,
 type of the template matching operation More...


void cv::matchTemplate (InputArray image, InputArray templ, OutputArray result, int method, InputArray mask=noArray())
 Compares a template against overlapped image regions. More...

Detailed Description

Enumeration Type Documentation

§ TemplateMatchModes

type of the template matching operation


\[R(x,y)= \sum _{x',y'} (T(x',y')-I(x+x',y+y'))^2\]


\[R(x,y)= \frac{\sum_{x',y'} (T(x',y')-I(x+x',y+y'))^2}{\sqrt{\sum_{x',y'}T(x',y')^2 \cdot \sum_{x',y'} I(x+x',y+y')^2}}\]


\[R(x,y)= \sum _{x',y'} (T(x',y') \cdot I(x+x',y+y'))\]


\[R(x,y)= \frac{\sum_{x',y'} (T(x',y') \cdot I(x+x',y+y'))}{\sqrt{\sum_{x',y'}T(x',y')^2 \cdot \sum_{x',y'} I(x+x',y+y')^2}}\]


\[R(x,y)= \sum _{x',y'} (T'(x',y') \cdot I'(x+x',y+y'))\]


\[\begin{array}{l} T'(x',y')=T(x',y') - 1/(w \cdot h) \cdot \sum _{x'',y''} T(x'',y'') \\ I'(x+x',y+y')=I(x+x',y+y') - 1/(w \cdot h) \cdot \sum _{x'',y''} I(x+x'',y+y'') \end{array}\]


\[R(x,y)= \frac{ \sum_{x',y'} (T'(x',y') \cdot I'(x+x',y+y')) }{ \sqrt{\sum_{x',y'}T'(x',y')^2 \cdot \sum_{x',y'} I'(x+x',y+y')^2} }\]

Function Documentation

§ matchTemplate()

void cv::matchTemplate ( InputArray  image,
InputArray  templ,
OutputArray  result,
int  method,
InputArray  mask = noArray() 

Compares a template against overlapped image regions.

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

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.

imageImage where the search is running. It must be 8-bit or 32-bit floating-point.
templSearched template. It must be not greater than the source image and have the same data type.
resultMap of comparison results. It must be single-channel 32-bit floating-point. If image is \(W \times H\) and templ is \(w \times h\) , then result is \((W-w+1) \times (H-h+1)\) .
methodParameter specifying the comparison method, see cv::TemplateMatchModes
maskMask of searched template. It must have the same datatype and size with templ. It is not set by default. Currently, only the TM_SQDIFF and TM_CCORR_NORMED methods are supported.