OpenCV  4.5.1-dev Open Source Computer Vision
Object Detection

## Enumerations

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

## Functions

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

## ◆ TemplateMatchModes

#include <opencv2/imgproc.hpp>

type of the template matching operation

Enumerator
TM_SQDIFF
Python: cv.TM_SQDIFF

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

$R(x,y)= \sum _{x',y'} \left( (T(x',y')-I(x+x',y+y')) \cdot M(x',y') \right)^2$

TM_SQDIFF_NORMED
Python: cv.TM_SQDIFF_NORMED

$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)= \frac{\sum _{x',y'} \left( (T(x',y')-I(x+x',y+y')) \cdot M(x',y') \right)^2}{\sqrt{\sum_{x',y'} \left( T(x',y') \cdot M(x',y') \right)^2 \cdot \sum_{x',y'} \left( I(x+x',y+y') \cdot M(x',y') \right)^2}}$

TM_CCORR
Python: cv.TM_CCORR

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

$R(x,y)= \sum _{x',y'} (T(x',y') \cdot I(x+x',y+y') \cdot M(x',y') ^2)$

TM_CCORR_NORMED
Python: cv.TM_CCORR_NORMED

$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)= \frac{\sum_{x',y'} (T(x',y') \cdot I(x+x',y+y') \cdot M(x',y')^2)}{\sqrt{\sum_{x',y'} \left( T(x',y') \cdot M(x',y') \right)^2 \cdot \sum_{x',y'} \left( I(x+x',y+y') \cdot M(x',y') \right)^2}}$

TM_CCOEFF
Python: cv.TM_CCOEFF

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

where

$\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}$

$\begin{array}{l} T'(x',y')=M(x',y') \cdot \left( T(x',y') - \frac{1}{\sum _{x'',y''} M(x'',y'')} \cdot \sum _{x'',y''} (T(x'',y'') \cdot M(x'',y'')) \right) \\ I'(x+x',y+y')=M(x',y') \cdot \left( I(x+x',y+y') - \frac{1}{\sum _{x'',y''} M(x'',y'')} \cdot \sum _{x'',y''} (I(x+x'',y+y'') \cdot M(x'',y'')) \right) \end{array}$

TM_CCOEFF_NORMED
Python: cv.TM_CCOEFF_NORMED

$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} }$

## ◆ matchTemplate()

 void cv::matchTemplate ( InputArray image, InputArray templ, OutputArray result, int method, InputArray mask = noArray() )
Python:
#include <opencv2/imgproc.hpp>
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 . TemplateMatchModes describes the formulae for the available comparison methods ( $$I$$ denotes image, $$T$$ template, $$R$$ result, $$M$$ the optional mask ). The summation is done over template and/or the image patch: $$x' = 0...w-1, y' = 0...h-1$$
 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 the same data type. result Map 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)$$ . method Parameter specifying the comparison method, see TemplateMatchModes 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.