Template Matching
Using the Template Matching Block
Template matching is a technique in image
processing for finding subregions of an image which match a template
image. Template matching applications include manufacturing, robotics,
edge, or shape detection and facial recognition. The matching process
moves the template image to all possible positions in a larger source
image. It computes a numerical metric that indicates how well the
template matches the image in that position.
You can use any of the follow metrics for template matching
with the Video and Image Processing Blockset:
Sum of Absolute Differences (SAD)
Sum of Squared Differences (SSD)
Maximum Absolute Difference (MaxAD)
Choosing an Output Option
The block outputs either a matrix of match metric values or
the zero-based location of the best template match. The block outputs
the matrix to the Metric port or the location
to the Loc port. Optionally, the block can output
an NxN matrix of neighborhood match metric values
to the NMetric port.
Input and Output Signal Sizes
Since the Template Matching block does not pad
the input data, it can only compute values for the match metrics between
the input image and the template for where the template is positioned
such that it entirely falls on the input image. A set of all such
positions of the template is termed as the valid region
of the input image. The size of the valid region
is the difference between the sizes of the input and template images
plus one.

The output at the Metric port for the Match
metric mode is of the valid image size.
The output at the Loc port for the Best
match index mode is a two-element vector of indices relative
to the top-left corner of the input image.
The neighborhood metric output at the NMetric port
is of the size NxN where N must
be an odd number defined in the block mask.
Defining the Region of Interest (ROI)
To perform template matching on a subregion of the input image,
select the Enable ROI processing check box. This
check box adds an input port ROI to the Template
Matching block. The ROI processing option is only available
for the Best match index mode.
The ROI port requires a four-element vector that defines a rectangular
area. The first two elements represent the zero-based row and column
coordinates for the upper-left corner. The second two elements represent
the height and width of the ROI. The block outputs best match location
index relative to the top left corner of the input image.
Choosing a Match Metric
The block computes the match metric at each step of the iteration.
Choose the match metric which best suits your application. The block
evaluates the best metric value based on the metric choice. The best
metric is the global optimum over the support of the "valid"
subregion of the input image intersected by the ROI, if provided.
Returning the Matrix of Match Metric Values
The matrix of the match metric values always implements single-step
exhaustive window iteration. Therefore, the block computes the metric
values at every pixel.
Returning the Best Match Location
When in the ROI processing mode, the block treats the image
around the ROI as an extension of the ROI subregion. Therefore, it
computes the best match locations true to the actual boundaries of
the ROI. The best match location indices returned are relative to
the top-left corner of the input image.
Returning the Neighborhood Match Metric around the Best Match
If you choose to return the matrix of metric values in a neighborhood
around the best match, an exhaustive loop computes all the metric
values for the NxN neighborhood. This output
is particularly useful for performing template matching with subpixel
accuracy.
Choosing a Search Method
When you select Best match location as the
output option, you can choose to use either Exhaustive or Three-step search
methods.
The Exhaustive search method is computation
intensive because it searches at every pixel location of the image.
The Three-step search method is a fast search
method employing the following steps:
The search starts with a step size equal to or slightly
greater than half of the maximum search range.
The block compares nine search points in each step.
They comprise the central point of the search square, and eight search
points located on the search area boundaries.
The block decrements the step size by one, after each
step, ending the search with a step size of 1 pixel.
At each new step, the block moves the search center
to the best matching point resulting from the previous step.

Three-Step Search
Using the ROIValid and NValid flags for Diagnostics
The ROIValid and NValid ports
represent boolean flags, which track the valid Region of Interest
(ROI) and neighborhood. You can use these flags to communicate with
the downstream blocks and operations.
Valid Region of Interest
If you select the Output flag indicating if ROI is
valid check box, the block adds the ROIValid port.
If the ROI lies partially outside the valid image, the block only
processes the intersection of the ROI and the valid image. The block
sets the ROI flag output to this port as follows:
True, set to 1 indicating the ROI
lies completely inside the input image.
False, set to 0 indicating the
ROI lies completely or partially outside of the input image.
Valid Neighborhood
The neighborhood matrix of metric values is valid inside of
the Region of Interest (ROI). You can use the Boolean flag at the NValid port
to track the valid neighborhood region. The block sets the neighborhood NValid boolean
flag output as follows:
True, set to 1 indicating that
the neighborhood containing the best match is completely inside the
region of interest.
False, set to 0 indicating that
the neighborhood containing the best match is completely or partially
outside of the region of interest.
Back to Top
Video Stabilization
The Estimate Motion Subsystem for Video Stabilization demo
implements the Template Matching block. The purpose
of the model is to track a license plate of a vehicle while reducing
the effect of camera motion from a video stream.

In the first video frame, the model defines the target to track.
In this case, it is the back of a car and the license plate. It also
establishes a dynamic search region, where the last known target location
determines the position.
You can find demos for the Video and Image Processing Blockset by
typing vipdemos on
the MATLAB command line. You can launch the Video Stabilization
model directly by typing vipstabilize on
the MATLAB command line.
Back to Top
Panorama Creation
The Motion Estimation Subsystem of the Panorama Creation demo
implements the Template Matching block. This model
uses the block to estimate the motion between consecutive video frames.
Then it computes the motion vector of a particular block in the current
frame with respect to the previous frame. The model uses this motion
vector to align consecutive frames of the video to form a panoramic
picture.

You can find demos for the Video and Image Processing Blockset by
typing vipdemos on
the MATLAB command line. You can launch the Panorama model directly
by typing vippanorama on
the MATLAB command line.
Back to Top
 | Image Enhancement | | Pixel Statistics |  |
Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.
Get this Simulink Kit