-
Notifications
You must be signed in to change notification settings - Fork 0
/
EPnP.cpp
451 lines (381 loc) · 14.6 KB
/
EPnP.cpp
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
#include "EPnP.h"
EPnP::EPnP(Matrix opoint, Matrix ipoint, FLOAT _fx, FLOAT _fy, FLOAT _cx, FLOAT _cy)
{
number_of_correspondences = opoint.m;
pws = Matrix(number_of_correspondences, 3);
us = Matrix(number_of_correspondences, 2);
for (int i = 0; i < number_of_correspondences; i++)
{
for (int j = 0; j < 3; j++)
pws.val[i][j] = opoint.val[i][j];
for (int j = 0; j < 2; j++)
us.val[i][j] = ipoint.val[i][j];
}
cx = _cx;
cy = _cy;
fx = _fx;
fy = _fy;
max_nr = 0;
A1 = NULL;
A2 = NULL;
}
EPnP::~EPnP()
{
}
void EPnP::compute(Matrix &rmat, Matrix &tvec)
{
// control points in world-coord;
Matrix cws = Matrix(4, 3);
// Take C0 as the reference points centroid:
cws.val[0][0] = cws.val[0][1] = cws.val[0][2] = 0;
for (int i = 0; i < number_of_correspondences; i++)
for (int j = 0; j < 3; j++)
cws.val[0][j] += pws.val[i][j];
for (int j = 0; j < 3; j++)
cws.val[0][j] /= number_of_correspondences;
// Take C1, C2, and C3 from PCA on the reference points:
Matrix PW0 = Matrix(number_of_correspondences, 3);
for (int i = 0; i < number_of_correspondences; i++)
for (int j = 0; j < 3; j++)
PW0.val[i][j] = pws.val[i][j] - cws.val[0][j];
Matrix PW0tPW0 = PW0.multrans();
Matrix DC;
Matrix UC;
Matrix VC;
PW0tPW0.svd(UC, DC, VC);
// svd(MtM, λ, ν, ...)
PW0.releaseMemory();
for (int i = 1; i < 4; i++)
{
double k = sqrt(DC.val[i - 1][0] / number_of_correspondences);
// cout << "k " << k << endl;
for (int j = 0; j < 3; j++)
cws.val[i][j] = cws.val[0][j] + k * UC.val[j][i - 1];
}
DC.releaseMemory();
UC.releaseMemory();
VC.releaseMemory();
// compute_barycentric_coordinates();
Matrix CC = Matrix(3, 3);
for (int i = 0; i < 3; i++)
for (int j = 1; j < 4; j++)
CC.val[i][j - 1] = cws.val[j][i] - cws.val[0][i];
Matrix CC_inv = CC.inv();
alphas = Matrix(number_of_correspondences, 4);
for (int i = 0; i < number_of_correspondences; i++)
{
for (int j = 0; j < 3; j++)
{
alphas.val[i][1 + j] =
CC_inv.val[j][0] * (pws.val[i][0] - cws.val[0][0]) +
CC_inv.val[j][1] * (pws.val[i][1] - cws.val[0][1]) +
CC_inv.val[j][2] * (pws.val[i][2] - cws.val[0][2]);
}
alphas.val[i][0] = 1.0f - alphas.val[i][1] - alphas.val[i][2] - alphas.val[i][3];
}
CC.releaseMemory();
CC_inv.releaseMemory();
// fill_M ();
Matrix M = Matrix(2 * number_of_correspondences, 12);
for (int i = 0; i < number_of_correspondences; i++)
{
for (int j = 0; j < 4; j++)
{
M.val[2 * i][3 * j] = alphas.val[i][j] * fx;
M.val[2 * i][3 * j + 1] = 0.0;
M.val[2 * i][3 * j + 2] = alphas.val[i][j] * (cx - us.val[i][0]);
M.val[2 * i + 1][3 * j] = 0.0;
M.val[2 * i + 1][3 * j + 1] = alphas.val[i][j] * fy;
M.val[2 * i + 1][3 * j + 2] = alphas.val[i][j] * (cy - us.val[i][1]);
}
}
Matrix MtM = M.multrans(), DM, UM, VM;
MtM.svd(UM, DM, VM);
// compute_L_6x10();
Matrix L_6x10 = Matrix(6, 10);
Matrix Rho = Matrix(6, 1);
Matrix dv[4];
for (int i = 0; i < 4; i++)
dv[i] = Matrix(6, 3);
for (int i = 0; i < 4; i++)
{
int a = 0, b = 1;
for (int j = 0; j < 6; j++)
{
dv[i].val[j][0] = UM.val[3 * b][11 - i] - UM.val[3 * a][11 - i];
dv[i].val[j][1] = UM.val[3 * b + 1][11 - i] - UM.val[3 * a + 1][11 - i];
dv[i].val[j][2] = UM.val[3 * b + 2][11 - i] - UM.val[3 * a + 2][11 - i];
b++;
if (b > 3)
{
a++;
b = a + 1;
}
}
}
MtM.releaseMemory();
DM.releaseMemory();
VM.releaseMemory();
for (int i = 0; i < 6; i++)
{
L_6x10.val[i][0] = Matrix::dot(dv[0].val[i], dv[0].val[i]);
L_6x10.val[i][1] = 2.0f * Matrix::dot(dv[0].val[i], dv[1].val[i]);
L_6x10.val[i][2] = Matrix::dot(dv[1].val[i], dv[1].val[i]);
L_6x10.val[i][3] = 2.0f * Matrix::dot(dv[0].val[i], dv[2].val[i]);
L_6x10.val[i][4] = 2.0f * Matrix::dot(dv[1].val[i], dv[2].val[i]);
L_6x10.val[i][5] = Matrix::dot(dv[2].val[i], dv[2].val[i]);
L_6x10.val[i][6] = 2.0f * Matrix::dot(dv[0].val[i], dv[3].val[i]);
L_6x10.val[i][7] = 2.0f * Matrix::dot(dv[1].val[i], dv[3].val[i]);
L_6x10.val[i][8] = 2.0f * Matrix::dot(dv[2].val[i], dv[3].val[i]);
L_6x10.val[i][9] = Matrix::dot(dv[3].val[i], dv[3].val[i]);
}
for (int i = 0; i < 4; i++)
dv[i].releaseMemory();
// compute_rho(rho);
Rho.val[0][0] = (cws.val[0][0] - cws.val[1][0]) * (cws.val[0][0] - cws.val[1][0]) +
(cws.val[0][1] - cws.val[1][1]) * (cws.val[0][1] - cws.val[1][1]) +
(cws.val[0][2] - cws.val[1][2]) * (cws.val[0][2] - cws.val[1][2]);
Rho.val[1][0] = (cws.val[0][0] - cws.val[2][0]) * (cws.val[0][0] - cws.val[2][0]) +
(cws.val[0][1] - cws.val[2][1]) * (cws.val[0][1] - cws.val[2][1]) +
(cws.val[0][2] - cws.val[2][2]) * (cws.val[0][2] - cws.val[2][2]);
Rho.val[2][0] = (cws.val[0][0] - cws.val[3][0]) * (cws.val[0][0] - cws.val[3][0]) +
(cws.val[0][1] - cws.val[3][1]) * (cws.val[0][1] - cws.val[3][1]) +
(cws.val[0][2] - cws.val[3][2]) * (cws.val[0][2] - cws.val[3][2]);
Rho.val[3][0] = (cws.val[1][0] - cws.val[2][0]) * (cws.val[1][0] - cws.val[2][0]) +
(cws.val[1][1] - cws.val[2][1]) * (cws.val[1][1] - cws.val[2][1]) +
(cws.val[1][2] - cws.val[2][2]) * (cws.val[1][2] - cws.val[2][2]);
Rho.val[4][0] = (cws.val[1][0] - cws.val[3][0]) * (cws.val[1][0] - cws.val[3][0]) +
(cws.val[1][1] - cws.val[3][1]) * (cws.val[1][1] - cws.val[3][1]) +
(cws.val[1][2] - cws.val[3][2]) * (cws.val[1][2] - cws.val[3][2]);
Rho.val[5][0] = (cws.val[2][0] - cws.val[3][0]) * (cws.val[2][0] - cws.val[3][0]) +
(cws.val[2][1] - cws.val[3][1]) * (cws.val[2][1] - cws.val[3][1]) +
(cws.val[2][2] - cws.val[3][2]) * (cws.val[2][2] - cws.val[3][2]);
Matrix Betas = Matrix(4, 4);
double rep_errors[4] = {};
Matrix Rs[4], ts = Matrix(4, 3);
for (int i = 0; i < 4; i++)
Rs[i] = Matrix(3, 3);
// find_betas_approx_1(&L_6x10, &Rho, Betas[1]);
Matrix L_6x4 = Matrix(6, 4);
for (int i = 0; i < 6; i++)
{
L_6x4.val[i][0] = L_6x10.val[i][0];
L_6x4.val[i][1] = L_6x10.val[i][1];
L_6x4.val[i][2] = L_6x10.val[i][3];
L_6x4.val[i][3] = L_6x10.val[i][6];
}
Matrix B4;
Matrix::solve(L_6x4, B4, Rho);
Betas.val[1][0] = sqrt(fabs(B4.val[0][0]));
Betas.val[1][1] = fabs(B4.val[1][0]) / Betas.val[1][0];
Betas.val[1][2] = fabs(B4.val[2][0]) / Betas.val[1][0];
Betas.val[1][3] = fabs(B4.val[3][0]) / Betas.val[1][0];
gauss_newton(L_6x10, Rho, Betas.val[1]);
rep_errors[1] = compute_R_and_t(UM, Betas.val[1], Rs[1], ts.val[1]);
// find_betas_approx_2(&L_6x10, &Rho, Betas[2]);
Matrix L_6x3 = Matrix(6, 3);
for (int i = 0; i < 6; i++)
{
L_6x3.val[i][0] = L_6x10.val[i][0];
L_6x3.val[i][1] = L_6x10.val[i][1];
L_6x3.val[i][2] = L_6x10.val[i][2];
}
Matrix B3;
Matrix::solve(L_6x3, B3, Rho);
if (B3.val[0][0] < 0)
{
Betas.val[2][0] = sqrt(-B3.val[0][0]);
Betas.val[2][1] = (B3.val[2][0] < 0) ? sqrt(-B3.val[2][0]) : 0.0;
}
else
{
Betas.val[2][0] = sqrt(B3.val[0][0]);
Betas.val[2][1] = (B3.val[2][0] > 0) ? sqrt(B3.val[2][0]) : 0.0;
}
if (B3.val[1][0] < 0)
Betas.val[2][0] = -Betas.val[2][0];
Betas.val[2][2] = 0.0;
Betas.val[2][3] = 0.0;
gauss_newton(L_6x10, Rho, Betas.val[2]);
rep_errors[2] = compute_R_and_t(UM, Betas.val[2], Rs[2], ts.val[2]);
// find_betas_approx_3(&L_6x10, &Rho, Betas[3]);
Matrix L_6x5 = Matrix(6, 5);
for (int i = 0; i < 6; i++)
{
L_6x5.val[i][0] = L_6x10.val[i][0];
L_6x5.val[i][1] = L_6x10.val[i][1];
L_6x5.val[i][2] = L_6x10.val[i][2];
L_6x5.val[i][3] = L_6x10.val[i][3];
L_6x5.val[i][4] = L_6x10.val[i][4];
}
Matrix B5;
Matrix::solve(L_6x5, B5, Rho);
if (B5.val[0][0] < 0)
{
Betas.val[3][0] = sqrt(-B5.val[0][0]);
Betas.val[3][1] = (B5.val[2][0] < 0) ? sqrt(-B5.val[2][0]) : 0.0;
}
else
{
Betas.val[3][0] = sqrt(B5.val[0][0]);
Betas.val[3][1] = (B5.val[2][0] > 0) ? sqrt(B5.val[2][0]) : 0.0;
}
if (B5.val[1][0] < 0)
Betas.val[3][0] = -Betas.val[3][0];
Betas.val[3][2] = B5.val[3][0] / Betas.val[3][0];
Betas.val[3][3] = 0.0;
gauss_newton(L_6x10, Rho, Betas.val[3]);
rep_errors[3] = compute_R_and_t(UM, Betas.val[3], Rs[3], ts.val[3]);
int N = 1;
if (rep_errors[2] < rep_errors[1])
N = 2;
if (rep_errors[3] < rep_errors[N])
N = 3;
rmat = Matrix(3, 3);
tvec = Matrix(3, 1);
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
rmat.val[i][j] = Rs[N].val[i][j];
tvec.val[i][0] = ts.val[N][i];
}
}
FLOAT EPnP::compute_R_and_t(const Matrix &u, const FLOAT *betas,
Matrix &R, FLOAT t[3])
{
Matrix ccs = Matrix(4, 3);
pcs = Matrix(number_of_correspondences, 3);
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
for (int k = 0; k < 3; k++)
ccs.val[j][k] += betas[i] * u.val[3 * j + k][11 - i];
}
for (int i = 0; i < number_of_correspondences; i++)
{
for (int j = 0; j < 3; j++)
pcs.val[i][j] = alphas.val[i][0] * ccs.val[0][j] +
alphas.val[i][1] * ccs.val[1][j] +
alphas.val[i][2] * ccs.val[2][j] +
alphas.val[i][3] * ccs.val[3][j];
}
if (pcs.val[0][2] < 0.0)
{
for (int i = 0; i < 4; i++)
for (int j = 0; j < 3; j++)
ccs.val[i][j] = -ccs.val[i][j];
for (int i = 0; i < number_of_correspondences; i++)
{
pcs.val[i][0] = -pcs.val[i][0];
pcs.val[i][1] = -pcs.val[i][1];
pcs.val[i][2] = -pcs.val[i][2];
}
}
// estimate_R_and_t(R, t);
double pc0[3] = {}, pw0[3] = {};
pc0[0] = pc0[1] = pc0[2] = 0.0;
pw0[0] = pw0[1] = pw0[2] = 0.0;
for (int i = 0; i < number_of_correspondences; i++)
{
for (int j = 0; j < 3; j++)
{
pc0[j] += pcs.val[i][j];
pw0[j] += pws.val[i][j];
}
}
for (int j = 0; j < 3; j++)
{
pc0[j] /= number_of_correspondences;
pw0[j] /= number_of_correspondences;
}
Matrix ABt = Matrix(3, 3);
Matrix ABt_D = Matrix(3, 1);
Matrix ABt_U = Matrix(3, 3);
Matrix ABt_V = Matrix(3, 3);
for (int i = 0; i < number_of_correspondences; i++)
{
for (int j = 0; j < 3; j++)
{
ABt.val[j][0] += (pcs.val[i][j] - pc0[j]) * (pws.val[i][0] - pw0[0]);
ABt.val[j][1] += (pcs.val[i][j] - pc0[j]) * (pws.val[i][1] - pw0[1]);
ABt.val[j][2] += (pcs.val[i][j] - pc0[j]) * (pws.val[i][2] - pw0[2]);
}
}
ABt.svd(ABt_U, ABt_D, ABt_V);
R = Matrix(3, 3);
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
{
R.val[i][j] = Matrix::dot(ABt_U.val[i], ABt_V.val[j]);
}
const FLOAT det =
R.val[0][0] * R.val[1][1] * R.val[2][2] +
R.val[0][1] * R.val[1][2] * R.val[2][0] +
R.val[0][2] * R.val[1][0] * R.val[2][1] -
R.val[0][2] * R.val[1][1] * R.val[2][0] -
R.val[0][1] * R.val[1][0] * R.val[2][2] -
R.val[0][0] * R.val[1][2] * R.val[2][1];
if (det < 0)
{
R.val[2][0] = -R.val[2][0];
R.val[2][1] = -R.val[2][1];
R.val[2][2] = -R.val[2][2];
}
t[0] = pc0[0] - Matrix::dot(R.val[0], pw0);
t[1] = pc0[1] - Matrix::dot(R.val[1], pw0);
t[2] = pc0[2] - Matrix::dot(R.val[2], pw0);
double sum2 = 0.0;
for (int i = 0; i < number_of_correspondences; i++)
{
double Xc = Matrix::dot(R.val[0], pws.val[i]) + t[0];
double Yc = Matrix::dot(R.val[1], pws.val[i]) + t[1];
double inv_Zc = 1.0 / (Matrix::dot(R.val[2], pws.val[i]) + t[2]);
double ue = cx + fx * Xc * inv_Zc;
double ve = cy + fy * Yc * inv_Zc;
sum2 += sqrt((us.val[i][0] - ue) * (us.val[i][0] - ue) + (us.val[i][1] - ve) * (us.val[i][1] - ve));
}
return sum2 / number_of_correspondences;
}
void EPnP::gauss_newton(const Matrix &L_6x10, const Matrix &Rho, FLOAT betas[4])
{
const int32_t iterations_number = 5;
Matrix A = Matrix(6, 4);
Matrix B = Matrix(6, 1);
Matrix X = Matrix(4, 1);
for (int32_t k = 0; k < iterations_number; k++)
{
for (int32_t i = 0; i < 6; i++)
{
A.val[i][0] = 2 * L_6x10.val[i][0] * betas[0] +
L_6x10.val[i][1] * betas[1] +
L_6x10.val[i][3] * betas[2] +
L_6x10.val[i][6] * betas[3];
A.val[i][1] = L_6x10.val[i][1] * betas[0] +
2 * L_6x10.val[i][2] * betas[1] +
L_6x10.val[i][4] * betas[2] +
L_6x10.val[i][7] * betas[3];
A.val[i][2] = L_6x10.val[i][3] * betas[0] +
L_6x10.val[i][4] * betas[1] +
2 * L_6x10.val[i][5] * betas[2] +
L_6x10.val[i][8] * betas[3];
A.val[i][3] = L_6x10.val[i][6] * betas[0] +
L_6x10.val[i][7] * betas[1] +
L_6x10.val[i][8] * betas[2] +
2 * L_6x10.val[i][9] * betas[3];
B.val[i][0] = Rho.val[i][0] -
(L_6x10.val[i][0] * betas[0] * betas[0] +
L_6x10.val[i][1] * betas[0] * betas[1] +
L_6x10.val[i][2] * betas[1] * betas[1] +
L_6x10.val[i][3] * betas[0] * betas[2] +
L_6x10.val[i][4] * betas[1] * betas[2] +
L_6x10.val[i][5] * betas[2] * betas[2] +
L_6x10.val[i][6] * betas[0] * betas[3] +
L_6x10.val[i][7] * betas[1] * betas[3] +
L_6x10.val[i][8] * betas[2] * betas[3] +
L_6x10.val[i][9] * betas[3] * betas[3]);
}
Matrix::solve(A, X, B);
for (int32_t i = 0; i < 4; i++)
betas[i] += X.val[i][0];
}
}