Object for estimating optical flow using Lucas-Kanade derivative of Gaussian method
Create an optical flow object for estimating the direction and speed of moving
objects using the Lucas-Kanade derivative of Gaussian (DoG) method. Use the object function
estimateFlow to estimate the optical flow vectors. Using the
reset object function, you can reset the internal state of the optical flow
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 derivative of
Gaussian (DoG) method.
opticFlow = opticalFlowLKDoG
returns an optical flow object with properties specified as one or more
opticFlow = opticalFlowLKDoG(
Name,Value pair arguments. Any unspecified properties have default
values. Enclose each property name in quotes.
NumFrames — Number of buffered frames
3 (default) | positive integer-valued scalar
Number of buffered frames for temporal smoothing, specified as a positive
integer-valued scalar. As you increase this number, the optical flow estimation method
becomes less robust to abrupt changes in the trajectory of the moving objects. The
amount of delay in flow estimation depends on the value of
NumFrames. The output flow corresponds to the image at tflow =
0.5(NumFrames-1), where tcurrent is the time
of the current image.
ImageFilterSigma — Standard deviation for image smoothing filter
1.5 | positive scalar
Standard deviation for image smoothing filter, specified as a positive scalar.
GradientFilterSigma — Standard deviation for gradient smoothing filter
1 | positive scalar
Standard deviation for gradient smoothing filter, specified as a positive scalar.
NoiseThreshold — 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.
Compute Optical Flow Using Lucas-Kanade DoG Method
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 DoG method. Specify the threshold for noise reduction. The output is an optical flow object specifying the optical flow estimation method and its properties.
opticFlow = opticalFlowLKDoG('NoiseThreshold',0.0005)
opticFlow = opticalFlowLKDoG with properties: NumFrames: 3 ImageFilterSigma: 1.5000 GradientFilterSigma: 1 NoiseThreshold: 5.0000e-04
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 = im2gray(frameRGB); flow = estimateFlow(opticFlow,frameGray); imshow(frameRGB) hold on plot(flow,'DecimationFactor',[5 5],'ScaleFactor',35,'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.
Lucas-Kanade Derivative of Gaussian Method
The Lucas-Kanade method computes using a derivative of Gaussian filter.
To solve the optical flow constraint equation for u and v:
Compute and using the following steps:
Use a Gaussian filter to perform temporal filtering. Specify the temporal filter characteristics such as the standard deviation and number of filter coefficients using the
Use a Gaussian filter and the derivative of a Gaussian filter to smooth the image using spatial filtering. Specify the standard deviation and length of the image smoothing filter using the
Compute between images 1 and 2 using the following steps:
Use the derivative of a Gaussian filter to perform temporal filtering. Specify the temporal filter characteristics such as the standard deviation and number of filter coefficients using the
Use the filter described in step 1b to perform spatial filtering on the output of the temporal filter.
Smooth the gradient components, , , and , using a gradient smoothing filter. Use the
GradientFilterSigmaproperty to specify the standard deviation and the number of filter coefficients for the gradient smoothing filter.
Solve the 2-by-2 linear equations for each pixel using the following method:
Then the eigenvalues of A are
When the algorithm finds the eigenvalues, it compares them to the threshold, , that corresponds to the value you enter for the
NoiseThresholdproperty. The results fall into one of the following cases:
Case 1: and
A is nonsingular, so the algorithm solves the system of equations using Cramer's rule.
Case 2: and
A is singular (noninvertible), so the algorithm normalizes the gradient flow 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.
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Introduced in R2015a