Documentation Center

  • Trial Software
  • Product Updates

Contents

Track an Object Using Correlation

You can open the example model by typing

ex_vision_track_objectex_vision_track_object

on the MATLAB® command line.

In this example, you use the 2-D Correlation, 2-D Maximum, and Draw Shapes blocks to find and indicate the location of a sculpture in each video frame:

  1. Create a new Simulink® model, and add to it the blocks shown in the following table.

    Block

    Library

    Quantity

    Read Binary File

    Computer Vision System Toolbox™ > Sources

    1

    Image Data Type Conversion

    Computer Vision System Toolbox > Conversions

    1

    Image From File

    Computer Vision System Toolbox > Sources

    1

    2-D Correlation

    Computer Vision System Toolbox > Statistics

    1

    2-D Maximum

    Computer Vision System Toolbox > Statistics

    1

    Draw Shapes

    Computer Vision System Toolbox > Text & Graphics

    1

    Video Viewer

    Computer Vision System Toolbox > Sinks

    1

    Data Type Conversion

    Simulink > Signal Attributes

    1

    Constant

    Simulink > Sources

    1

    Mux

    Simulink > Signal Routing

    1

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

  3. Use the Image Data Type Conversion block to convert the data type of the video to single-precision floating point. Accept the default parameter.

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

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

  6. Use the 2-D Maximum block to find the index of the maximum value in each input matrix. Set the Mode parameter to Index. This block outputs the zero-based location of the maximum value as a two-element vector of 32-bit unsigned integers at the Idx port.

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

  8. Use the Constant block to define the size of the image of the cat sculpture. Set the Constant value parameter to single([41 41]).

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

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

  11. Use the Video Viewer block to display the video stream with the ROI displayed on it. Accept the default parameters. This 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.

  12. Connect the blocks as shown in the following figure.

  13. Set the configuration parameters. Open the Configuration dialog box by selecting Model 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)

  14. Run the simulation.

    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, 2-D Maximum, and Draw Shapes block reference pages.

    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.

Was this topic helpful?