Read .flo file in Middlebury format
(fn)
| 10 | TAG_CHAR = np.array([202021.25], np.float32) |
| 11 | |
| 12 | def readFlow(fn): |
| 13 | """ Read .flo file in Middlebury format""" |
| 14 | # Code adapted from: |
| 15 | # http://stackoverflow.com/questions/28013200/reading-middlebury-flow-files-with-python-bytes-array-numpy |
| 16 | |
| 17 | # WARNING: this will work on little-endian architectures (eg Intel x86) only! |
| 18 | # print 'fn = %s'%(fn) |
| 19 | with open(fn, 'rb') as f: |
| 20 | magic = np.fromfile(f, np.float32, count=1) |
| 21 | if 202021.25 != magic: |
| 22 | print('Magic number incorrect. Invalid .flo file') |
| 23 | return None |
| 24 | else: |
| 25 | w = np.fromfile(f, np.int32, count=1) |
| 26 | h = np.fromfile(f, np.int32, count=1) |
| 27 | # print 'Reading %d x %d flo file\n' % (w, h) |
| 28 | data = np.fromfile(f, np.float32, count=2*int(w)*int(h)) |
| 29 | # Reshape data into 3D array (columns, rows, bands) |
| 30 | # The reshape here is for visualization, the original code is (w,h,2) |
| 31 | return np.resize(data, (int(h), int(w), 2)) |
| 32 | |
| 33 | def readPFM(file): |
| 34 | file = open(file, 'rb') |