c++opencvopticalflow

OpenCV - why Optical Flow does not work with dequeue<Mat>?


I am trying to use Optical Flow on some videos. But it doesn't work at all when I don't resize the video.

According to the documentation I have set the parameters as:

calcOpticalFlowFarneback(prevgray, current, flow, 0.5, 1, 10, 2, 5, 1.1, 0);

In case of videos that are scaled-down or up it works fine:

enter image description here enter image description here

But in case of keeping the original size of the videos it does not work at all:

enter image description here enter image description here

I have tried changing the parameters of the function:

calcOpticalFlowFarneback(prevgray, current, flow, 0.5, 1, 4, 2, 3, 1.1, 0);
//or
calcOpticalFlowFarneback(prevgray, current, flow, 0.5, 1, 50, 2, 5, 1.2, 0);
//or
calcOpticalFlowFarneback(prevgray, current, flow, 0.5, 1, 100, 20, 7, 1.2, 0);
...

But none of them make any difference. The result for original size videos is still no flow.

The Lukas Kanade algorithm has the exact same problem:

enter image description here enter image description here

When I scale down the 720 x 480 or other high-resolution videos to half-size (360 x 240), Optical Flow algorithms still work well. But they don't work at all for videos without scaling (original size), no matter how I set the parameters.

How can I make Optical Flow work for videos without resizing the video?


Solution

  • According to this post, the problem was with the current and prev pointing to the same frame.

    It was fixed by using frame.clone() instead of frame in the queue of frames.

    deque<Mat> frames;
    ...
    frames.push_back(frame.clone());
    ...
    current = frame;
    prev = frames[frames.size() - 5];
    ...
    calcOpticalFlowFarneback(prevgray, current, flow, 0.5, 1, 10, 2, 5, 1.1, 0);