0 — The neighborhood containing the best match is completely or partially outside of the region of interest.

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

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.

Image

Enable to track the valid neighborhood region. The neighborhood matrix of metric values is valid inside of the ROI. The block sets the NValid output as follows:

The SSD 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 l2 norm of the difference image. The general SSD distance metric becomes:

[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.

At each new step, the block moves the search center to the best matching point resulting from the previous step. The number one blue circles in the figure below represent a search with a starting step size of three. The number two green circles represent the next search, with step size of two, centered around the best match found from the previous search. Finally, the number three orange circles represent the final search, with step size of one, centered around the previous best match.

The block outputs either the match metric values or the one-based (x,y) coordinates of the best template match. Optionally the block can output an N-by-N matrix of the match metric values centered around the best match location.

The Three-step search method is a fast search that uses a neighborhood approach versus a search at every pixel. The search starts with a step size equal to or slightly greater than half of the maximum search range and then employs the following steps:

The block decrements the step size by one, after each step, ending the search with a step size of one pixel.

Matrix of match metric values. The matrix of the match metrics always implements single-step exhaustive window iteration. Therefore, the block computes the metric values at every pixel.

The SAD 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 l1 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:

The Template Matching block does not pad the input data. Therefore, it can only compute values for the match metrics between the input image and the template, where the template is positioned such that it falls entirely 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.sizevalid=sizeinput – sizetemplate+1(2)

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

[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.

Size of output matrix. This value determines the size of the N-by-N output matrix centered around the best match location index. N must be an odd number.

The MaxAD 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∞ norm of the difference image. The general MaxAD distance metric becomes:

Region of interest vector in the format (x,y,width,height), where (x,y) are one-based coordinates for the upper-left corner of the region. The block outputs the best match location index relative to the top left corner of the input image.

This parameter appears when you enable the Output NxN matrix of metric values around best match parameter.

Specify the search method as either Exhaustive or Three-step. The Exhaustive search method is computationally intensive because it searches at every pixel location of the image. However, this method provides a more precise result.

Best match location — Output the [x,y] coordinates for the location of the best match. This option adds the Loc output port to the block. It also makes the Search method, Output NxN matrix of metric values around best match, and Enable ROI processing parameters available.

The Output parameter on the Data Types pane appears when you set the Output parameter on the Main tab to Metric matrix or if you set it to Best match location and you enable the Output NxN matrix of metric values around best match parameter.

Best template match located at the one-based (x,y) coordinates. 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 block outputs the best match coordinates, relative to the top-left corner of the image. The one-based [x y] coordinates of the location correspond to the center of the template. The following table shows how the block outputs the center coordinates for odd and even templates:

The block compares nine search points in each step. There is a central point and eight search points located on the search area boundary.

Metric values in the neighborhood of the best match, returned as an N-by-N matrix. N, specified in the mask, must be an odd number. When you select Best match location to return the matrix of metrics in a neighborhood around the best match, an exhaustive loop computes all the metric values for the N-by-N neighborhood. This output is particularly useful for performing template matching with subpixel accuracy.

Input image to use with the template. The block does not pad the input data. Therefore, it can only compute values for the match metrics between the input image and the template, where the template is positioned such that it falls entirely 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.sizevalid=sizeinput – sizetemplate+1(1)

Enable to track the valid region of interest. 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 ROIValid output as follows:

When you enable this parameter, the block adds the ROI input port, and the Output flag indicating if ROI is valid parameter appears.

The block computes the match metric at each step of the iteration. Choose the match metric that best suits your application. The block calculates the global optimum for the best metric value. It uses the valid subregion of the input image intersected by the ROI, if provided.