Estimate optical flow using Farneback method
Estimate the direction and speed of a moving object from one image or video frame to another using the Farneback method.
an optical flow object that you can use to estimate the direction
and speed of an object’s motion.
opticFlow = opticalFlowFarneback
of this class uses the Farneback algorithm to estimate the optical
additional options specified by one or more
opticFlow = opticalFlowFarneback(
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
'NumPyramidLevels'— Number of pyramid layers
3(default) | positive scalar
Number of pyramid layers, specified as the comma-separated pair
'NumPyramidLevels' and a positive
scalar. The value includes the initial image as one of the layers.
When you set this value to
uses the original image only. It does not add any pyramid layers.
opticalFlowFarneback algorithm generates
an image pyramid, where each level has a lower resolution compared
to the previous level. When you select a pyramid level greater than
1, the algorithm can track the points at multiple levels of resolution,
starting at the lowest level. Increasing the number of pyramid levels
enables the algorithm to handle larger displacements of points between
frames. However, the number of computations also increases. Recommended
values are between
The diagram shows an image pyramid with 3 levels.
The algorithm forms each pyramid level by downsampling the previous level. The tracking begins in the lowest resolution level, and continues tracking until convergence. The optical flow algorithm propagates the result of that level to the next level as the initial guess of the point locations. In this way, the algorithm refines the tracking with each level, ending with the original image. Using the pyramid levels enables the optical flow algorithm to handle large pixel motions, which can be distances greater than the neighborhood size.
'PyramidScale'— Image scale
0.5(default) | positive scalar in the range (0,1)
Image scale, specified as the comma-separated pair consisting
PyramidScale' and a positive scalar in the
range (0,1). The pyramid scale is applied to each image at every pyramid
level. A value of
0.5 creates a classical pyramid,
where each level reduces in resolution by a factor of two compared
to the previous level.
'NumIterations'— Number of search iterations per pyramid level
3(default) | positive integer
Number of search iterations per pyramid level, specified as
the comma-separated pair consisting of '
and a positive integer. The Farneback algorithm performs an iterative
search for the new location of each point until convergence.
'NeighborhoodSize'— Size of the pixel neighborhood
5(default) | positive integer
Size of the pixel neighborhood, specified as the comma-separated
pair consisting of '
NeighborhoodSize' and a positive
integer. Increase the neighborhood size to increase blurred motion.
The blur motion yields a more robust estimation of optical flow. A
typical value for
'FilterSize'— Averaging filter size
15(default) | positive integer in the range [2, Inf)
Averaging filter size, specified as the comma-separated pair
consisting of '
FilterSize' and a positive integer
in the range [2, Inf). After the algorithm computes the displacement
(flow), the averaging over neighborhoods is done using a Gaussian
filter of size (
Additionally, the pixels close to the borders are given a reduced
weight because the algorithm assumes that the polynomial expansion
coefficients are less reliable there. Increasing the filter size increases
the robustness of the algorithm to image noise. The larger the filter
size, the greater the algorithm handles image noise and fast motion
detection, making it more robust.
Load a video.
vidReader = VideoReader('visiontraffic.avi','CurrentTime',11);
Set up an optical flow object to do the estimate.
opticFlow = opticalFlowFarneback;
Read in video frames and estimate optical flow of each frame. Display the video frames with flow vectors.
while hasFrame(vidReader) frameRGB = readFrame(vidReader); frameGray = rgb2gray(frameRGB); flow = estimateFlow(opticFlow,frameGray); imshow(frameRGB) hold on plot(flow,'DecimationFactor',[5 5],'ScaleFactor',2) hold off end
 Farneback, G. “Two-Frame Motion Estimation Based on Polynomial Expansion.” Proceedings of the 13th Scandinavian Conference on Image Analysis. Gothenburg, Sweden, 2003.
Usage notes and limitations:
Generates portable C code using a C++ compiler that links to OpenCV (Version 3.1.0) libraries. See Portable C Code Generation for Functions That Use OpenCV Library.