| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → Video and Image Processing Blockset |
| Contents | Index |
| Learn more about Video and Image Processing Blockset |
Analysis & Enhancement



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.
The match metrics use a difference equation with general form:
![]()
denotes
the metric space
for
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
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:
![]()
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
norm
of the difference image. The general SSD distance metric becomes:
![]()
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
norm
of the difference image. The general MaxAD distance metric becomes:
![]()
which simplifies to:
![]()
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 location index 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
Three-step
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 N-by-N 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.
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 Fixed-point pane of the Template Matching block dialog box appears as shown in the following figure.

Select the rounding mode for fixed-point operations.
Select the overflow mode for fixed-point operations.
Wrap
Saturate
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 Video and Image Processing Blockset 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.
Select this parameter to prevent the autoscaling tool in the Fixed-Point Tool overriding any fixed-point scaling you specify in this block mask. For more information, see fxptdlg, a reference page on the Fixed-Point Tool in the Simulink documentation.
| Port | Supported Data Types |
|---|---|
| I (Input Image) |
|
| T (Template) |
|
| ROI (Region of Interest) |
|
| Metric (Match Metric Values) |
|
| Loc (Best match location [x,y]) |
|
| NMetric (Metric values in Neighborhood of best match) |
|
| NValid (Neighborhood valid) |
|
| ROIValid (ROI valid) |
|
[1] Koga T., et. Al. Motion-compensated 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 .
Video and Image Processing Blockset Demos
![]() | Standard Deviation | To Multimedia File | ![]() |

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |