阅读Middlebury' flow' python文件(bytes array& numpy)

时间:2015-01-18 18:23:34

标签: python c++ image numpy file-format

我试图将.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通道numpy二维数组。

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

也许有人知道该怎么做?

2 个答案:

答案 0 :(得分:6)

试试这个。到目前为止,我已经在一个.flo文件上测试了它。

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)
                h = np.fromfile(f, np.int32, count=1)
                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, (w, h, 2))
    else:
        print 'Specify a .flo file on the command line.'

答案 1 :(得分:4)

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.'