-
Notifications
You must be signed in to change notification settings - Fork 10
/
methods.py
367 lines (284 loc) · 11.9 KB
/
methods.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2018 Hiroaki Santo
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
import warnings
import numpy as np
import utils
XTOL_BA = 1.0e-8
MAXFEV = int(1e5)
IRLS_TOL = 1.0e-8
IRLS_ITER = int(1e5)
def solution_near_linear(projected_points, Rs, tvecs):
"""
Solve Eq. (7) and (10)
:param np.ndarray projected_points: shadow positions
:param np.ndarray Rs: board poses
:param np.ndarray tvecs: board poses
:return: dict of estimated result
"""
pose_num, pin_num, _ = projected_points.shape
assert Rs.shape == (pose_num, 3, 3)
assert tvecs.shape == (pose_num, 3)
def __Ab_ij(R_, t_, s_):
"""
:param np.ndarray R_: board pose
:param np.ndarray t_: board pose
:param np.ndarray s_: shadow position
:return: A_ij anb b_ij in Eq. (7)
"""
assert R_.shape == (3, 3)
assert s_.shape == (3,)
assert t_.shape == (3,)
R_ = R_.T
t_ = -R_.dot(t_)
#
R_ = R_.reshape(-1)
A_ = np.zeros(shape=(3, 15))
b_ = np.zeros(shape=3)
A_[:, 0] = (R_[6] * s_[1], -R_[6] * s_[0], R_[3] * s_[0] - R_[0] * s_[1])
A_[:, 1] = (R_[7] * s_[1], -R_[7] * s_[0], R_[4] * s_[0] - R_[1] * s_[1])
A_[:, 2] = (R_[8] * s_[1], -R_[8] * s_[0], R_[5] * s_[0] - R_[2] * s_[1])
A_[:, 3] = (0, t_[2], -t_[1] + s_[1])
A_[:, 4] = (-t_[2], 0, t_[0] - s_[0])
A_[:, 5] = (t_[1] - s_[1], -t_[0] + s_[0], 0)
A_[:, 6] = (0, R_[6], -R_[3])
A_[:, 7] = (-R_[6], 0, R_[0])
A_[:, 8] = (R_[3], -R_[0], 0)
A_[:, 9] = (0, R_[7], -R_[4])
A_[:, 10] = (-R_[7], 0, R_[1])
A_[:, 11] = (R_[4], -R_[1], 0)
A_[:, 12] = (0, R_[8], -R_[5])
A_[:, 13] = (-R_[8], 0, R_[2])
A_[:, 14] = (R_[5], -R_[2], 0)
b_[:] = (-t_[2] * s_[1],
t_[2] * s_[0],
-t_[1] * s_[0] + t_[0] * s_[1])
return A_, b_
def __x2light_pin(x):
"""
extract light coordinates and pin positions from \theta_j in Eq. (7)
:param x:
:return: light and pin positions
"""
x = np.array(x).flatten()
assert x.shape == (3 + 12 * pin_num,), (x.shape, (3 + 12 * pin_num))
lc = np.ones(3)
lc[0:3] = x[0:3]
#
pins = np.zeros(shape=(pin_num, 3))
for p in range(pin_num):
pins[p, :] = x[3 + 12 * p:3 + 12 * p + 3]
return lc, pins
Qc = np.zeros(shape=(3 * pose_num * pin_num, 3 + 12 * pin_num))
Bc = np.zeros(shape=(3 * pose_num * pin_num))
for i in range(pose_num):
for p in range(pin_num):
A, b = __Ab_ij(Rs[i], tvecs[i], projected_points[i, p])
Qc[3 * pose_num * p + 3 * i:3 * pose_num * p + 3 * i + 3, 0:3] = A[:, 0:3]
Qc[3 * pose_num * p + 3 * i:3 * pose_num * p + 3 * i + 3, 3 + 12 * p:3 + 12 * p + 12] = A[:, 3:]
Bc[3 * pose_num * p + 3 * i:3 * pose_num * p + 3 * i + 3] = b
from L1_solver.L1_residual_min import L1_residual_min
X = L1_residual_min(np.matrix(Qc), np.matrix(Bc).T, MAX_ITER=IRLS_ITER, tol=IRLS_TOL)
X = np.array(X)
lc, P = __x2light_pin(X)
# calculate l_i from l, R_i and t_i
L = np.zeros(shape=(pose_num, 3))
for l in range(pose_num):
R = np.matrix(Rs[l]).T
t = -R.dot(tvecs[l]).flatten()
L[l, :] = R.dot(lc.flatten()) + t
res = utils.error_reprojection_near(projected_points, L, P)
result = {}
result["best_global_position"] = lc
result["L"] = L
result["P"] = P
result["indices"] = np.arange(pose_num)
result["res"] = res
return result
def solution_distant_linear(projected_points, Rs, tvecs=None):
pose_num, pin_num, _ = projected_points.shape
assert Rs.shape == (pose_num, 3, 3), Rs.shape
def __Ab_ij(R_, s_):
assert R_.shape == (3, 3), R_.shape
assert s_.shape == (3,), s_.shape
R_ = R_.T.reshape(-1)
A_ = np.zeros(shape=(3, 11))
b_ = np.zeros(shape=3)
A_[0, :] = (R_[6] * s_[1], R_[7] * s_[1], 0, -R_[8], R_[5], 0, -R_[6], R_[3], 0, -R_[7], R_[4],)
A_[1, :] = (
-R_[6] * s_[0], -R_[7] * s_[0], R_[8], 0, -R_[2], R_[6], 0, -R_[0], R_[7], 0, -R_[1],)
A_[2, :] = (
R_[3] * s_[0] - R_[0] * s_[1], R_[4] * s_[0] - R_[1] * s_[1], -R_[5], R_[2], 0, -R_[3], R_[0], 0, -R_[4],
R_[1], 0)
b_[:] = (R_[8] * s_[1], -R_[8] * s_[0], R_[5] * s_[0] - R_[2] * s_[1])
b_ = -b_
return A_, b_
def __x2light_pin(x):
assert x.shape == (2 + 9 * pin_num,), (x.shape, (2 + 9 * pin_num))
lc = np.ones(3)
lc[0:2] = x[0:2]
#
pins = np.zeros(shape=(pin_num, 3))
for p in range(pin_num):
pins[p, :] = x[2 + 9 * p:2 + 9 * p + 3]
return lc, pins
Qc = np.zeros(shape=(3 * pose_num * pin_num, 2 + 9 * pin_num))
Bc = np.zeros(shape=(3 * pose_num * pin_num))
for i in range(pose_num):
for p in range(pin_num):
A, b = __Ab_ij(Rs[i], projected_points[i, p])
Qc[3 * pose_num * p + 3 * i:3 * pose_num * p + 3 * i + 3, 0:2] = A[:, 0:2]
Qc[3 * pose_num * p + 3 * i:3 * pose_num * p + 3 * i + 3, 2 + 9 * p:2 + 9 * p + 9] = A[:, 2:]
Bc[3 * pose_num * p + 3 * i:3 * pose_num * p + 3 * i + 3] = b
from L1_solver.L1_residual_min import L1_residual_min
X = L1_residual_min(np.matrix(Qc), np.matrix(Bc).T, MAX_ITER=IRLS_ITER, tol=IRLS_TOL)
X = np.array(X).flatten()
lc, P = __x2light_pin(X)
#
L = np.zeros(shape=(pose_num, 3))
for l in range(pose_num):
L[l, :] = Rs[l].T.dot(lc)
L[l, :] /= L[l, 2]
res = utils.error_reprojection_distant(projected_points, L, P)
result = {}
result["best_global_position"] = lc
result["L"] = L
result["P"] = P
result["res"] = res
return result
def solve_unified(projected_points, Rs, tvecs, init_L, init_P):
pose_num, pin_num, _ = projected_points.shape
assert Rs.shape == (pose_num, 3, 3)
assert tvecs.shape == (pose_num, 3)
def _fit_func(params, _):
param_P = params[:pin_num * 3].reshape(pin_num, 3)
param_global_light_position = params[pin_num * 3:]
L = np.zeros(shape=(pose_num, 3), dtype=params.dtype)
for l in range(pose_num):
L[l, :] = Rs[l].T.dot(param_global_light_position) - Rs[l].T.dot(tvecs[l])
res = projected_points.astype(params.dtype) - utils.project_unified(L, param_P)
return np.r_[res.flatten(),].flatten()
init_param = np.ones(shape=(pin_num * 3 + 3))
P = init_param[:pin_num * 3].reshape(pin_num, 3)
global_light_position = init_param[pin_num * 3:]
######################################################
P[:] = init_P
global_light_position[:] = Rs[0].dot(init_L[0, :]) + tvecs[0]
#######################################################
from scipy.optimize import leastsq
x, cov_x, infodict, mesg, ier = leastsq(_fit_func, init_param.reshape(-1),
args=[],
xtol=XTOL_BA, maxfev=MAXFEV, full_output=True)
if ier not in [1, 2, 3, 4]:
warnings.warn("Solution not found: ier:{}, {}".format(ier, mesg))
res = np.linalg.norm(infodict["fvec"])
P = x[:pin_num * 3].reshape(pin_num, 3).astype(np.float64)
global_light_position = x[pin_num * 3:].astype(np.float64)
L = np.zeros(shape=(pose_num, 3))
for l in range(pose_num):
L[l, :] = Rs[l].T.dot(global_light_position) - Rs[l].T.dot(tvecs[l])
res = utils.error_reprojection(projected_points, L, P)
res = np.mean(res)
result = {}
result["best_global_position"] = global_light_position
result["L"] = L
result["P"] = P
result["res"] = res
return result
def solve_near(projected_points, Rs, tvecs, init_L, init_P):
"""
Solve Eq. (5) in near light case
:param np.ndarray projected_points:
:param np.ndarray Rs:
:param np.ndarray tvecs:
:param np.ndarray init_L: initial estimation for light positions (all of l_i)
:param np.ndarray init_P:initial estimation for pin positions
:return: dict of estimated result
"""
pose_num, pin_num, _ = projected_points.shape
assert Rs.shape == (pose_num, 3, 3)
assert tvecs.shape == (pose_num, 3)
def _fit_func(params, _):
"""
objective function for bundle adjustment
"""
param_P = params[:pin_num * 3].reshape(pin_num, 3)
param_global_light_position = params[pin_num * 3:]
L = np.zeros(shape=(pose_num, 3))
for l in range(pose_num):
L[l, :] = Rs[l].T.dot(param_global_light_position) - Rs[l].T.dot(tvecs[l])
res = projected_points - utils.project_near(L, param_P)
return np.r_[res.flatten(),].flatten()
# variables for optimization
init_param = np.ones(shape=(pin_num * 3 + 3))
P = init_param[:pin_num * 3].reshape(pin_num, 3)
global_light_position = init_param[pin_num * 3:]
# set initial estimation
P[:] = init_P
global_light_position[:] = Rs[0].dot(init_L[0, :]) + tvecs[0]
# run optimization
from scipy.optimize import leastsq
x, cov_x, infodict, mesg, ier = leastsq(_fit_func, init_param.reshape(-1),
args=[],
xtol=XTOL_BA, maxfev=MAXFEV, full_output=True)
if ier not in [1, 2, 3, 4]:
warnings.warn("Solution not found: ier:{}, {}".format(ier, mesg))
res = np.linalg.norm(infodict["fvec"])
P = x[:pin_num * 3].reshape(pin_num, 3)
global_light_position = x[pin_num * 3:]
# calculate l_i from l, R_i and t_i
L = np.zeros(shape=(pose_num, 3))
for l in range(pose_num):
L[l, :] = Rs[l].T.dot(global_light_position) - Rs[l].T.dot(tvecs[l])
# calculate reprojection error
res = utils.error_reprojection_near(projected_points, L, P)
# store result to dict
result = {}
result["best_global_position"] = global_light_position
result["L"] = L
result["P"] = P
result["res"] = res
return result
def solve_distant(projected_points, Rs, tvecs, init_L, init_P):
pose_num, pin_num, _ = projected_points.shape
assert Rs.shape == (pose_num, 3, 3), Rs.shape
assert tvecs.shape == (pose_num, 3), tvecs.shape
assert init_L.shape == (pose_num, 3), init_L
assert init_P.shape == (pin_num, 3), init_P
def _fit_func(params, _):
param_P = params[:pin_num * 3].reshape(pin_num, 3)
param_global_light_position = params[pin_num * 3:]
L = np.zeros(shape=(pose_num, 3))
for l in range(pose_num):
L[l, :] = Rs[l].T.dot(param_global_light_position)
res = projected_points - utils.project_distant(L, param_P)
return np.r_[res.flatten()].flatten()
init_param = np.ones(shape=(pin_num * 3 + 3))
P = init_param[:pin_num * 3].reshape(pin_num, 3)
global_light_position = init_param[pin_num * 3:]
P[:] = init_P
global_light_position[:] = Rs[0].dot(init_L[0])
from scipy.optimize import leastsq
x, cov_x, infodict, mesg, ier = leastsq(_fit_func, init_param.reshape(-1),
args=[],
xtol=XTOL_BA, maxfev=MAXFEV, full_output=True)
if ier not in [1, 2, 3, 4]:
warnings.warn("Solution not found: ier:{}, {}".format(ier, mesg))
P = x[:pin_num * 3].reshape(pin_num, 3)
global_light_position = x[pin_num * 3:]
global_light_position /= global_light_position[2]
L = np.zeros(shape=(pose_num, 3))
for l in range(pose_num):
L[l, :] = Rs[l].T.dot(global_light_position)
L[l, :] /= L[l, 2]
res = utils.error_reprojection_distant(projected_points, L, P)
result = {}
result["best_global_position"] = global_light_position
result["L"] = L
result["P"] = P
result["res"] = res
return result