簡體   English   中英

光流.flo文件

[英]optical flow .flo files

對於進行光流項目,我有幾個問題。 我使用Python 2(計划使用千層面來進行深度學習來學習光流),並且不知道如何在流的可視化中將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文件進行比較嗎?

編碼:

################################ 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文件( http://vision.middlebury.edu/flow/code/flow-code.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中使用的,但是任何想要實現類似目標的人都可以使用代碼幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM