-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathflow_utils.py
300 lines (240 loc) · 7.84 KB
/
flow_utils.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import os
import re
import numpy as np
import tensorflow as tf
UNKNOWN_FLOW_THRESH = 1e7
SMALLFLOW = 0.0
LARGEFLOW = 1e8
'''
Library for manipulate optical flow are borrowed from:
- https://github.com/liruoteng/OpticalFlowToolkit
Copyright (c) 2019 LI RUOTENG
MIT License
'''
def read_flo_file(filename):
"""
Read from Middlebury .flo file
:param flow_file: name of the flow file
:return: optical flow data in matrix
"""
f = open(filename, 'rb')
magic = np.fromfile(f, np.float32, count=1)
data2d = None
if 202021.25 != magic:
print('Magic number incorrect. Invalid .flo file')
else:
w = np.fromfile(f, np.int32, count=1)[0]
h = np.fromfile(f, np.int32, count=1)[0]
# print("Reading %d x %d flow file in .flo format" % (h, w))
data2d = np.fromfile(f, np.float32, count=2 * w * h)
# reshape data into 3D array (columns, rows, channels)
data2d = np.resize(data2d, (h, w, 2))
f.close()
return data2d
def read_pfm_file(flow_file):
"""
Read from .pfm file
:param flow_file: name of the flow file
:return: optical flow data in matrix
"""
def readPFM(file):
file = open(file, 'rb')
color = None
width = None
height = None
scale = None
endian = None
header = file.readline().rstrip().decode('utf-8')
assert header == 'PF'
dim_match = file.readline().decode('utf-8')
dim_match = re.match(r'^(\d+)\s(\d+)\s$', dim_match)
if dim_match:
width, height = map(int, dim_match.groups())
else:
raise Exception('Malformed PFM header.')
scale = float(file.readline().rstrip().decode('utf-8'))
if scale < 0: # little-endian
endian = '<'
scale = -scale
else:
endian = '>' # big-endian
data = np.fromfile(file, endian + 'f')
shape = (height, width, 3)
data = np.reshape(data, shape)[:, :, :2]
data = np.flipud(data)
return data, scale
(data, scale) = readPFM(flow_file)
return data
def read_flow(filename):
"""
read optical flow data from flow file
:param filename: name of the flow file
:return: optical flow data in numpy array
"""
if filename.endswith('.flo'):
flow = read_flo_file(filename)
elif filename.endswith('.pfm'):
flow = read_pfm_file(filename)
else:
raise Exception('Invalid flow file format!')
return flow
def flow_to_image(flow):
"""
Convert flow into middlebury color code image
:param flow: optical flow map
:return: optical flow image in middlebury color
"""
u = flow[:, :, 0]
v = flow[:, :, 1]
maxu = -999.
maxv = -999.
minu = 999.
minv = 999.
idxUnknow = (abs(u) > UNKNOWN_FLOW_THRESH) | (abs(v) > UNKNOWN_FLOW_THRESH)
u[idxUnknow] = 0
v[idxUnknow] = 0
maxu = max(maxu, np.max(u))
minu = min(minu, np.min(u))
maxv = max(maxv, np.max(v))
minv = min(minv, np.min(v))
rad = np.sqrt(u ** 2 + v ** 2)
maxrad = max(-1, np.max(rad))
u = u/(maxrad + np.finfo(float).eps)
v = v/(maxrad + np.finfo(float).eps)
img = compute_color(u, v)
idx = np.repeat(idxUnknow[:, :, np.newaxis], 3, axis=2)
img[idx] = 0
return np.uint8(img)
def make_color_wheel():
"""
Generate color wheel according Middlebury color code
:return: Color wheel
"""
RY = 15
YG = 6
GC = 4
CB = 11
BM = 13
MR = 6
ncols = RY + YG + GC + CB + BM + MR
colorwheel = np.zeros([ncols, 3])
col = 0
# RY
colorwheel[0:RY, 0] = 255
colorwheel[0:RY, 1] = np.transpose(np.floor(255*np.arange(0, RY) / RY))
col += RY
# YG
colorwheel[col:col+YG, 0] = 255 - np.transpose(np.floor(255*np.arange(0, YG) / YG))
colorwheel[col:col+YG, 1] = 255
col += YG
# GC
colorwheel[col:col+GC, 1] = 255
colorwheel[col:col+GC, 2] = np.transpose(np.floor(255*np.arange(0, GC) / GC))
col += GC
# CB
colorwheel[col:col+CB, 1] = 255 - np.transpose(np.floor(255*np.arange(0, CB) / CB))
colorwheel[col:col+CB, 2] = 255
col += CB
# BM
colorwheel[col:col+BM, 2] = 255
colorwheel[col:col+BM, 0] = np.transpose(np.floor(255*np.arange(0, BM) / BM))
col += + BM
# MR
colorwheel[col:col+MR, 2] = 255 - np.transpose(np.floor(255 * np.arange(0, MR) / MR))
colorwheel[col:col+MR, 0] = 255
return colorwheel
def compute_color(u, v):
"""
compute optical flow color map
:param u: optical flow horizontal map
:param v: optical flow vertical map
:return: optical flow in color code
"""
[h, w] = u.shape
img = np.zeros([h, w, 3])
nanIdx = np.isnan(u) | np.isnan(v)
u[nanIdx] = 0
v[nanIdx] = 0
colorwheel = make_color_wheel()
ncols = np.size(colorwheel, 0)
rad = np.sqrt(u**2+v**2)
a = np.arctan2(-v, -u) / np.pi
fk = (a+1) / 2 * (ncols - 1) + 1
k0 = np.floor(fk).astype(int)
k1 = k0 + 1
k1[k1 == ncols+1] = 1
f = fk - k0
for i in range(0, np.size(colorwheel,1)):
tmp = colorwheel[:, i]
col0 = tmp[k0-1] / 255
col1 = tmp[k1-1] / 255
col = (1-f) * col0 + f * col1
idx = rad <= 1
col[idx] = 1-rad[idx]*(1-col[idx])
notidx = np.logical_not(idx)
col[notidx] *= 0.75
img[:, :, i] = np.uint8(np.floor(255 * col*(1-nanIdx)))
return img
def write_flow(flow, filename):
"""
write optical flow in Middlebury .flo format
:param flow: optical flow map
:param filename: optical flow file path to be saved
:return: None
"""
f = open(filename, 'wb')
magic = np.array([202021.25], dtype=np.float32)
(height, width) = flow.shape[0:2]
w = np.array([width], dtype=np.int32)
h = np.array([height], dtype=np.int32)
magic.tofile(f)
w.tofile(f)
h.tofile(f)
flow.tofile(f)
f.close()
'''
Codes for warping optical flow are borrowed from:
- https://github.com/daigo0927/pwcnet
Copyright (c) 2019 diago0927
MIT License
'''
def get_grid(x):
batch_size, height, width, filters = tf.unstack(tf.shape(x))
Bg, Yg, Xg = tf.meshgrid(tf.range(batch_size), tf.range(height), tf.range(width),
indexing = 'ij')
# return indices volume indicate (batch, y, x)
# return tf.stack([Bg, Yg, Xg], axis = 3)
return Bg, Yg, Xg # return collectively for elementwise processing
def bilinear_warp(x, flow):
_, h, w, _ = tf.unstack(tf.shape(x))
grid_b, grid_y, grid_x = get_grid(x)
grid_b = tf.cast(grid_b, tf.float32)
grid_y = tf.cast(grid_y, tf.float32)
grid_x = tf.cast(grid_x, tf.float32)
fx, fy = tf.unstack(flow, axis = -1)
fx_0 = tf.floor(fx)
fx_1 = fx_0+1
fy_0 = tf.floor(fy)
fy_1 = fy_0+1
# warping indices
h_lim = tf.cast(h-1, tf.float32)
w_lim = tf.cast(w-1, tf.float32)
gy_0 = tf.clip_by_value(grid_y + fy_0, 0., h_lim)
gy_1 = tf.clip_by_value(grid_y + fy_1, 0., h_lim)
gx_0 = tf.clip_by_value(grid_x + fx_0, 0., w_lim)
gx_1 = tf.clip_by_value(grid_x + fx_1, 0., w_lim)
g_00 = tf.cast(tf.stack([grid_b, gy_0, gx_0], axis = 3), tf.int32)
g_01 = tf.cast(tf.stack([grid_b, gy_0, gx_1], axis = 3), tf.int32)
g_10 = tf.cast(tf.stack([grid_b, gy_1, gx_0], axis = 3), tf.int32)
g_11 = tf.cast(tf.stack([grid_b, gy_1, gx_1], axis = 3), tf.int32)
# gather contents
x_00 = tf.gather_nd(x, g_00)
x_01 = tf.gather_nd(x, g_01)
x_10 = tf.gather_nd(x, g_10)
x_11 = tf.gather_nd(x, g_11)
# coefficients
c_00 = tf.expand_dims((fy_1 - fy)*(fx_1 - fx), axis = 3)
c_01 = tf.expand_dims((fy_1 - fy)*(fx - fx_0), axis = 3)
c_10 = tf.expand_dims((fy - fy_0)*(fx_1 - fx), axis = 3)
c_11 = tf.expand_dims((fy - fy_0)*(fx - fx_0), axis = 3)
return c_00*x_00 + c_01*x_01 + c_10*x_10 + c_11*x_11