Object for estimating optical flow using Lucas-Kanade method
returns an optical
flow object that you can use to estimate the direction and speed of the moving objects in
a video. The optical flow is estimated using the Lucas-Kanade method.
opticFlow = opticalFlowLK
returns an optical flow object with the property
opticFlow = opticalFlowLK(
specified as a
Name,Value pair. Enclose the property name in
threshold— Threshold for noise reduction
0.0039(default) | positive scalar
Threshold for noise reduction, specified as a positive scalar. As you increase this number, the movement of the objects has less impact on optical flow calculation.
Read a video file. Specify the timestamp of the frame to be read.
vidReader = VideoReader('visiontraffic.avi','CurrentTime',11);
Create an optical flow object for estimating the optical flow using Lucas-Kanade method. Specify the threshold for noise reduction. The output is an optical flow object specifying the optical flow estimation method and its properties.
opticFlow = opticalFlowLK('NoiseThreshold',0.009);
Create a custom figure window to visualize the optical flow vectors.
h = figure; movegui(h); hViewPanel = uipanel(h,'Position',[0 0 1 1],'Title','Plot of Optical Flow Vectors'); hPlot = axes(hViewPanel);
Read the image frames and convert to grayscale images. Estimate the optical flow from consecutive image frames. Display the current image frame and plot the optical flow vectors as quiver plot.
while hasFrame(vidReader) frameRGB = readFrame(vidReader); frameGray = rgb2gray(frameRGB); flow = estimateFlow(opticFlow,frameGray); imshow(frameRGB) hold on plot(flow,'DecimationFactor',[5 5],'ScaleFactor',10,'Parent',hPlot); hold off pause(10^-3) end
To compute the optical flow between two images, you must solve this optical flow constraint equation:
, , and are the spatiotemporal image brightness derivatives.
u is the horizontal optical flow.
v is the vertical optical flow.
To solve the optical flow constraint equation for u and v, the Lucas-Kanade method divides the original 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 in each section . The method achieves this fit by minimizing this equation:
W is a window function that emphasizes the constraints at the center of each section. The solution to the minimization problem is
The Lucas-Kanade method computes using
a difference filter,
u and v are solved as follows:
Compute and using the kernel and its transposed form.
Compute between images 1 and 2 using the kernel.
Smooth the gradient components, , , and , using a separable and isotropic 5-by-5 element kernel whose effective 1-D coefficients are .
Solve the 2-by-2 linear equations for each pixel using the following method:
Then the eigenvalues of A are
The eigenvalues are compared to the threshold, , that corresponds to the value you enter for the threshold for noise reduction. The results fall into one of the following cases:
Case 1: and
A is nonsingular, the system of equations are solved using Cramer's rule.
Case 2: and
A is singular (noninvertible), the gradient flow is normalized to calculate u and v.
Case 3: and
The optical flow, u and v, is 0.
 Barron, J. L., D. J. Fleet, S. S. Beauchemin, and T. A. Burkitt. “ Performance of optical flow techniques.” In Proceedings of the IEEE Conference on Computer Vision and Pattern Recognition (CVPR),236-242. Champaign, IL: CVPR, 1992.