使用 Python 读取 Middlebury 'flow' 文件 (bytes array & numpy)
reading middlebury 'flow' files with python (bytes array & numpy)
我正在尝试将 .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], ...
(本自述文件节选)
这是我的代码,但我有点卡住了,我不知道如何将文件读取为 2 通道 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))
也许有人知道如何做到这一点?
试试这个。到目前为止,我已经在一个 .flo 文件上对其进行了测试。
import numpy as np
import sys
if __name__ == '__main__':
if len(sys.argv) <= 1:
print('Specify a .flo file on the command line.')
else:
with open(sys.argv[1], 'rb') as f:
magic, = np.fromfile(f, np.float32, count=1)
if 202021.25 != magic:
print('Magic number incorrect. Invalid .flo file')
else:
w, h = np.fromfile(f, np.int32, count=2)
print(f'Reading {w} x {h} flo file')
data = np.fromfile(f, np.float32, count=2*w*h)
# Reshape data into 3D array (columns, rows, bands)
data2D = np.resize(data, (w, h, 2))
print(data2D)
BSA的答案不适用于Python 3.5及更高版本。下面显示的小修改,例如 np.fromfile(f, np.int32, count=1)[0],将会。
import numpy as np
import os
import sys
# WARNING: this will work on little-endian architectures (eg Intel x86) only!
if '__main__' == __name__:
if len(sys.argv) > 1:
with open(sys.argv[1], 'rb') as f:
magic = np.fromfile(f, np.float32, count=1)
if 202021.25 != magic:
print('Magic number incorrect. Invalid .flo file')
else:
w = np.fromfile(f, np.int32, count=1)[0]
h = np.fromfile(f, np.int32, count=1)[0]
print('Reading %d x %d flo file' % (w, h))
data = np.fromfile(f, np.float32, count=2*w*h)
# Reshape data into 3D array (columns, rows, bands)
data2D = np.resize(data, (h, w, 2))
else:
print('Specify a .flo file on the command line.')
相关文章:
- C++11 中不同类型的对象的 std::array 的替代方案
- constexpr begin of a std::array
- C++如果必须在编译时确定大小,std::array 有什么意义?
- OpenGL VBO Indexing ( How to compute Index Array)
- 标准::unordered_map 中的 std::array 的值初始化
- "Warning: Comma within array index expression"但逗号分隔函数参数
- 确保编译时的特定 std::array 位置
- std::array的长度有大小限制吗?
- 将 std::array 移动到另一个 std::array
- 首先按给定顺序打印所有数字,然后使用 Array 打印所有字符和其他符号
- 为什么 std::shared_ptr 被认为是"heavy"和"expensive",但 std::array "same perfprmance as plain (c-style) arrays
- 将 **float array 从 C++ Dll 传递给 python
- std::bind on statd::array 的运算符 []
- 检查输入 std::array 指针数据是否等于某个常量数组
- 我可以安全地复制矢量<array>吗?
- 解析问题 - 预期的非限定 ID - #include <array> 编译错误
- 如何读/写或遍历 std::array 中的特定元素范围?
- 通过 host() 从 af::array 检索数据会导致错误的数据
- 是否可以使用 std::array 作为 POD 结构的数据容器?
- 使用 Python 读取 Middlebury 'flow' 文件 (bytes array & numpy)