如何将光流场(浮点)映射到像素数据(char)以进行图像变形?

Syn*_*tix 3 c c++ graphics opencv image-processing

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

另外,我不确定应用transform/warp的循环是什么样的.我已经做了很多循环来改变颜色,但像素总是保持在同一个位置.移动像素对我来说是新的领域!

更新:我得到了这个工作,但结果图像很乱:

//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];
    }
}
Run Code Online (Sandbox Code Playgroud)

我无法让这个工作.输出只是乱码噪音:

cvRemap(src_img,dst_img,velx,vely,CV_INTER_CUBIC,cvScalarAll(0));
Run Code Online (Sandbox Code Playgroud)

eta*_*ion 5

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