Search code examples
pythonc++imagenumpyfile-format

reading middlebury 'flow' files with python (bytes array & numpy)


I'm trying to read a .flo file as a numpy 2Channels image.

The format is described like this:

".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], ...

(Excerpt from this readme)

Here's my code but I'm kind of stuck, I don't know how to read the file as a 2 Channels numpy 2D array.

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

Maybe someone knows how to do that ?


Solution

  • Try this. I've tested it on one .flo file so far.

    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)