gpt4 book ai didi

python - 光流 .flo 文件

转载 作者:太空宇宙 更新时间:2023-11-03 21:19:05 43 4
gpt4 key购买 nike

我有几个关于做光流项目的问题。我使用 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 文件进行比较?

代码:

################################ 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 ( 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 中使用的,但代码可以帮助任何想要实现类似目标的人。

关于python - 光流 .flo 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40330965/

43 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com