3
votes

I can't understand how Matlab calculates the velocity matrix of a video frame by Optical Flow using just the current frame. The velocity wouldn't be a relation of different pixels position varying in the time what would include the analysis of two frame or more per time?

http://www.mathworks.com/help/imaq/examples/live-motion-detection-using-optical-flow.html?requestedDomain=www.mathworks.com

http://www.mathworks.com/help/vision/ref/vision.opticalflow-class.html

% Set up for stream
nFrames = 0;
while (nFrames<100)     % Process for the first 100 frames.
    % Acquire single frame from imaging device.
    rgbData = step(vidDevice);

    % Compute the optical flow for that particular frame.
    optFlow = step(optical,rgb2gray(rgbData)); %***HERE IS THE DOUBT! iT JUST USES ONE FRAME!!!***

    % Downsample optical flow field.
    optFlow_DS = optFlow(r, c);
    H = imag(optFlow_DS)*50;
    V = real(optFlow_DS)*50;

    % Draw lines on top of image
    lines = [Y(:)'; X(:)'; Y(:)'+V(:)'; X(:)'+H(:)'];
    rgb_Out = step(shapes, rgbData,  lines');

    % Send image data to video player
    % Display original video.
    step(hVideoIn, rgbData);
    % Display video along with motion vectors.
    step(hVideoOut, rgb_Out);

    % Increment frame count
    nFrames = nFrames + 1;
end
1
I guess it depends on what the step function does. But just at lookign at the code, it looks like it gets the next frame? because to obtain rgbdata the code uses step, and in the next line it uses step again, so my guess is that that line captures the next frame and computes the optical flow together - Ander Biguri

1 Answers

1
votes

vision.OpticalFlow is a class. When you create a vision.OpticalFlow object, and call its step method, it remembers the frame you pass into it. Then on every subsequent call to step, it computes the optical flow between the stored frame from the last call, and the current frame.

By the way, vision.OpticalFlow has been deprecated. If you have a recent version of MATLAB, there is a family of optical flow functions you can use: opticalFlowFarneback, opticalFlowHS, opticalFlowLK, and opticalFlowLKDoG.