Object for estimating optical flow using Horn-Schunck 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 Horn-Schunck method.
opticFlow = opticalFlowHS
returns an optical flow object with properties specified as one or more
opticFlow = opticalFlowHS(
Name,Value pair arguments. Any unspecified properties have default
values. Enclose each property name in quotes.
Smoothness— Expected smoothness
1(default) | positive scalar
Expected smoothness of optical flow, specified as a positive scalar. Increase this
value when there is increased motion between consecutive frames. A typical value for
'Smoothness' is around
MaxIteration— Maximum number of iterations
10(default) | positive integer-valued scalar
Maximum number of iterations, specified as a positive integer-valued scalar. Increase this value to estimate the optical flow of objects with low velocity.
The iterative computation stops when the number of iterations equals the value of
'MaxIteration' or when the algorithm reaches the value set for
'VelocityDifference'. To stop computation only by using
'MaxIteration', set the value of
VelocityDifference— Minimum absolute velocity difference
0(default) | positive scalar
Minimum absolute velocity difference, specified as a positive scalar. This value depends on the input data type. Decrease this value to estimate the optical flow of objects that have low velocity.
The iterative computation stops when the algorithm reaches the value set for
'VelocityDifference' or the number of iterations equals
'MaxIteration'. To use only
to stop computation, set
VideoReader object for the input video file,
visiontraffic.avi. Specify the timestamp of the frame to read as 11.
vidReader = VideoReader('visiontraffic.avi','CurrentTime',11);
Specify the optical flow estimation method as
opticalFlowHS. The output is an object specifying the optical flow estimation method and its properties.
opticFlow = opticalFlowHS
opticFlow = opticalFlowHS with properties: Smoothness: 1 MaxIteration: 10 VelocityDifference: 0
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 image frames from the
VideoReader object 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',60,'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.
By assuming that the optical flow is smooth across the entire image, the Horn-Schunck method estimates a velocity field, , that minimizes this equation:
In this equation, and are the spatial derivatives of the optical velocity component, u, and scales the global smoothness term. The Horn-Schunck method minimizes the previous equation to obtain the velocity field, [u v], for each pixel in the image. This method is given by the following equations:
In these equations, is the velocity estimate for the pixel at (x,y), and is the neighborhood average of . For k = 0, the initial velocity is 0.
To solve u and v using the Horn-Schunck method:
Compute and by using the Sobel convolution kernel, , and its transposed form for each pixel in the first image.
Compute between images 1 and 2 using the kernel.
Assume the previous velocity to be 0, and compute the average velocity for each pixel using as a convolution kernel.
Iteratively solve for u and v.
 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.