Jéssica Soares
Jéssica Soares

Reputation: 49

Calculate object's velocity in video frames - Optical Flow (Matlab)

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

Upvotes: 3

Views: 2547

Answers (1)

Dima
Dima

Reputation: 39389

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.

Upvotes: 1

Related Questions