| 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 |
| On this page… |
|---|
Pattern matching can be used to recognize and/or locate specific objects in an image. It can be accomplished using several techniques, one of which is correlation. Correlation provides a direct measure of the similarity between two images. Though sensitive to the scaling or rotation of objects, normalized correlation is robust to changes in lighting.
In this example, you use the 2-D Correlation, Maximum, and Draw Shapes blocks to find and indicate the location of a sculpture in each video frame:
Create a new Simulink model, and add to it the blocks shown in the following table.
Block | Library | Quantity |
|---|---|---|
Read Binary File | Video and Image Processing Blockset > Sources | 1 |
Image Data Type Conversion | Video and Image Processing Blockset > Conversions | 1 |
Image From File | Video and Image Processing Blockset > Sources | 1 |
2-D Correlation | Video and Image Processing Blockset > Statistics | 1 |
Maximum | Video and Image Processing Blockset > Statistics | 1 |
Draw Shapes | Video and Image Processing Blockset > Text & Graphics | 1 |
Video Viewer | Video and Image Processing Blockset > Sinks | 1 |
Data Type Conversion | Simulink > Signal Attributes | 1 |
Constant | Simulink > Sources | 1 |
Mux | Simulink > Signal Routing | 1 |
Position the blocks as shown in the following figure.

You are now ready to set your block parameters by double-clicking the blocks, modifying the block parameter values, and clicking OK.
Use the Read Binary File block to import a binary file into the model. Set the block parameters as follows:
File name = cat_video.bin
Four character code = GREY
Number of times to play file = inf
Sample time = 1/30

Use the Image Data Type Conversion block to convert the data type of the video to single-precision floating point. Accept the default parameter.
Use the Image From File block to import the image of the cat sculpture, which is the object you want to track. Set the block parameters as follows:
Main pane, File name = cat_target.png
Data Types pane, Output data type = single
Use the 2-D Correlation block to determine the portion of each video frame that best matches the image of the cat sculpture. Set the block parameters as follows:
Output size = Valid
Select the Normalized output check box.

Because you chose Valid for the Output size parameter, the block outputs only those parts of the correlation that are computed without the zero-padded edges of any input.
Use the Maximum block to find the index of the maximum value in each input matrix. Set the Mode parameter to Index.
The block outputs the zero-based location of the maximum value as a two-element vector of 32-bit unsigned integers at the Idx port.
Use the Data Type Conversion block to change the index values from 32-bit unsigned integers to single-precision floating-point values. Set the Output data type parameter to single.
Use the Constant block to define the size of the image of the cat sculpture. Set the Constant value parameter to single([41 41]).
Use the Mux block to concatenate the location of the maximum value and the size of the image of the cat sculpture into a single vector. You use this vector to define a rectangular region of interest (ROI) that you pass to the Draw Shapes block.
Use the Draw Shapes block to draw a rectangle around the portion of each video frame that best matches the image of the cat sculpture. Accept the default parameters.

Use the Video Viewer block to display the video stream with the ROI displayed on it. Accept the default parameters.
The Video Viewer block automatically displays the video in the Video Viewer window when you run the model. Because the image is represented by single-precision floating-point values, a value of 0 corresponds to black and a value of 1 corresponds to white.
Connect the blocks as shown in the following figure.

Set the configuration parameters. Open the Configuration dialog box by selecting Configuration Parameters from the Simulation menu. Set the parameters as follows:
Solver pane, Stop time = inf
Solver pane, Type = Fixed-step
Solver pane, Solver = Discrete (no continuous states)
The video is displayed in the Video Viewer window and a rectangular box appears around the cat sculpture. To view the video at its true size, right-click the window and select Set Display To True Size.
![]()
As the video plays, you can watch the rectangular ROI follow the sculpture as it moves.
![]()
In this example, you used the 2-D Correlation, 2-D Maximum, and Draw Shapes blocks to track the motion of an object in a video stream. For more information about these blocks, see the 2-D Correlation, Maximum, and Draw Shapes block reference pages in the Video and Image Processing Blockset Reference.
Note This example model does not provide an indication of whether or not the sculpture is present in each video frame. For an example of this type of model, type vippattern at the MATLAB command prompt. |
![]() | Example Applications | Motion Compensation | ![]() |

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 |