OpenCV warping image based on calcOpticalFlowFarneback

前端 未结 1 798
南方客
南方客 2021-02-20 13:11

I\'m trying to perform a complex warp of an image using Dense Optical Flow (I am trying to wap the second image into roughly the same shape as the first image). I\'m probably g

相关标签:
1条回答
  • 2021-02-20 14:18

    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);
    
    0 讨论(0)
提交回复
热议问题