11 #include <mrpt/config.h>
16 #include <mrpt/config.h>
17 #if MRPT_HAS_OPENCV && MRPT_OPENCV_VERSION_NUM < 0x240
18 #undef MRPT_HAS_OPENCV
19 #define MRPT_HAS_OPENCV 0
23 #include <mrpt/otherlibs/do_opencv_includes.h>
29 const cv::Mat& cameraMatrix,
const cv::Mat& opoints,
const cv::Mat& ipoints)
31 if (cameraMatrix.depth() == CV_32F)
32 init_camera_parameters<float>(cameraMatrix);
34 init_camera_parameters<double>(cameraMatrix);
36 number_of_correspondences = std::max(
37 opoints.checkVector(3, CV_32F), opoints.checkVector(3, CV_64F));
39 pws.resize(3 * number_of_correspondences);
40 us.resize(2 * number_of_correspondences);
42 if (opoints.depth() == ipoints.depth())
44 if (opoints.depth() == CV_32F)
45 init_points<float, float>(opoints, ipoints);
47 init_points<double, double>(opoints, ipoints);
49 else if (opoints.depth() == CV_32F)
50 init_points<float, double>(opoints, ipoints);
52 init_points<double, float>(opoints, ipoints);
54 alphas.resize(4 * number_of_correspondences);
55 pcs.resize(3 * number_of_correspondences);
71 cws[0][0] = cws[0][1] = cws[0][2] = 0;
72 for (
int i = 0; i < number_of_correspondences; i++)
73 for (
int j = 0; j < 3; j++) cws[0][j] += pws[3 * i + j];
75 for (
int j = 0; j < 3; j++) cws[0][j] /= number_of_correspondences;
78 CvMat* PW0 = cvCreateMat(number_of_correspondences, 3, CV_64F);
80 double pw0tpw0[3 * 3], dc[3], uct[3 * 3];
81 CvMat PW0tPW0 = cvMat(3, 3, CV_64F, pw0tpw0);
82 CvMat DC = cvMat(3, 1, CV_64F, dc);
83 CvMat UCt = cvMat(3, 3, CV_64F, uct);
85 for (
int i = 0; i < number_of_correspondences; i++)
86 for (
int j = 0; j < 3; j++)
87 PW0->data.db[3 * i + j] = pws[3 * i + j] - cws[0][j];
89 cvMulTransposed(PW0, &PW0tPW0, 1);
90 cvSVD(&PW0tPW0, &DC, &UCt, 0, CV_SVD_MODIFY_A | CV_SVD_U_T);
94 for (
int i = 1; i < 4; i++)
96 double k = sqrt(dc[i - 1] / number_of_correspondences);
97 for (
int j = 0; j < 3; j++)
98 cws[i][j] = cws[0][j] + k * uct[3 * (i - 1) + j];
104 double cc[3 * 3], cc_inv[3 * 3];
105 CvMat CC = cvMat(3, 3, CV_64F, cc);
106 CvMat CC_inv = cvMat(3, 3, CV_64F, cc_inv);
108 for (
int i = 0; i < 3; i++)
109 for (
int j = 1; j < 4; j++) cc[3 * i + j - 1] = cws[j][i] - cws[0][i];
111 cvInvert(&CC, &CC_inv, CV_SVD);
113 for (
int i = 0; i < number_of_correspondences; i++)
115 double* pi = &pws[0] + 3 * i;
116 double*
a = &alphas[0] + 4 * i;
118 for (
int j = 0; j < 3; j++)
119 a[1 + j] = ci[3 * j] * (pi[0] - cws[0][0]) +
120 ci[3 * j + 1] * (pi[1] - cws[0][1]) +
121 ci[3 * j + 2] * (pi[2] - cws[0][2]);
122 a[0] = 1.0f -
a[1] -
a[2] -
a[3];
127 CvMat* M,
const int row,
const double* as,
const double u,
const double v)
129 double* M1 = M->data.db +
row * 12;
130 double* M2 = M1 + 12;
132 for (
int i = 0; i < 4; i++)
134 M1[3 * i] = as[i] * fu;
136 M1[3 * i + 2] = as[i] * (uc - u);
139 M2[3 * i + 1] = as[i] * fv;
140 M2[3 * i + 2] = as[i] * (vc -
v);
146 for (
int i = 0; i < 4; i++) ccs[i][0] = ccs[i][1] = ccs[i][2] = 0.0f;
148 for (
int i = 0; i < 4; i++)
150 const double*
v = ut + 12 * (11 - i);
151 for (
int j = 0; j < 4; j++)
152 for (
int k = 0; k < 3; k++) ccs[j][k] += betas[i] *
v[3 * j + k];
158 for (
int i = 0; i < number_of_correspondences; i++)
160 double*
a = &alphas[0] + 4 * i;
161 double* pc = &pcs[0] + 3 * i;
163 for (
int j = 0; j < 3; j++)
164 pc[j] =
a[0] * ccs[0][j] +
a[1] * ccs[1][j] +
a[2] * ccs[2][j] +
171 choose_control_points();
172 compute_barycentric_coordinates();
174 CvMat* M = cvCreateMat(2 * number_of_correspondences, 12, CV_64F);
176 for (
int i = 0; i < number_of_correspondences; i++)
177 fill_M(M, 2 * i, &alphas[0] + 4 * i, us[2 * i], us[2 * i + 1]);
179 double mtm[12 * 12], d[12], ut[12 * 12];
180 CvMat MtM = cvMat(12, 12, CV_64F, mtm);
181 CvMat D = cvMat(12, 1, CV_64F, d);
182 CvMat Ut = cvMat(12, 12, CV_64F, ut);
184 cvMulTransposed(M, &MtM, 1);
185 cvSVD(&MtM, &D, &Ut, 0, CV_SVD_MODIFY_A | CV_SVD_U_T);
188 double l_6x10[6 * 10], rho[6];
189 CvMat L_6x10 = cvMat(6, 10, CV_64F, l_6x10);
190 CvMat Rho = cvMat(6, 1, CV_64F, rho);
192 compute_L_6x10(ut, l_6x10);
195 double Betas[4][4], rep_errors[4];
196 double Rs[4][3][3], ts[4][3];
198 find_betas_approx_1(&L_6x10, &Rho, Betas[1]);
199 gauss_newton(&L_6x10, &Rho, Betas[1]);
200 rep_errors[1] = compute_R_and_t(ut, Betas[1], Rs[1], ts[1]);
202 find_betas_approx_2(&L_6x10, &Rho, Betas[2]);
203 gauss_newton(&L_6x10, &Rho, Betas[2]);
204 rep_errors[2] = compute_R_and_t(ut, Betas[2], Rs[2], ts[2]);
206 find_betas_approx_3(&L_6x10, &Rho, Betas[3]);
207 gauss_newton(&L_6x10, &Rho, Betas[3]);
208 rep_errors[3] = compute_R_and_t(ut, Betas[3], Rs[3], ts[3]);
211 if (rep_errors[2] < rep_errors[1]) N = 2;
212 if (rep_errors[3] < rep_errors[N]) N = 3;
214 cv::Mat(3, 1, CV_64F, ts[N]).copyTo(
t);
215 cv::Mat(3, 3, CV_64F, Rs[N]).copyTo(
R);
219 const double R_src[3][3],
const double t_src[3],
double R_dst[3][3],
222 for (
int i = 0; i < 3; i++)
224 for (
int j = 0; j < 3; j++) R_dst[i][j] = R_src[i][j];
231 return (p1[0] - p2[0]) * (p1[0] - p2[0]) +
232 (p1[1] - p2[1]) * (p1[1] - p2[1]) +
233 (p1[2] - p2[2]) * (p1[2] - p2[2]);
243 double pc0[3], pw0[3];
245 pc0[0] = pc0[1] = pc0[2] = 0.0;
246 pw0[0] = pw0[1] = pw0[2] = 0.0;
248 for (
int i = 0; i < number_of_correspondences; i++)
250 const double* pc = &pcs[3 * i];
251 const double* pw = &pws[3 * i];
253 for (
int j = 0; j < 3; j++)
259 for (
int j = 0; j < 3; j++)
261 pc0[j] /= number_of_correspondences;
262 pw0[j] /= number_of_correspondences;
265 double abt[3 * 3], abt_d[3], abt_u[3 * 3], abt_v[3 * 3];
266 CvMat ABt = cvMat(3, 3, CV_64F, abt);
267 CvMat ABt_D = cvMat(3, 1, CV_64F, abt_d);
268 CvMat ABt_U = cvMat(3, 3, CV_64F, abt_u);
269 CvMat ABt_V = cvMat(3, 3, CV_64F, abt_v);
272 for (
int i = 0; i < number_of_correspondences; i++)
274 double* pc = &pcs[3 * i];
275 double* pw = &pws[3 * i];
277 for (
int j = 0; j < 3; j++)
279 abt[3 * j] += (pc[j] - pc0[j]) * (pw[0] - pw0[0]);
280 abt[3 * j + 1] += (pc[j] - pc0[j]) * (pw[1] - pw0[1]);
281 abt[3 * j + 2] += (pc[j] - pc0[j]) * (pw[2] - pw0[2]);
285 cvSVD(&ABt, &ABt_D, &ABt_U, &ABt_V, CV_SVD_MODIFY_A);
287 for (
int i = 0; i < 3; i++)
288 for (
int j = 0; j < 3; j++)
R[i][j] = dot(abt_u + 3 * i, abt_v + 3 * j);
291 R[0][0] *
R[1][1] *
R[2][2] +
R[0][1] *
R[1][2] *
R[2][0] +
292 R[0][2] *
R[1][0] *
R[2][1] -
R[0][2] *
R[1][1] *
R[2][0] -
293 R[0][1] *
R[1][0] *
R[2][2] -
R[0][0] *
R[1][2] *
R[2][1];
302 t[0] = pc0[0] - dot(
R[0], pw0);
303 t[1] = pc0[1] - dot(
R[1], pw0);
304 t[2] = pc0[2] - dot(
R[2], pw0);
311 for (
int i = 0; i < 4; i++)
312 for (
int j = 0; j < 3; j++) ccs[i][j] = -ccs[i][j];
314 for (
int i = 0; i < number_of_correspondences; i++)
316 pcs[3 * i] = -pcs[3 * i];
317 pcs[3 * i + 1] = -pcs[3 * i + 1];
318 pcs[3 * i + 2] = -pcs[3 * i + 2];
324 const double* ut,
const double* betas,
double R[3][3],
double t[3])
326 compute_ccs(betas, ut);
331 estimate_R_and_t(
R,
t);
333 return reprojection_error(
R,
t);
337 const double R[3][3],
const double t[3])
341 for (
int i = 0; i < number_of_correspondences; i++)
343 double* pw = &pws[3 * i];
344 double Xc = dot(
R[0], pw) +
t[0];
345 double Yc = dot(
R[1], pw) +
t[1];
346 double inv_Zc = 1.0 / (dot(
R[2], pw) +
t[2]);
347 double ue = uc + fu * Xc * inv_Zc;
348 double ve = vc + fv * Yc * inv_Zc;
349 double u = us[2 * i],
v = us[2 * i + 1];
351 sum2 += sqrt((u - ue) * (u - ue) + (
v - ve) * (
v - ve));
354 return sum2 / number_of_correspondences;
361 const CvMat* L_6x10,
const CvMat* Rho,
double* betas)
363 double l_6x4[6 * 4], b4[4];
364 CvMat L_6x4 = cvMat(6, 4, CV_64F, l_6x4);
365 CvMat B4 = cvMat(4, 1, CV_64F, b4);
367 for (
int i = 0; i < 6; i++)
369 cvmSet(&L_6x4, i, 0, cvmGet(L_6x10, i, 0));
370 cvmSet(&L_6x4, i, 1, cvmGet(L_6x10, i, 1));
371 cvmSet(&L_6x4, i, 2, cvmGet(L_6x10, i, 3));
372 cvmSet(&L_6x4, i, 3, cvmGet(L_6x10, i, 6));
375 cvSolve(&L_6x4, Rho, &B4, CV_SVD);
379 betas[0] = sqrt(-b4[0]);
380 betas[1] = -b4[1] / betas[0];
381 betas[2] = -b4[2] / betas[0];
382 betas[3] = -b4[3] / betas[0];
386 betas[0] = sqrt(b4[0]);
387 betas[1] = b4[1] / betas[0];
388 betas[2] = b4[2] / betas[0];
389 betas[3] = b4[3] / betas[0];
397 const CvMat* L_6x10,
const CvMat* Rho,
double* betas)
399 double l_6x3[6 * 3],
b3[3];
400 CvMat L_6x3 = cvMat(6, 3, CV_64F, l_6x3);
401 CvMat B3 = cvMat(3, 1, CV_64F,
b3);
403 for (
int i = 0; i < 6; i++)
405 cvmSet(&L_6x3, i, 0, cvmGet(L_6x10, i, 0));
406 cvmSet(&L_6x3, i, 1, cvmGet(L_6x10, i, 1));
407 cvmSet(&L_6x3, i, 2, cvmGet(L_6x10, i, 2));
410 cvSolve(&L_6x3, Rho, &B3, CV_SVD);
414 betas[0] = sqrt(-
b3[0]);
415 betas[1] = (
b3[2] < 0) ? sqrt(-
b3[2]) : 0.0;
419 betas[0] = sqrt(
b3[0]);
420 betas[1] = (
b3[2] > 0) ? sqrt(
b3[2]) : 0.0;
423 if (
b3[1] < 0) betas[0] = -betas[0];
433 const CvMat* L_6x10,
const CvMat* Rho,
double* betas)
435 double l_6x5[6 * 5], b5[5];
436 CvMat L_6x5 = cvMat(6, 5, CV_64F, l_6x5);
437 CvMat B5 = cvMat(5, 1, CV_64F, b5);
439 for (
int i = 0; i < 6; i++)
441 cvmSet(&L_6x5, i, 0, cvmGet(L_6x10, i, 0));
442 cvmSet(&L_6x5, i, 1, cvmGet(L_6x10, i, 1));
443 cvmSet(&L_6x5, i, 2, cvmGet(L_6x10, i, 2));
444 cvmSet(&L_6x5, i, 3, cvmGet(L_6x10, i, 3));
445 cvmSet(&L_6x5, i, 4, cvmGet(L_6x10, i, 4));
448 cvSolve(&L_6x5, Rho, &B5, CV_SVD);
452 betas[0] = sqrt(-b5[0]);
453 betas[1] = (b5[2] < 0) ? sqrt(-b5[2]) : 0.0;
457 betas[0] = sqrt(b5[0]);
458 betas[1] = (b5[2] > 0) ? sqrt(b5[2]) : 0.0;
460 if (b5[1] < 0) betas[0] = -betas[0];
461 betas[2] = b5[3] / betas[0];
476 for (
int i = 0; i < 4; i++)
479 for (
int j = 0; j < 6; j++)
481 dv[i][j][0] =
v[i][3 *
a] -
v[i][3 *
b];
482 dv[i][j][1] =
v[i][3 *
a + 1] -
v[i][3 *
b + 1];
483 dv[i][j][2] =
v[i][3 *
a + 2] -
v[i][3 *
b + 2];
494 for (
int i = 0; i < 6; i++)
496 double*
row = l_6x10 + 10 * i;
498 row[0] = dot(dv[0][i], dv[0][i]);
499 row[1] = 2.0f * dot(dv[0][i], dv[1][i]);
500 row[2] = dot(dv[1][i], dv[1][i]);
501 row[3] = 2.0f * dot(dv[0][i], dv[2][i]);
502 row[4] = 2.0f * dot(dv[1][i], dv[2][i]);
503 row[5] = dot(dv[2][i], dv[2][i]);
504 row[6] = 2.0f * dot(dv[0][i], dv[3][i]);
505 row[7] = 2.0f * dot(dv[1][i], dv[3][i]);
506 row[8] = 2.0f * dot(dv[2][i], dv[3][i]);
507 row[9] = dot(dv[3][i], dv[3][i]);
513 rho[0] = dist2(cws[0], cws[1]);
514 rho[1] = dist2(cws[0], cws[2]);
515 rho[2] = dist2(cws[0], cws[3]);
516 rho[3] = dist2(cws[1], cws[2]);
517 rho[4] = dist2(cws[1], cws[3]);
518 rho[5] = dist2(cws[2], cws[3]);
522 const double* l_6x10,
const double* rho,
const double betas[4], CvMat* A,
525 for (
int i = 0; i < 6; i++)
527 const double* rowL = l_6x10 + i * 10;
528 double* rowA = A->data.db + i * 4;
530 rowA[0] = 2 * rowL[0] * betas[0] + rowL[1] * betas[1] +
531 rowL[3] * betas[2] + rowL[6] * betas[3];
532 rowA[1] = rowL[1] * betas[0] + 2 * rowL[2] * betas[1] +
533 rowL[4] * betas[2] + rowL[7] * betas[3];
534 rowA[2] = rowL[3] * betas[0] + rowL[4] * betas[1] +
535 2 * rowL[5] * betas[2] + rowL[8] * betas[3];
536 rowA[3] = rowL[6] * betas[0] + rowL[7] * betas[1] + rowL[8] * betas[2] +
537 2 * rowL[9] * betas[3];
542 (rowL[0] * betas[0] * betas[0] + rowL[1] * betas[0] * betas[1] +
543 rowL[2] * betas[1] * betas[1] + rowL[3] * betas[0] * betas[2] +
544 rowL[4] * betas[1] * betas[2] + rowL[5] * betas[2] * betas[2] +
545 rowL[6] * betas[0] * betas[3] + rowL[7] * betas[1] * betas[3] +
546 rowL[8] * betas[2] * betas[3] +
547 rowL[9] * betas[3] * betas[3]));
552 const CvMat* L_6x10,
const CvMat* Rho,
double betas[4])
554 const int iterations_number = 5;
556 double a[6 * 4],
b[6],
x[4];
557 CvMat A = cvMat(6, 4, CV_64F,
a);
558 CvMat B = cvMat(6, 1, CV_64F,
b);
559 CvMat X = cvMat(4, 1, CV_64F,
x);
561 for (
int k = 0; k < iterations_number; k++)
563 compute_A_and_b_gauss_newton(
564 L_6x10->data.db, Rho->data.db, betas, &A, &B);
565 qr_solve(&A, &B, &X);
566 for (
int i = 0; i < 4; i++) betas[i] +=
x[i];
572 const int nr = A->rows;
573 const int nc = A->cols;
575 if (max_nr != 0 && max_nr < nr)
587 double *pA = A->data.db, *ppAkk = pA;
588 for (
int k = 0; k < nc; k++)
590 double *ppAik1 = ppAkk, eta = fabs(*ppAik1);
591 for (
int i = k + 1; i < nr; i++)
593 double elt = fabs(*ppAik1);
594 if (eta < elt) eta = elt;
606 double *ppAik2 = ppAkk, sum2 = 0.0, inv_eta = 1. / eta;
607 for (
int i = k; i < nr; i++)
610 sum2 += *ppAik2 * *ppAik2;
613 double sigma = sqrt(sum2);
614 if (*ppAkk < 0) sigma = -sigma;
616 A1[k] = sigma * *ppAkk;
617 A2[k] = -eta * sigma;
618 for (
int j = k + 1; j < nc; j++)
620 double *ppAik = ppAkk,
sum = 0;
621 for (
int i = k; i < nr; i++)
623 sum += *ppAik * ppAik[j - k];
626 double tau =
sum /
A1[k];
628 for (
int i = k; i < nr; i++)
630 ppAik[j - k] -= tau * *ppAik;
639 double *ppAjj = pA, *pb =
b->data.db;
640 for (
int j = 0; j < nc; j++)
642 double *ppAij = ppAjj, tau = 0;
643 for (
int i = j; i < nr; i++)
645 tau += *ppAij * pb[i];
650 for (
int i = j; i < nr; i++)
652 pb[i] -= tau * *ppAij;
659 double* pX = X->data.db;
660 pX[nc - 1] = pb[nc - 1] / A2[nc - 1];
661 for (
int i = nc - 2; i >= 0; i--)
663 double *ppAij = pA + i * nc + (i + 1),
sum = 0;
665 for (
int j = i + 1; j < nc; j++)
667 sum += *ppAij * pX[j];
670 pX[i] = (pb[i] -
sum) / A2[i];