Main Content

Calculate Optical Flow by Using Neighborhood Processing Subsystem Blocks

This example shows how to calculate optical flow in a video by using Neighborhood Processing Subsystem blocks. Optical flow is the distribution of the apparent velocities of objects in an image. Use optical flow to identify and track objects in a video.

Inspect Model

Open the model.

model = 'OpticalFlowNeighborhoodExample';
open_system(model);

The model references a video input, rhinos.avi, by using the From Multimedia File block from Computer Vision Toolbox. At each iteration, the model passes the current and previous frames of the video to the DUT subsystem, which performs the optical flow calculation. This calculation outputs an optical flow matrix which represents the apparent motion at each pixel of the video. The model overlays these calculated motion vectors over the input video.

Inspect DUT Subsystem

Open the DUT subsystem.

This example calculates optical flow by using the Lucas-Kanade method. The Lucas-Kanade method requires the values Ix, Iy, and It, which are the derivatives of pixel brightness along the horizontal direction, vertical direction, and time, respectively.

The Compute Ix Neighborhood Processing Subsystem block calculates Ix by using a 1-by-5 neighborhood and these blocks. The Neighborhood control block NeighborhoodConfig specifies the neighborhood size using its Neighborhood size parameter.

The Compute Iy block calculates Iy with the same blocks and a 5-by-1 neighborhood. The model calculates It as the difference between the current video frame and previous video frame by using a Sum block.

The horizontal optical flow u and vertical optical flow v represent the solution to this equation.

Ixu+Iyv+It=0

To solve this equation, the Lucas-Kanade method divides the input image into smaller sections and assumes a constant velocity in each section. Then it performs a weighted, least-square fit of the optical flow constraint equation to a constant model for [uv]T in each section Ω. The method achieves this fit by minimizing this equation, where W is a window function that emphasizes the constraints at the center of each section:

xΩW2[Ixu+Iyv+It]2

The solution to the minimization problem is:

[W2Ix2W2IxIyW2IxIyW2Iy2][uv]=-[W2IxIyW2IyIt]

The example calculates Ix2, IxIy, Iy2, and IyIt by using Product blocks. The example implements Wby using Neighborhood Processing Subsystem blocks with 5-by-5 neighborhoods.

Inspect LK Method Subsystem

Open the LK Method subsystem.

The Calculate Eigenvalues Neighborhood Processing Subsystem calculates the eigenvalues of A=[abcd]=[W2Ix2W2IxIyW2IxIyW2Iy2] by solving the equation λi=a+c2±4b2+(a+c)22;i=1,2.

The example implements the rest of the Lucas-Kanade method by using the Conditions subsystem and a MATLAB Function block. For information about these calculations, see opticalFlowLK (Computer Vision Toolbox).

Simulation and Results

Simulate the model. The model displays the input video with vectors overlaid, representing the optical flow.

Close the model.

close_system(model);

See Also

| (Computer Vision Toolbox)

Related Topics