Search code examples
matlabimage-processingcomputer-visionmatlab-cvstopticalflow

How to obtain velocity and magnitude vectors of the optical flow field? I am using Lucas-Kande method of optical flow


I am working on the following code.

n=0;

folder = fileparts(which('viptraffic.avi'));

movieFullFileName = fullfile(folder, 'viptraffic.avi');

vidReader = VideoReader(movieFullFileName);

opticFlow = opticalFlowLK('NoiseThreshold',0.0039);

while hasFrame(vidReader)

   frameRGB = readFrame(vidReader);

   frameGray = rgb2gray(frameRGB);

   flow = estimateFlow(opticFlow,frameGray); 

   H=imag(flow)

   V=real(flow)

    frameWithFlow = getframe(gca);

    imshow(frameRGB);

    imshow(frameWithFlow.cdata)

    hold on

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

    hold off

    n=n+1;

end

Is there a way to get the optical flow estimates of velocity and magnitude for every optical flow field obtained in every image?


Solution

  • estimateFlow returns an opticalFlow object, which has properties for magnitude, phase, and velocity. So, in your case,flow is an opticalFlow object, not a complex array, and you cannot pass it to real and imag. Instead, use the properties of the object:

    flow.Vx          % x component of velocity
    flow.Vy          % y component of velocity
    flow.Orientation % Phase
    flow.Magnitude   % Magnitude