be a single-channel byte or floating-point image of size (image.width – templ.width + 1,
image.height – templ.height + 1). The matching method is chosen from one of the options
listed below (we use I to denote the input image, T the template, and R the result image in the definitions).
For each of these, there is also a normalized version
10
:
Figure 7-8: cv::matchTemplate() sweeps a template image patch across another image looking for
matches
Square difference matching method (method = cv::TM_SQDIFF)
These methods match the squared difference, so a perfect match will be 0 and bad matches will be large:
𝑅
!"_!"##
= 𝑇 𝑥
!
, 𝑦
!
− 𝐼 𝑥 + 𝑥
!
, 𝑦 + 𝑦
! !
!
!
,!!
Normalized square difference matching method (method = cv::TM_SQDIFF_NORMED)
𝑅
!"_!"##_!"#$%&
=
𝑇 𝑥
!
, 𝑦
!
− 𝐼 𝑥 + 𝑥
!
, 𝑦 + 𝑦
! !
!
!
,!!
𝑇 𝑥
!
, 𝑦
! !
!
!
,!!
∙ 𝐼 𝑥 + 𝑥
!
, 𝑦 + 𝑦
! !
!
!
,!!
Correlation matching methods (method = cv::TM_CCORR)
These methods multiplicatively match the template against the image, so a perfect match will be large and
bad matches will be small or zero.
𝑅
!!"##
= 𝑇 𝑥
!
, 𝑦
!
∙ 𝐼 𝑥 + 𝑥
!
, 𝑦 + 𝑦
!
!
!
,!!
Normalized cross-correlation matching method (method = cv::TM_SQDIFF_NORMED)
𝑅
!!"##_!"#$%&
=
𝑇 𝑥
!
, 𝑦
!
∙ 𝐼 𝑥 + 𝑥
!
, 𝑦 + 𝑦
!
!
!
,!!
𝑇 𝑥
!
, 𝑦
! !
!
!
,!!
∙ 𝐼 𝑥 + 𝑥
!
, 𝑦 + 𝑦
! !
!
!
,!!
10
The normalized versions were first developed by Galton [Galton] as described by Rodgers [Rodgers88]. The
normalized methods are useful, as they can help reduce ...