This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materials including this page, select Japan from the country navigator on the bottom of this page.

opticalFlowLK class

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.


opticFlow = opticalFlowLK returns an optical flow object used to estimate the direction and speed of an object’s motion. opticalFlowLK uses the Lucas-Kanade algorithm and a difference filter, [-1 1], for temporal smoothing.

opticFlow = opticalFlowLK(Name,Value) includes additional options specified by one or more Name,Value pair arguments.

Input Arguments

expand all

Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is 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 Name1,Value1,...,NameN,ValueN.

Example: 'NoiseThreshold',0.0039

expand all

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.


estimateFlowEstimate optical flow
resetReset the internal state of the object


expand all

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); 

    hold on
    plot(flow,'DecimationFactor',[5 5],'ScaleFactor',10)
    hold off 


expand all

To compute the optical flow between two images, you must solve the following optical flow constraint equation:



  • Ix, Iy, and It are the spatiotemporal image brightness derivatives.

  • u is the horizontal optical flow.

  • v is the vertical optical flow.


[1] Barron, J. L., D. J. Fleet, S. S. Beauchemin, and T. A. Burkitt. “Performance of optical flow techniques”. CVPR, 1992.

Extended Capabilities

Introduced in R2015a