Locate a template in an image
Analysis & Enhancement
visionanalysis
The Template Matching block finds the best match of a template within an input image. The block computes match metric values by shifting a template over a region of interest or the entire image, and then finds the best match location.
Port  Supported Data Types 

I (Input Image) 

T (Template) 

ROI (Region of Interest, [x y width height]) 

Metric (Match Metric Values) 

Loc (Best match location [x y]) 

NMetric (Metric values in Neighborhood of best match) 

NValid (Neighborhood valid) 

ROIValid (ROI valid) 

Defining the Region of Interest (ROI)
Returning the Matrix of Match Metric Values
Returning the Best Match Location
Returning the Neighborhood Match Metric around the Best Match
The match metrics use a difference equation with general form:
$${d}_{p}(x,y)=({\displaystyle \sum _{i=1}^{n}{\left{x}_{i}{y}_{i}\right}^{p}{)}^{\raisebox{1ex}{$1$}\!\left/ \!\raisebox{1ex}{$p$}\right.}}$$
$${l}_{n}^{p}$$ denotes
the metric space $$({R}^{n},{d}_{p})$$ for $${R}^{n}$$ n > 1
.
Sum of Absolute Differences
(SAD)
This metric is also known as the Taxicab or Manhattan Distance metric.
It sums the absolute values of the differences between pixels in the
original image and the corresponding pixels in the template image.
This metric is the $${l}^{1}$$ norm
of the difference image. The lowest SAD score estimates the best position
of template within the search image. The general SAD distance metric
becomes:
$${d}_{1}({I}_{j},T)={\displaystyle \sum _{i=1}^{n}\left{I}_{i,j}{T}_{i}\right}$$
Sum of Squared Differences (SSD)
This metric is also known as the Euclidean Distance metric.
It sums the square of the absolute differences between pixels in the
original image and the corresponding pixels in the template image.
This metric is the square of the $${l}^{2}$$ norm
of the difference image. The general SSD distance metric becomes:
$${d}_{2}({I}_{j},T)={\displaystyle \sum _{i=1}^{n}{\left{I}_{i,j}{T}_{i}\right}^{2}}$$
Maximum Absolute Difference
(MaxAD)
This metric is also known as the Uniform
Distance metric. It sums the maximum of absolute values
of the differences between pixels in the original image and the corresponding
pixels in the template image. This distance metric provides the $${l}^{\infty}$$ norm
of the difference image. The general MaxAD distance metric becomes:
$${d}_{\infty}({I}_{j},T)=\underset{x\to \infty}{\mathrm{lim}}{\displaystyle \sum _{i=1}^{n}{\left{I}_{i,j}{T}_{i}\right}^{p}}$$
which simplifies to:
$${d}_{\infty}({I}_{j},T)=\underset{i}{\overset{n}{\mathrm{max}}}{\left{I}_{i,j}{T}_{i}\right}^{p}$$
The Main pane of the Template Matching block appears as shown in the following figure.
Select one of three types of match metrics:
Sum of absolute differences
(SAD)
Sum of squared differences
(SSD)
Maximum absolute difference
(MaxAD)
Select one of two output types:
Metric matrix
Select
this option to output the match metric matrix. This option adds the Metric output
port to the block.
Best match location
Select
this option to output the [x y] coordinates for the location of the
best match. This option adds the Loc output port
to the block. When you select Best match location
,
the Search method, Output NxN matrix
of metric values around best match, and Enable
ROI processing parameter options appear.
This option appears when you select Best match location
for
the Output parameter. Select one of two search
methods.
Exhaustive
Threestep
This option appears when you select Best match location
for
the Output parameter. Select the check box to
output a matrix of metric values centered around the best match. When
you do so, the block adds the NMetric and NValid output
ports.
This option appears when you select the Output NxN matrix of metric values around best match check box. Enter an integer number that determines the size of the NbyN output matrix centered around the best match location index. N must be an odd number.
This option appears when you select Best match location
for
the Output parameter. Select the check box for
the Template Matching block to perform region of interest
processing. When you do so, the block adds the ROI input
port, and the Output flag indicating if ROI is valid check
box appears. The ROI input must have the format
[x y width height], where [x y] are the coordinates of the upperleft
corner of the ROI.
This option appears when you select the Enable ROI processing check box. Select the check box for the Template Matching block to indicate whether the ROI is within the valid region of the image boundary. When you do so, the block adds the ROIValid output port.
The Data Types pane of the Template Matching block dialog box appears as shown in the following figure.
Select the Rounding Modes for fixedpoint operations.
Select the Overflow mode for fixedpoint operations.
Wrap
Saturate
Use this parameter to specify how to designate the product output word and fraction lengths. Refer to Multiplication Data Types for illustrations depicting the use of the product output data type in this block:
When you select Same as input
,
these characteristics match those of the input to the block.
When you select Binary point scaling
,
you can enter the word length and the fraction length of the product
output, in bits.
When you select Slope and bias scaling
,
you can enter the word length, in bits, and the slope of the product
output. This block requires poweroftwo slope and a bias of zero.
Use this parameter to specify how you would like to designate the accumulator word and fraction lengths.
When you select Same as product output
the
characteristics match the characteristics of the product output. See Multiplication Data Types for
illustrations depicting the use of the accumulator data type in this
block:
When you select Binary point scaling
, you
can enter the Word length and the Fraction
length of the accumulator, in bits.
When you select Slope and bias scaling
, you
can enter the Word length, in bits, and the Slope of
the Accumulator. All signals in the Computer Vision System Toolbox™ software
have a bias of 0.
The block casts inputs to the Accumulator to the accumulator data type. It adds each element of the input to the output of the adder, which remains in the accumulator data type. Use this parameter to specify how to designate this accumulator word and fraction lengths.
Choose how to specify the Word length, Fraction length and Slope of the Template Matching output:
When you select Same as first input
,
these characteristics match the characteristics of the accumulator.
When you select Binary point scaling
,
you can enter the Word length and Fraction
length of the output, in bits.
When you select Slope and bias scaling
,
you can enter the Word length, in bits, and the Slope of
the output. All signals in the Computer Vision System Toolbox software
have a bias of 0.
The Output parameter on the Data Types
pane appears when you select Metric matrix
or
if you select Best match location
and the Output
NxN matrix of metric values around best match check box
is selected.
Select this parameter to prevent the fixedpoint tools from
overriding the data types you specify on the block mask. For more
information, see fxptdlg
,
a reference page on the FixedPoint Tool in the Simulink^{®} documentation.
[1] Koga T., et. Al. Motioncompensated interframe coding for video conferencing. In National Telecommunications Conference. Nov. 1981, G5.3.1–5, New Orleans, LA.
[2] Zakai M., "General distance criteria" IEEE Transaction on Information Theory, pp. 94–95, January 1964.
[3] Yu, J., J. Amores, N. Sebe, Q. Tian, "A New Study on Distance Metrics as Similarity Measurement" IEEE International Conference on Multimedia and Expo, 2006 .
Image Processing Toolbox™  
Computer Vision System Toolbox 