基于calcOpticalFlowFarneback的OpenCV变形图像 [英] OpenCV warping image based on calcOpticalFlowFarneback

查看:267
本文介绍了基于calcOpticalFlowFarneback的OpenCV变形图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用密集光流"对图像进行复杂的扭曲(我正在尝试将第二个图像换成与第一个图像大致相同的形状).我可能会把所有这些都弄错了,但我会贴出我尝试过的内容:

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 getting this all wrong but Ill post up what I've tried:

cv::Mat flow;
cv::calcOpticalFlowFarneback( mGrayFrame1, mGrayFrame2, flow, 0.5, 3, 15, 3, 5, 1.2, 0 );

cv::Mat newFrame = cv::Mat::zeros( frame.rows, frame.cols, frame.type() );
cv:remap( frame, newFrame, flow, cv::Mat(), CV_INTER_LINEAR );

如果我要从2个灰度框架计算流量垫的想法.我回来了似乎很有意义的流垫,但是现在我正尝试使用此流信息重新映射我的原始(即非灰度)图像.

The idea if that I am calculating the flow mat from 2 gray scale frames. I get back a flow mat which seems to make sense but now I'm trying to remap my original (ie non-grayscale) image using this flow information.

我假设重映射函数是我想要的,但是从中我得到了一个非常严重失真的图像.我所有的色彩信息都无法幸存.我最终得到的是橙色和黑色的图像,与原始图像有点相似.

I assumed that the remap function is what I want but I get a very badly distorted image from it. None of my colour information survives at all. I simply end up with an orange and black image that bears a small resemblance to my original image.

我猜我误解了remap函数,它要么没有实现我想要的功能,要么我传递的内容出了问题.

I'm guessing that I'm misunderstanding the remap function and it either doesn't do what I want it to do or I'm getting something wrong with what I'm passing in.

如果前者对我如何解决此问题有任何建议?如果是后者,我怎么了?

If the former has anyone got any suggestions as to how I can solve this problem? If the latter, what am i getting wrong?

任何帮助深表感谢!

推荐答案

remap函数不能用于光流.函数remap使用指定的映射来变换源图像:

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))

光流具有另一个公式:

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

因此,要首先使用remap函数,您需要从流中创建一个地图:

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);

这篇关于基于calcOpticalFlowFarneback的OpenCV变形图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆