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