【发布时间】:2015-03-16 18:48:48
【问题描述】:
我正在尝试将 .flo 文件读取为 numpy 2Channels 图像。
格式描述如下:
".flo" file format used for optical flow evaluation
Stores 2-band float image for horizontal (u) and vertical (v) flow components.
Floats are stored in little-endian order.
A flow value is considered "unknown" if either |u| or |v| is greater than 1e9.
bytes contents
0-3 tag: "PIEH" in ASCII, which in little endian happens to be the float 202021.25
(just a sanity check that floats are represented correctly)
4-7 width as an integer
8-11 height as an integer
12-end data (width*height*2*4 bytes total)
the float values for u and v, interleaved, in row order, i.e.,
u[row0,col0], v[row0,col0], u[row0,col1], v[row0,col1], ...
(摘自此readme)
这是我的代码,但我有点卡住了,我不知道如何将文件读取为 2 Channels numpy 2D 数组。
import numpy as np
import os
# test.flo, 512*512 optical flow file
f = open('test.flo', 'rb')
f.seek(11, os.SEEK_SET) # skip header bytes
data_array = np.fromfile(f, np.float16)
data_2D = np.resize(data_array, (512, 512))
也许有人知道怎么做?
【问题讨论】:
-
好吧,格式在您链接的 README 中进行了描述,用于读取
.flo文件的示例 C++ 代码是 here - 有关详细信息,请参阅ReadFlowFile()函数(第 46 行)。对于有一点 C/C++ 知识的人来说翻译应该不会太难(不幸的是我不是……) -
另外,如果你下载
flow-code-matlab.zip,你可以在Matlab中找到readFlowFile.m,如果你更熟练的话。 -
嗨,感谢 cmets,我对 C++ 或数学实验室不是很流利,但我会尝试。实际上我正在尝试找到一个 numpy 解决方案来避免 C++ 风格的循环,这在 python 中会很慢。
-
跳出来的两件事:1)您正在指定 np.float16。从自述文件和 C 源代码中,有两个 4 字节(32 位)浮点数。尝试 np.float32。 2)您正在寻找偏移量 11 .. 数据开始时不应该是偏移量 12 吗?
标签: python c++ image numpy file-format