| 31 | return np.resize(data, (int(h), int(w), 2)) |
| 32 | |
| 33 | def readPFM(file): |
| 34 | file = open(file, 'rb') |
| 35 | |
| 36 | color = None |
| 37 | width = None |
| 38 | height = None |
| 39 | scale = None |
| 40 | endian = None |
| 41 | |
| 42 | header = file.readline().rstrip() |
| 43 | if header == b'PF': |
| 44 | color = True |
| 45 | elif header == b'Pf': |
| 46 | color = False |
| 47 | else: |
| 48 | raise Exception('Not a PFM file.') |
| 49 | |
| 50 | dim_match = re.match(rb'^(\d+)\s(\d+)\s$', file.readline()) |
| 51 | if dim_match: |
| 52 | width, height = map(int, dim_match.groups()) |
| 53 | else: |
| 54 | raise Exception('Malformed PFM header.') |
| 55 | |
| 56 | scale = float(file.readline().rstrip()) |
| 57 | if scale < 0: # little-endian |
| 58 | endian = '<' |
| 59 | scale = -scale |
| 60 | else: |
| 61 | endian = '>' # big-endian |
| 62 | |
| 63 | data = np.fromfile(file, endian + 'f') |
| 64 | shape = (height, width, 3) if color else (height, width) |
| 65 | |
| 66 | data = np.reshape(data, shape) |
| 67 | data = np.flipud(data) |
| 68 | return data |
| 69 | |
| 70 | def writeFlow(filename,uv,v=None): |
| 71 | """ Write optical flow to file. |