Estimate optical flow using Lucas-Kanade method
Estimate the direction and speed of a moving object from one image or video frame to another using the Lucas-Kanade method.
an optical flow object used to estimate the direction and speed of
an object’s motion.
opticFlow = opticalFlowLK
opticalFlowLK uses the
Lucas-Kanade algorithm and a difference filter,
for temporal smoothing.
additional options specified by one or more
opticFlow = opticalFlowLK(
comma-separated pairs of
the argument name and
Value is the corresponding value.
Name must appear inside single quotes (
' '). You can
specify several name and value pair arguments in any order as
'NoiseThreshold'— Threshold for noise reduction
0.0039(default) | positive scalar
Threshold for noise reduction, specified as the comma-separated
pair consisting of '
NoiseThreshold' and a positive
scalar. The more you increase this number, the less an object’s
movement affects the optical flow calculation.
Read in a video file.
vidReader = VideoReader('viptraffic.avi');
Create optical flow object.
opticFlow = opticalFlowLK('NoiseThreshold',0.009);
Estimate and display the optical flow of objects in the video.
while hasFrame(vidReader) frameRGB = readFrame(vidReader); frameGray = rgb2gray(frameRGB); flow = estimateFlow(opticFlow,frameGray); imshow(frameRGB) hold on plot(flow,'DecimationFactor',[5 5],'ScaleFactor',10) hold off end
To compute the optical flow between two images, you must solve the following 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 the following 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”. CVPR, 1992.
Usage notes and limitations: