OpenCV warping image based on calcOpticalFlowFarneback

风流意气都作罢 提交于 2019-12-04 03:05:05
jet47

The remap function can't work with optical flow. The function remap transforms the source image using the specified map:

dst(x, y) = src(mapx(x, y), mapy(x, y))

Optical flow has another formula:

frame1(x, y) = frame2(x + flowx(x, y), y + flowy(x, y))

So to use remap function first you need to create a map from flow:

Mat flow; // backward flow
calcOpticalFlowFarneback(nextFrame, prevFrame, flow);

Mat map(flow.size(), CV_32FC2);
for (int y = 0; y < map.rows; ++y)
{
    for (int x = 0; x < map.cols; ++x)
    {
        Point2f f = flow.at<Point2f>(y, x);
        map.at<Point2f>(y, x) = Point2f(x + f.x, y + f.y);
    }
}

Mat newFrame;
remap(prevFrame, newFrame, map);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!