Read an optical flow map. Args: flow_path (ndarray or str): Flow path. quantize (bool): whether to read quantized pair, if set to True, remaining args will be passed to :func:`dequantize_flow`. concat_axis (int): The axis that dx and dy are concatenated,
(flow_path, quantize=False, concat_axis=0, *args, **kwargs)
| 5 | |
| 6 | |
| 7 | def flowread(flow_path, quantize=False, concat_axis=0, *args, **kwargs): |
| 8 | """Read an optical flow map. |
| 9 | |
| 10 | Args: |
| 11 | flow_path (ndarray or str): Flow path. |
| 12 | quantize (bool): whether to read quantized pair, if set to True, |
| 13 | remaining args will be passed to :func:`dequantize_flow`. |
| 14 | concat_axis (int): The axis that dx and dy are concatenated, |
| 15 | can be either 0 or 1. Ignored if quantize is False. |
| 16 | |
| 17 | Returns: |
| 18 | ndarray: Optical flow represented as a (h, w, 2) numpy array |
| 19 | """ |
| 20 | if quantize: |
| 21 | assert concat_axis in [0, 1] |
| 22 | cat_flow = cv2.imread(flow_path, cv2.IMREAD_UNCHANGED) |
| 23 | if cat_flow.ndim != 2: |
| 24 | raise IOError(f'{flow_path} is not a valid quantized flow file, its dimension is {cat_flow.ndim}.') |
| 25 | assert cat_flow.shape[concat_axis] % 2 == 0 |
| 26 | dx, dy = np.split(cat_flow, 2, axis=concat_axis) |
| 27 | flow = dequantize_flow(dx, dy, *args, **kwargs) |
| 28 | else: |
| 29 | with open(flow_path, 'rb') as f: |
| 30 | try: |
| 31 | header = f.read(4).decode('utf-8') |
| 32 | except Exception: |
| 33 | raise IOError(f'Invalid flow file: {flow_path}') |
| 34 | else: |
| 35 | if header != 'PIEH': |
| 36 | raise IOError(f'Invalid flow file: {flow_path}, header does not contain PIEH') |
| 37 | |
| 38 | w = np.fromfile(f, np.int32, 1).squeeze() |
| 39 | h = np.fromfile(f, np.int32, 1).squeeze() |
| 40 | flow = np.fromfile(f, np.float32, w * h * 2).reshape((h, w, 2)) |
| 41 | |
| 42 | return flow.astype(np.float32) |
| 43 | |
| 44 | |
| 45 | def flowwrite(flow, filename, quantize=False, concat_axis=0, *args, **kwargs): |
nothing calls this directly
no test coverage detected