光流.flo文件

时间:2016-10-30 16:07:26

标签: python opencv deep-learning opticalflow

我有一些关于光流项目的问题。我使用Python 2(计划使用lasagne来使用深度学习来学习光流),并且不知道如何在流的可视化中将c ++函数转换为python函数。

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

  2. 在评估EPE(终点错误)时,我应该将我的预测与.flo文件进行比较?

  3. 代码:

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

1 个答案:

答案 0 :(得分:5)

在Middlebury的页面上有一个名为flow-code(http://vision.middlebury.edu/flow/code/flow-code.zip)的zip文件,它提供了一个名为color_flow的工具,用于将这些.flo文件转换为彩色图像。

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

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

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

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中使用的,但代码帮助应该是任何想要实现类似功能的人。