2011-02-22 21:43:26 +01:00
Object Detection
================
2011-04-19 13:41:12 +02:00
.. highlight :: cpp
2011-02-28 22:26:43 +01:00
matchTemplate
2011-02-22 21:43:26 +01:00
-----------------
2011-06-30 00:06:42 +02:00
Compares a template against overlapped image regions.
2012-05-30 13:13:07 +02:00
.. ocv:function :: void matchTemplate( InputArray image, InputArray templ, OutputArray result, int method )
2011-02-22 21:43:26 +01:00
2011-06-30 00:06:42 +02:00
.. ocv:pyfunction :: cv2.matchTemplate(image, templ, method[, result]) -> result
.. ocv:cfunction :: void cvMatchTemplate( const CvArr* image, const CvArr* templ, CvArr* result, int method )
.. ocv:pyoldfunction :: cv.MatchTemplate(image, templ, result, method)-> None
2011-02-22 21:43:26 +01:00
2011-04-30 15:52:54 +02:00
:param image: Image where the search is running. It must be 8-bit or 32-bit floating-point.
2011-02-22 21:43:26 +01:00
2011-04-30 15:52:54 +02:00
:param templ: Searched template. It must be not greater than the source image and have the same data type.
2011-02-22 21:43:26 +01:00
2011-04-30 15:52:54 +02:00
:param result: Map of comparison results. It must be single-channel 32-bit floating-point. If ``image`` is :math:`W \times H` and ``templ`` is :math:`w \times h` , then ``result`` is :math:`(W-w+1) \times (H-h+1)` .
2012-05-30 13:13:07 +02:00
2011-04-30 15:52:54 +02:00
:param method: Parameter specifying the comparison method (see below).
2011-02-26 12:05:10 +01:00
The function slides through `` image `` , compares the
overlapped patches of size
2011-04-30 15:52:54 +02:00
:math: `w \times h` against `` templ `` using the specified method and stores the comparison results in `` result `` . Here are the formulae for the available comparison
2011-02-22 21:43:26 +01:00
methods (
2011-06-08 17:37:49 +02:00
:math: `I` denotes `` image `` , :math: `T` `` template `` , :math: `R` `` result `` ). The summation is done over template and/or the
2011-02-26 12:05:10 +01:00
image patch:
2011-02-22 21:43:26 +01:00
:math: `x' = 0...w-1, y' = 0...h-1`
* method=CV\_TM\_SQDIFF
2011-02-26 12:05:10 +01:00
2011-02-22 21:43:26 +01:00
.. math ::
2011-02-26 12:05:10 +01:00
R(x,y)= \sum _{x',y'} (T(x',y')-I(x+x',y+y'))^2
2011-02-22 21:43:26 +01:00
* method=CV\_TM\_SQDIFF\_NORMED
2011-02-26 12:05:10 +01:00
2011-02-22 21:43:26 +01:00
.. math ::
2011-02-26 12:05:10 +01:00
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}}
2011-02-22 21:43:26 +01:00
* method=CV\_TM\_CCORR
2011-02-26 12:05:10 +01:00
2011-02-22 21:43:26 +01:00
.. math ::
2011-02-26 12:05:10 +01:00
R(x,y)= \sum _{x',y'} (T(x',y') \cdot I(x+x',y+y'))
2011-02-22 21:43:26 +01:00
* method=CV\_TM\_CCORR\_NORMED
2011-02-26 12:05:10 +01:00
2011-02-22 21:43:26 +01:00
.. math ::
2011-02-26 12:05:10 +01:00
2011-06-08 17:37:49 +02:00
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}}
2011-02-22 21:43:26 +01:00
* method=CV\_TM\_CCOEFF
2011-02-26 12:05:10 +01:00
2011-02-22 21:43:26 +01:00
.. math ::
2011-02-26 12:05:10 +01:00
2011-06-08 17:37:49 +02:00
R(x,y)= \sum _{x',y'} (T'(x',y') \cdot I'(x+x',y+y'))
2011-02-26 12:05:10 +01:00
2011-02-22 21:43:26 +01:00
where
2011-02-26 12:05:10 +01:00
2011-02-22 21:43:26 +01:00
.. math ::
2011-02-26 12:05:10 +01:00
\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}
2011-02-22 21:43:26 +01:00
* method=CV\_TM\_CCOEFF\_NORMED
2011-02-26 12:05:10 +01:00
2011-02-22 21:43:26 +01:00
.. math ::
2011-02-26 12:05:10 +01:00
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} }
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 `` was used) using the
2011-06-16 14:48:23 +02:00
:ocv:func: `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.
2011-02-22 21:43:26 +01:00