| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
import numpy as np |
| 7 |
|
| 8 |
|
| 9 |
def make_colorwheel(): |
| 10 |
RY = 15 |
| 11 |
YG = 6 |
| 12 |
GC = 4 |
| 13 |
CB = 11 |
| 14 |
BM = 13 |
| 15 |
MR = 6 |
| 16 |
|
| 17 |
ncols = RY + YG + GC + CB + BM + MR |
| 18 |
colorwheel = np.zeros((ncols, 3)) |
| 19 |
col = 0 |
| 20 |
|
| 21 |
|
| 22 |
colorwheel[0:RY, 0] = 255 |
| 23 |
colorwheel[0:RY, 1] = np.floor(255 * np.arange(0, RY) / RY) |
| 24 |
col = col + RY |
| 25 |
|
| 26 |
colorwheel[col : col + YG, 0] = 255 - np.floor(255 * np.arange(0, YG) / YG) |
| 27 |
colorwheel[col : col + YG, 1] = 255 |
| 28 |
col = col + YG |
| 29 |
|
| 30 |
colorwheel[col : col + GC, 1] = 255 |
| 31 |
colorwheel[col : col + GC, 2] = np.floor(255 * np.arange(0, GC) / GC) |
| 32 |
col = col + GC |
| 33 |
|
| 34 |
colorwheel[col : col + CB, 1] = 255 - np.floor(255 * np.arange(CB) / CB) |
| 35 |
colorwheel[col : col + CB, 2] = 255 |
| 36 |
col = col + CB |
| 37 |
|
| 38 |
colorwheel[col : col + BM, 2] = 255 |
| 39 |
colorwheel[col : col + BM, 0] = np.floor(255 * np.arange(0, BM) / BM) |
| 40 |
col = col + BM |
| 41 |
|
| 42 |
colorwheel[col : col + MR, 2] = 255 - np.floor(255 * np.arange(MR) / MR) |
| 43 |
colorwheel[col : col + MR, 0] = 255 |
| 44 |
return colorwheel |
| 45 |
|
| 46 |
|
| 47 |
def flow_compute_color(flow_u, flow_v): |
| 48 |
H, W = flow_u.shape[:2] |
| 49 |
flow_image = np.zeros((H, W, 3), np.uint8) |
| 50 |
|
| 51 |
colorwheel = make_colorwheel() |
| 52 |
ncols = colorwheel.shape[0] |
| 53 |
|
| 54 |
rad = np.sqrt(np.square(flow_u) + np.square(flow_v)) |
| 55 |
a = np.arctan2(-flow_v, -flow_u) / np.pi |
| 56 |
|
| 57 |
fk = (a + 1) / 2 * (ncols - 1) + 1 |
| 58 |
k0 = np.floor(fk).astype(np.int32) |
| 59 |
k1 = k0 + 1 |
| 60 |
k1[k1 == ncols] = 1 |
| 61 |
f = fk - k0 |
| 62 |
|
| 63 |
for i in range(colorwheel.shape[1]): |
| 64 |
tmp = colorwheel[:, i] |
| 65 |
col0 = tmp[k0] / 255.0 |
| 66 |
col1 = tmp[k1] / 255.0 |
| 67 |
col = (1 - f) * col0 + f * col1 |
| 68 |
|
| 69 |
idx = rad <= 1 |
| 70 |
col[idx] = 1 - rad[idx] * (1 - col[idx]) |
| 71 |
col[~idx] = col[~idx] * 0.75 |
| 72 |
|
| 73 |
ch_idx = i |
| 74 |
flow_image[:, :, ch_idx] = np.floor(255 * col) |
| 75 |
|
| 76 |
return flow_image |
| 77 |
|
| 78 |
|
| 79 |
def flow2rgb(flow_uv): |
| 80 |
"""Visualize optical flow. |
| 81 |
|
| 82 |
Parameters |
| 83 |
---------- |
| 84 |
flow_uv: numpy.ndarray, (H, W, 2), float |
| 85 |
Optical flow. |
| 86 |
|
| 87 |
Returns |
| 88 |
------- |
| 89 |
dst: numpy.ndarray |
| 90 |
RGB image. |
| 91 |
|
| 92 |
""" |
| 93 |
assert flow_uv.ndim == 3, "flow must be 3 dimensional" |
| 94 |
assert flow_uv.shape[2] == 2, "flow must have shape (H, W, 2)" |
| 95 |
assert np.issubdtype(flow_uv.dtype, np.floating), "float must be float type" |
| 96 |
|
| 97 |
flow_u = flow_uv[:, :, 0] |
| 98 |
flow_v = flow_uv[:, :, 1] |
| 99 |
|
| 100 |
rad = np.sqrt(np.square(flow_u) + np.square(flow_v)) |
| 101 |
rad_max = np.max(rad) |
| 102 |
|
| 103 |
epsilon = 1e-5 |
| 104 |
flow_u = flow_u / (rad_max + epsilon) |
| 105 |
flow_v = flow_v / (rad_max + epsilon) |
| 106 |
|
| 107 |
return flow_compute_color(flow_u, flow_v) |