如何将光流场(浮点)映射到像素数据(字符)以进行图像变形? [英] How to map optical flow field (float) to pixel data (char) for image warping?

查看:86
本文介绍了如何将光流场(浮点)映射到像素数据(字符)以进行图像变形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在玩OpenCV中的光流功能,并被卡住了.我已经使用Farneback方法成功生成了X和Y光流场/贴图,但是我不知道如何将其应用于输入图像坐标以扭曲图像.产生的X和Y字段为32位浮点型(0-1.0),但这如何转换为输入和输出图像的坐标?例如1.0是什么?图片的宽度?两者之间的区别?

I've been playing with the optical flow functions in OpenCV and am stuck. I've successfully generated X and Y optical flow fields/maps using the Farneback method, but I don't know how to apply this to the input image coordinates to warp the images. The resulting X and Y fields are of 32bit float type (0-1.0), but how does this translate to the coordinates of the input and output images? For example, 1.0 of what? The width of the image? The difference between the two?

此外,我不确定要应用变换/变形的循环是什么样子.我已经做了很多循环来更改颜色,但是像素始终保持在同一位置.对我来说,移动像素是新的领域!

Plus, I'm not sure what my loop would look like to apply the transform/warp. I've done plenty of loops to change color, but the pixels always remain in the same location. Moving pixels around is new territory for me!

更新:我可以使用它,但是生成的图像混乱:

Update: I got this to work, but the resulting image is messy:

//make a float copy of 8 bit grayscale source image
IplImage *src_img = cvCreateImage(img_sz, IPL_DEPTH_32F, 1);
cvConvertScale(input_img,src_img,1/255.0); //convert 8 bit to float

//create destination image
IplImage *dst_img = cvCreateImage(img_sz, IPL_DEPTH_32F, 1);

for(y = 0; y < flow->height; y++){

    //grab flow maps for X and Y
    float* vx = (float*)(velx->imageData + velx->widthStep*y);
    float* vy = (float*)(vely->imageData + vely->widthStep*y);

    //coords for source and dest image
    const float *srcpx = (const float*)(src_img->imageData+(src_img->widthStep*y));
    float *dstpx = (float*)(dst_img->imageData+(dst_img->widthStep*y));

    for(x=0; x < flow->width; x++)
    {
        int newx = x+(vx[x]);
        int newy = (int)(vy[x])*flow->width;
        dstpx[newx+newy] = srcpx[x];
    }
}

我无法使它正常工作.输出只是乱码:

I could not get this to work. The output was just garbled noise:

cvRemap(src_img,dst_img,velx,vely,CV_INTER_CUBIC,cvScalarAll(0));

推荐答案

流向量是速度值.如果图像1中位于位置(x,y)的像素具有流矢量(vx,vy),则估计其位于位置(x + vx,y + vy)(因此这些值实际上不在 [0,1] 范围内-它们可以更大,也可以是负数).进行变形的最简单方法是创建具有这些值的浮点图像(x方向为 x + vx ,y则类似),然后使用 cv :: remap

The flow vectors are velocity values. If the pixel in image 1 at position (x, y) has the flow vector (vx, vy) it is estimated to be at position (x+vx, y+vy) (so the values aren't really in the [0, 1] range - they can be bigger, and be negative too). Easiest way to do the warping is to create floating point images with those values (x+vx for the x direction, similar for y), and then use cv::remap.

这篇关于如何将光流场(浮点)映射到像素数据(字符)以进行图像变形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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