based on https://github.com/cgtuebingen/learning-blind-motion-deblurring/blob/master/synthblur/src/flow.cpp#L44
| 9 | |
| 10 | |
| 11 | class Flow(object): |
| 12 | """ |
| 13 | based on https://github.com/cgtuebingen/learning-blind-motion-deblurring/blob/master/synthblur/src/flow.cpp#L44 |
| 14 | """ |
| 15 | def __init__(self): |
| 16 | super(Flow, self).__init__() |
| 17 | self.wheel = None |
| 18 | self._construct_wheel() |
| 19 | |
| 20 | @staticmethod |
| 21 | def read(file): |
| 22 | # https://stackoverflow.com/a/44906777/7443104 |
| 23 | with open(file, 'rb') as f: |
| 24 | magic = np.fromfile(f, np.float32, count=1) |
| 25 | if 202021.25 != magic: |
| 26 | raise Exception('Magic number incorrect. Invalid .flo file') |
| 27 | else: |
| 28 | w = np.fromfile(f, np.int32, count=1)[0] |
| 29 | h = np.fromfile(f, np.int32, count=1)[0] |
| 30 | data = np.fromfile(f, np.float32, count=2 * w * h) |
| 31 | return np.resize(data, (h, w, 2)) |
| 32 | |
| 33 | def _construct_wheel(self): |
| 34 | k = 0 |
| 35 | |
| 36 | RY, YG, GC = 15, 6, 4 |
| 37 | YG, GC, CB = 6, 4, 11 |
| 38 | BM, MR = 13, 6 |
| 39 | |
| 40 | self.wheel = np.zeros((55, 3), dtype=np.float32) |
| 41 | |
| 42 | for i in range(RY): |
| 43 | self.wheel[k] = np.array([255., 255. * i / float(RY), 0]) |
| 44 | k += 1 |
| 45 | |
| 46 | for i in range(YG): |
| 47 | self.wheel[k] = np.array([255. - 255. * i / float(YG), 255., 0]) |
| 48 | k += 1 |
| 49 | |
| 50 | for i in range(GC): |
| 51 | self.wheel[k] = np.array([0, 255., 255. * i / float(GC)]) |
| 52 | k += 1 |
| 53 | |
| 54 | for i in range(CB): |
| 55 | self.wheel[k] = np.array([0, 255. - 255. * i / float(CB), 255.]) |
| 56 | k += 1 |
| 57 | |
| 58 | for i in range(BM): |
| 59 | self.wheel[k] = np.array([255. * i / float(BM), 0, 255.]) |
| 60 | k += 1 |
| 61 | |
| 62 | for i in range(MR): |
| 63 | self.wheel[k] = np.array([255., 0, 255. - 255. * i / float(MR)]) |
| 64 | k += 1 |
| 65 | |
| 66 | self.wheel = self.wheel / 255. |
| 67 | |
| 68 | def visualize(self, nnf): |