-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfarneback_regularized.py
336 lines (279 loc) · 10.8 KB
/
farneback_regularized.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# from: https://github.com/ericPrince/optical-flow/blob/master/optical_flow.py
import numpy as np
import scipy.ndimage
from functools import partial
import skimage
def poly_exp(f, c, sigma):
"""
Calculates the local polynomial expansion of a 2D signal, as described by Farneback
Uses separable normalized correlation
$f ~ x^T A x + B^T x + C$
If f[i, j] and c[i, j] are the signal value and certainty of pixel (i, j) then
A[i, j] is a 2x2 array representing the quadratic term of the polynomial, B[i, j]
is a 2-element array representing the linear term, and C[i, j] is a scalar
representing the constant term.
Parameters
----------
f
Input signal
c
Certainty of signal
sigma
Standard deviation of applicability Gaussian kernel
Returns
-------
A
Quadratic term of polynomial expansion
B
Linear term of polynomial expansion
C
Constant term of polynomial expansion
"""
# Calculate applicability kernel (1D because it is separable)
n = int(4*sigma + 1)
x = np.arange(-n, n + 1, dtype=np.int)
a = np.exp(-x**2 / (2 * sigma**2)) # a: applicability kernel [n]
# b: calculate b from the paper. Calculate separately for X and Y dimensions
# [n, 6]
bx = np.stack([
np.ones(a.shape),
x,
np.ones(a.shape),
x**2,
np.ones(a.shape),
x
], axis=-1)
by = np.stack([
np.ones(a.shape),
np.ones(a.shape),
x,
np.ones(a.shape),
x**2,
x,
], axis=-1)
# Pre-calculate product of certainty and signal
cf = c * f
# G and v are used to calculate "r" from the paper: v = G*r
# r is the parametrization of the 2nd order polynomial for f
G = np.empty(list(f.shape) + [bx.shape[-1]]*2)
v = np.empty(list(f.shape) + [bx.shape[-1]])
# Apply separable cross-correlations
# Pre-calculate quantities recommended in paper
ab = np.einsum('i,ij->ij', a, bx)
abb = np.einsum('ij,ik->ijk', ab, bx)
# Calculate G and v for each pixel with cross-correlation
for i in range(bx.shape[-1]):
for j in range(bx.shape[-1]):
G[..., i, j] = scipy.ndimage.correlate1d(c, abb[..., i, j], axis=0, mode='constant', cval=0)
v[..., i] = scipy.ndimage.correlate1d(cf, ab[..., i], axis=0, mode='constant', cval=0)
# Pre-calculate quantities recommended in paper
ab = np.einsum('i,ij->ij', a, by)
abb = np.einsum('ij,ik->ijk', ab, by)
# Calculate G and v for each pixel with cross-correlation
for i in range(bx.shape[-1]):
for j in range(bx.shape[-1]):
G[..., i, j] = scipy.ndimage.correlate1d(G[..., i, j], abb[..., i, j], axis=1, mode='constant', cval=0)
v[..., i] = scipy.ndimage.correlate1d(v[..., i], ab[..., i], axis=1, mode='constant', cval=0)
# Solve r for each pixel
r = np.linalg.solve(G, v)
# Quadratic term
A = np.empty(list(f.shape) + [2, 2])
A[..., 0, 0] = r[..., 3]
A[..., 0, 1] = r[..., 5] / 2
A[..., 1, 0] = A[..., 0, 1]
A[..., 1, 1] = r[..., 4]
# Linear term
B = np.empty(list(f.shape) + [2])
B[..., 0] = r[..., 1]
B[..., 1] = r[..., 2]
# constant term
C = r[..., 0]
# b: [n, n, 6]
# r: [f, f, 6]
# f: [f, f]
# e = b*r - f
return A, B, C
def flow_iterative(
f1, f2, sigma, c1, c2, sigma_flow, num_iter=1, d=None, model='constant', mu=None, l_coef=0.95
):
"""
Calculates optical flow with an algorithm described by Gunnar Farneback
Parameters
----------
f1
First image
f2
Second image
sigma
Polynomial expansion applicability Gaussian kernel sigma
c1
Certainty of first image
c2
Certainty of second image
sigma_flow
Applicability window Gaussian kernel sigma for polynomial matching
num_iter
Number of iterations to run (defaults to 1)
d: (optional)
Initial displacement field
p: (optional)
Initial global displacement model parameters
model: ['constant', 'affine', 'eight_param']
Optical flow parametrization to use
mu: (optional)
Weighting term for usage of global parametrization. Defaults to
using value recommended in Farneback's thesis
l_coef:
Regularization coef for getting closer to optimal
Returns
-------
d
Optical flow field. d[i, j] is the (y, x) displacement for pixel (i, j)
"""
# TODO: add initial warp parameters as optional input?
# Calculate the polynomial expansion at each point in the images
A1, B1, C1 = poly_exp(f1, c1, sigma)
A2, B2, C2 = poly_exp(f2, c2, sigma)
# Pixel coordinates of each point in the images
x = np.stack(np.broadcast_arrays(
np.arange(f1.shape[0])[:, None],
np.arange(f1.shape[1])
), axis=-1).astype(np.int)
# Initialize displacement field
if d is None:
d = np.zeros(list(f1.shape) + [2])
# Set up applicability convolution window
n_flow = int(4*sigma_flow + 1)
xw = np.arange(-n_flow, n_flow + 1)
w = np.exp(-xw**2 / (2 * sigma_flow**2))
# Evaluate warp parametrization model at pixel coordinates
if model == 'constant':
S = np.eye(2)
elif model in ('affine', 'eight_param'):
S = np.empty(list(x.shape) + [6 if model == 'affine' else 8])
S[..., 0, 0] = 1
S[..., 0, 1] = x[..., 0]
S[..., 0, 2] = x[..., 1]
S[..., 0, 3] = 0
S[..., 0, 4] = 0
S[..., 0, 5] = 0
S[..., 1, 0] = 0
S[..., 1, 1] = 0
S[..., 1, 2] = 0
S[..., 1, 3] = 1
S[..., 1, 4] = x[..., 0]
S[..., 1, 5] = x[..., 1]
if model == 'eight_param':
S[..., 0, 6] = x[..., 0] ** 2
S[..., 0, 7] = x[..., 0] * x[..., 1]
S[..., 1, 6] = x[..., 0] * x[..., 1]
S[..., 1, 7] = x[..., 1] ** 2
else:
raise ValueError('Invalid parametrization model')
S_T = S.swapaxes(-1, -2)
# Iterate convolutions to estimate the optical flow
for _ in range(num_iter):
# Set d~ as displacement field fit to nearest pixel (and constrain to not
# being off image). Note we are setting certainty to 0 for points that
# would have been off-image had we not constrained them
# print(d.shape, x.shape)
d_ = d.astype(np.int)
x_ = x + d_
# x_ = np.maximum(np.minimum(x_, np.array(f1.shape) - 1), 0)
# Constrain d~ to be on-image, and find points that would have
# been off-image
x_2 = np.maximum(np.minimum(x_, np.array(f1.shape) - 1), 0)
off_f = np.any(x_ != x_2, axis=-1)
x_ = x_2
# Set certainty to 0 for off-image points
c_ = c1[x_[..., 0], x_[..., 1]]
c_[off_f] = 0
# Calculate A and delB for each point, according to paper
A = (A1 + A2[x_[..., 0], x_[..., 1]]) / 2
A *= c_[..., None, None] # recommendation in paper: add in certainty by applying to A and delB
delB = -1/2 * (B2[x_[..., 0], x_[..., 1]] - B1) + (A @ d_[..., None])[..., 0]
delB *= c_[..., None] # recommendation in paper: add in certainty by applying to A and delB
# Pre-calculate quantities recommended by paper
A_T = A.swapaxes(-1, -2)
ATA = S_T @ A_T @ A @ S
ATb = (S_T @ A_T @ delB[..., None])[..., 0]
# btb = delB.swapaxes(-1, -2) @ delB
# If mu is 0, it means the global/average parametrized warp should not be
# calculated, and the parametrization should apply to the local calculations
if mu == 0:
# Apply separable cross-correlation to calculate linear equation
# for each pixel: G*d = h
G = scipy.ndimage.correlate1d(ATA, w, axis=0, mode='constant', cval=0)
G = scipy.ndimage.correlate1d(G, w, axis=1, mode='constant', cval=0)
h = scipy.ndimage.correlate1d(ATb, w, axis=0, mode='constant', cval=0)
h = scipy.ndimage.correlate1d(h, w, axis=1, mode='constant', cval=0)
d = (S @ np.linalg.solve(G, h)[..., None])[..., 0]
# if mu is not 0, it should be used to regularize the least squares problem
# and "force" the background warp onto uncertain pixels
else:
# Calculate global parametrized warp
G_avg = np.mean(ATA, axis=(0, 1))
h_avg = np.mean(ATb, axis=(0, 1))
p_avg = np.linalg.solve(G_avg, h_avg)
d_avg = (S @ p_avg[..., None])[..., 0]
# Default value for mu is to set mu to 1/2 the trace of G_avg
if mu is None:
mu = 1/2 * np.trace(G_avg)
# Apply separable cross-correlation to calculate linear equation
G = scipy.ndimage.correlate1d(A_T @ A, w, axis=0, mode='constant', cval=0)
G = scipy.ndimage.correlate1d(G, w, axis=1, mode='constant', cval=0)
h = scipy.ndimage.correlate1d((A_T @ delB[..., None])[..., 0], w, axis=0, mode='constant', cval=0)
h = scipy.ndimage.correlate1d(h, w, axis=1, mode='constant', cval=0)
# Refine estimate of displacement field
d = np.linalg.solve(G + mu*np.eye(2), h + mu*d_avg)
if l_coef > 0:
d[..., 0] = l_coef * 0 + (1 - l_coef) * d[..., 0]
d[..., 1] = l_coef * d[..., 1].mean() + (1 - l_coef) * d[..., 1]
# TODO: return global displacement parameters and/or global displacement if mu != 0
return d
def infer(f1, f2, n_pyr=3, opts=dict(
sigma=5.0,
sigma_flow=5.0,
num_iter=15,
model='constant',
mu=None,
l_coef=0.99
)):
'''
Function for efficient inference with image pyramid
f1:
first slice
f2:
second slice
n_pyr:
num pyramid layers
opts:
options for flow_iterative, contains best parameters by default
Returns
-------
Optical flow field. d[i, j] is the (y, x) displacement for pixel (i, j)
'''
c1 = np.minimum(1, 1/5*np.minimum(np.arange(f1.shape[0])[:, None], np.arange(f1.shape[1])))
c1 = np.minimum(c1, 1/5*np.minimum(
f1.shape[0] - 1 - np.arange(f1.shape[0])[:, None],
f1.shape[1] - 1 - np.arange(f1.shape[1])
))
c2 = c1
# optical flow field
d = None
# calculate optical flow using pyramids
# note: reversed(...) because we start with the smallest pyramid
for pyr1, pyr2, c1_, c2_ in reversed(list(zip(
*list(map(
partial(skimage.transform.pyramid_gaussian, max_layer=n_pyr),
[f1, f2, c1, c2]
))
))):
if d is not None:
# TODO: account for shapes not quite matching
# print(d.shape)
d = skimage.transform.pyramid_expand(d, multichannel=True)
d = d[:pyr1.shape[0], :pyr2.shape[1]]
# print(d.shape)
d = flow_iterative(pyr1, pyr2, c1=c1_, c2=c2_, d=d, **opts)
return d