This repository has been archived by the owner on Oct 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathflow_color.py
125 lines (97 loc) · 2.54 KB
/
flow_color.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import sys
import numpy as np
def makeColorwheel():
# color encoding scheme
# adapted from the color circle idea described at
# http://members.shaw.ca/quadibloc/other/colint.htm
RY = 15
YG = 6
GC = 4
CB = 11
BM = 13
MR = 6
ncols = RY + YG + GC + CB + BM + MR
colorwheel = np.zeros([ncols, 3]) # r g b
col = 0
#RY
colorwheel[0:RY, 0] = 255
colorwheel[0:RY, 1] = np.floor(255*np.arange(0, RY, 1)/RY)
col += RY
#YG
colorwheel[col:YG+col, 0]= 255 - np.floor(255*np.arange(0, YG, 1)/YG)
colorwheel[col:YG+col, 1] = 255;
col += YG;
#GC
colorwheel[col:GC+col, 1]= 255
colorwheel[col:GC+col, 2] = np.floor(255*np.arange(0, GC, 1)/GC)
col += GC;
#CB
colorwheel[col:CB+col, 1]= 255 - np.floor(255*np.arange(0, CB, 1)/CB)
colorwheel[col:CB+col, 2] = 255
col += CB;
#BM
colorwheel[col:BM+col, 2]= 255
colorwheel[col:BM+col, 0] = np.floor(255*np.arange(0, BM, 1)/BM)
col += BM;
#MR
colorwheel[col:MR+col, 2]= 255 - np.floor(255*np.arange(0, MR, 1)/MR)
colorwheel[col:MR+col, 0] = 255
return colorwheel
def computeColor(u, v):
colorwheel = makeColorwheel();
nan_u = np.isnan(u)
nan_v = np.isnan(v)
nan_u = np.where(nan_u)
nan_v = np.where(nan_v)
u[nan_u] = 0
u[nan_v] = 0
v[nan_u] = 0
v[nan_v] = 0
ncols = colorwheel.shape[0]
radius = np.sqrt(u**2 + v**2)
a = np.arctan2(-v, -u) / np.pi
fk = (a+1) /2 * (ncols-1) # -1~1 maped to 1~ncols
k0 = fk.astype(np.uint8) # 1, 2, ..., ncols
k1 = k0+1;
k1[k1 == ncols] = 0
f = fk - k0
img = np.empty([k1.shape[0], k1.shape[1],3])
ncolors = colorwheel.shape[1]
for i in range(ncolors):
tmp = colorwheel[:,i]
col0 = tmp[k0]/255
col1 = tmp[k1]/255
col = (1-f)*col0 + f*col1
idx = radius <= 1
col[idx] = 1 - radius[idx]*(1-col[idx]) # increase saturation with radius
col[~idx] *= 0.75 # out of range
img[:,:,2-i] = np.floor(255*col).astype(np.uint8)
return img.astype(np.uint8)
def computeImg(flow):
eps = sys.float_info.epsilon
UNKNOWN_FLOW_THRESH = 1e9
UNKNOWN_FLOW = 1e10
u = flow[: , : , 0]
v = flow[: , : , 1]
maxu = -999
maxv = -999
minu = 999
minv = 999
maxrad = -1
#fix unknown flow
greater_u = np.where(u > UNKNOWN_FLOW_THRESH)
greater_v = np.where(v > UNKNOWN_FLOW_THRESH)
u[greater_u] = 0
u[greater_v] = 0
v[greater_u] = 0
v[greater_v] = 0
maxu = max([maxu, np.amax(u)])
minu = min([minu, np.amin(u)])
maxv = max([maxv, np.amax(v)])
minv = min([minv, np.amin(v)])
rad = np.sqrt(np.multiply(u,u)+np.multiply(v,v))
maxrad = max([maxrad, np.amax(rad)])
u = u/(maxrad+eps)
v = v/(maxrad+eps)
img = computeColor(u, v)
return img