光流.flo文件 [英] optical flow .flo files

查看:475
本文介绍了光流.flo文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对进行光流项目有几个问题.我使用的是Python 2(计划使用千层面来进行深度学习来学习光流),却不知道如何在流的可视化中将c ++函数转换为python的函数.

I have a few questions for doing optical flow projects. I use Python 2 (planning to use lasagne to use deep learning to learn optical flow), and don't know how to convert the c++ functions to that of python in visualization of the flows.

  1. 我从( http://vision.middlebury.edu/flow/data/comp/zip/other-gt-flow.zip )一些图像对,在这些图像对中,我必须估算它们的光流和地面真实流(.flo文件).问题是,当我将.flo文件读入程序时,它是矢量化代码.如何查看它们,就像它们在网页中的显示方式一样( http://vision.middlebury.edu/flow /data/)?我从各种来源阅读并尝试了以下操作,但没有用.

  1. I downloaded (from http://vision.middlebury.edu/flow/data/comp/zip/other-gt-flow.zip) some image pairs where I have to estimate their optical flow, and their ground truth flow (.flo file). The problem is, when I read the .flo file into the program, it is a vectorized code. How do I view them like how they show in the webpage (http://vision.middlebury.edu/flow/data/)? I read from various sources and tried the following, but doesn't work.

在评估EPE(端点错误)时,应该以哪种形式将我的预测与.flo文件进行比较?

In evaluating EPE (end point error) in what form should I have my prediction to be compared with the .flo file?

代码:

################################ Reading flow file ################################

f = open('flow10.flo', 'rb')

x = np.fromfile(f, np.int32, count=1) # not sure what this gives
w = np.fromfile(f, np.int32, count=1) # width
h = np.fromfile(f, np.int32, count=1) # height
print 'x %d, w %d, h %d flo file' % (x, w, h)

data = np.fromfile(f, np.float32) # vector 

data_2D = np.reshape(data, newshape=(388,584,2)); # convert to x,y - flow
x = data_2D[...,0]; y = data_2D[...,1]; 

################################ visualising flow file ################################
mag, ang = cv2.cartToPolar(x,y)
hsv = np.zeros_like(x)
hsv = np.array([ hsv,hsv,hsv ])
hsv = np.reshape(hsv, (388,584,3)); # having rgb channel
hsv[...,1] = 255; # full green channel
hsv[...,0] = ang*180/np.pi/2 # angle in pi
hsv[...,2] = cv2.normalize(mag,None,0,255,cv2.NORM_MINMAX) # magnitude [0,255]
bgr = cv2.cvtColor(hsv,cv2.COLOR_HSV2BGR)
bgr = draw_hsv(data_2D)
cv2.imwrite('opticalhsv.png',bgr)

推荐答案

在Middlebury的页面上,有一个名为flow-code的zip文件(

On Middlebury's page there is a zip file called flow-code (http://vision.middlebury.edu/flow/code/flow-code.zip), which provides a tool called color_flow to convert those .flo files to color images.

另一方面,如果您想实现自己的代码来进行转换,我有这段代码(我无法提供原始作者,已经有一段时间了),可以帮助您首先计算颜色:

On the other hand, if you want to implement your own code to do the transformation, i have this piece of code (i cannot provide the original author, it has been some time) that helps you to first compute the color:

static Vec3b computeColor(float fx, float fy)
{
static bool first = true;

// relative lengths of color transitions:
// these are chosen based on perceptual similarity
// (e.g. one can distinguish more shades between red and yellow
//  than between yellow and green)
const int RY = 15;
const int YG = 6;
const int GC = 4;
const int CB = 11;
const int BM = 13;
const int MR = 6;
const int NCOLS = RY + YG + GC + CB + BM + MR;
static Vec3i colorWheel[NCOLS];

if (first)
{
    int k = 0;

    for (int i = 0; i < RY; ++i, ++k)
        colorWheel[k] = Vec3i(255, 255 * i / RY, 0);

    for (int i = 0; i < YG; ++i, ++k)
        colorWheel[k] = Vec3i(255 - 255 * i / YG, 255, 0);

    for (int i = 0; i < GC; ++i, ++k)
        colorWheel[k] = Vec3i(0, 255, 255 * i / GC);

    for (int i = 0; i < CB; ++i, ++k)
        colorWheel[k] = Vec3i(0, 255 - 255 * i / CB, 255);

    for (int i = 0; i < BM; ++i, ++k)
        colorWheel[k] = Vec3i(255 * i / BM, 0, 255);

    for (int i = 0; i < MR; ++i, ++k)
        colorWheel[k] = Vec3i(255, 0, 255 - 255 * i / MR);

    first = false;
}

const float rad = sqrt(fx * fx + fy * fy);
const float a = atan2(-fy, -fx) / (float)CV_PI;

const float fk = (a + 1.0f) / 2.0f * (NCOLS - 1);
const int k0 = static_cast<int>(fk);
const int k1 = (k0 + 1) % NCOLS;
const float f = fk - k0;

Vec3b pix;

for (int b = 0; b < 3; b++)
{
    const float col0 = colorWheel[k0][b] / 255.f;
    const float col1 = colorWheel[k1][b] / 255.f;

    float col = (1 - f) * col0 + f * col1;

    if (rad <= 1)
        col = 1 - rad * (1 - col); // increase saturation with radius
    else
        col *= .75; // out of range

    pix[2 - b] = static_cast<uchar>(255.f * col);
}

return pix;
}

然后为所有像素调用上述函数:

Then it calls the above function for all the pixels:

static void drawOpticalFlow(const Mat_<Point2f>& flow, Mat& dst, float maxmotion = -1)
{
dst.create(flow.size(), CV_8UC3);
dst.setTo(Scalar::all(0));

// determine motion range:
float maxrad = maxmotion;

if (maxmotion <= 0)
{
    maxrad = 1;
    for (int y = 0; y < flow.rows; ++y)
    {
        for (int x = 0; x < flow.cols; ++x)
        {
            Point2f u = flow(y, x);

            if (!isFlowCorrect(u))
                continue;

            maxrad = max(maxrad, sqrt(u.x * u.x + u.y * u.y));
        }
    }
}

for (int y = 0; y < flow.rows; ++y)
{
    for (int x = 0; x < flow.cols; ++x)
    {
        Point2f u = flow(y, x);

        if (isFlowCorrect(u))
            dst.at<Vec3b>(y, x) = computeColor(u.x / maxrad, u.y / maxrad);
    }
}
}

这是供我在OpenCV中使用的,但是任何想要实现类似目标的人都可以使用代码帮助.

This is for my use in OpenCV, but the code help should anyone who wants achieve something similar.

这篇关于光流.flo文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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