10 #include <mrpt/config.h> 15 #include <mrpt/otherlibs/do_opencv_includes.h> 21 const cv::Mat& cameraMatrix,
const cv::Mat& opoints,
const cv::Mat& ipoints)
23 if (cameraMatrix.depth() == CV_32F)
24 init_camera_parameters<float>(cameraMatrix);
26 init_camera_parameters<double>(cameraMatrix);
28 number_of_correspondences = std::max(
29 opoints.checkVector(3, CV_32F), opoints.checkVector(3, CV_64F));
31 pws.resize(3 * number_of_correspondences);
32 us.resize(2 * number_of_correspondences);
34 if (opoints.depth() == ipoints.depth())
36 if (opoints.depth() == CV_32F)
37 init_points<float, float>(opoints, ipoints);
39 init_points<double, double>(opoints, ipoints);
41 else if (opoints.depth() == CV_32F)
42 init_points<float, double>(opoints, ipoints);
44 init_points<double, float>(opoints, ipoints);
46 alphas.resize(4 * number_of_correspondences);
47 pcs.resize(3 * number_of_correspondences);
63 cws[0][0] = cws[0][1] = cws[0][2] = 0;
64 for (
int i = 0; i < number_of_correspondences; i++)
65 for (
int j = 0; j < 3; j++) cws[0][j] += pws[3 * i + j];
67 for (
int j = 0; j < 3; j++) cws[0][j] /= number_of_correspondences;
70 CvMat* PW0 = cvCreateMat(number_of_correspondences, 3, CV_64F);
72 double pw0tpw0[3 * 3], dc[3], uct[3 * 3];
73 CvMat PW0tPW0 = cvMat(3, 3, CV_64F, pw0tpw0);
74 CvMat DC = cvMat(3, 1, CV_64F, dc);
75 CvMat UCt = cvMat(3, 3, CV_64F, uct);
77 for (
int i = 0; i < number_of_correspondences; i++)
78 for (
int j = 0; j < 3; j++)
79 PW0->data.db[3 * i + j] = pws[3 * i + j] - cws[0][j];
81 cvMulTransposed(PW0, &PW0tPW0, 1);
82 cvSVD(&PW0tPW0, &DC, &UCt,
nullptr, CV_SVD_MODIFY_A | CV_SVD_U_T);
86 for (
int i = 1; i < 4; i++)
88 double k = sqrt(dc[i - 1] / number_of_correspondences);
89 for (
int j = 0; j < 3; j++)
90 cws[i][j] = cws[0][j] + k * uct[3 * (i - 1) + j];
96 double cc[3 * 3], cc_inv[3 * 3];
97 CvMat CC = cvMat(3, 3, CV_64F, cc);
98 CvMat CC_inv = cvMat(3, 3, CV_64F, cc_inv);
100 for (
int i = 0; i < 3; i++)
101 for (
int j = 1; j < 4; j++) cc[3 * i + j - 1] = cws[j][i] - cws[0][i];
103 cvInvert(&CC, &CC_inv, CV_SVD);
105 for (
int i = 0; i < number_of_correspondences; i++)
107 double* pi = &pws[0] + 3 * i;
108 double*
a = &alphas[0] + 4 * i;
110 for (
int j = 0; j < 3; j++)
111 a[1 + j] = ci[3 * j] * (pi[0] - cws[0][0]) +
112 ci[3 * j + 1] * (pi[1] - cws[0][1]) +
113 ci[3 * j + 2] * (pi[2] - cws[0][2]);
114 a[0] = 1.0f -
a[1] -
a[2] -
a[3];
119 CvMat* M,
const int row,
const double* as,
const double u,
const double v)
121 double* M1 = M->data.db +
row * 12;
122 double* M2 = M1 + 12;
124 for (
int i = 0; i < 4; i++)
126 M1[3 * i] = as[i] * fu;
128 M1[3 * i + 2] = as[i] * (uc - u);
131 M2[3 * i + 1] = as[i] * fv;
132 M2[3 * i + 2] = as[i] * (vc -
v);
138 for (
int i = 0; i < 4; i++) ccs[i][0] = ccs[i][1] = ccs[i][2] = 0.0f;
140 for (
int i = 0; i < 4; i++)
142 const double*
v = ut + 12 * (11 - i);
143 for (
int j = 0; j < 4; j++)
144 for (
int k = 0; k < 3; k++) ccs[j][k] += betas[i] *
v[3 * j + k];
150 for (
int i = 0; i < number_of_correspondences; i++)
152 double*
a = &alphas[0] + 4 * i;
153 double* pc = &pcs[0] + 3 * i;
155 for (
int j = 0; j < 3; j++)
156 pc[j] =
a[0] * ccs[0][j] +
a[1] * ccs[1][j] +
a[2] * ccs[2][j] +
163 choose_control_points();
164 compute_barycentric_coordinates();
166 CvMat* M = cvCreateMat(2 * number_of_correspondences, 12, CV_64F);
168 for (
int i = 0; i < number_of_correspondences; i++)
169 fill_M(M, 2 * i, &alphas[0] + 4 * i, us[2 * i], us[2 * i + 1]);
171 double mtm[12 * 12], d[12], ut[12 * 12];
172 CvMat MtM = cvMat(12, 12, CV_64F, mtm);
173 CvMat D = cvMat(12, 1, CV_64F, d);
174 CvMat Ut = cvMat(12, 12, CV_64F, ut);
176 cvMulTransposed(M, &MtM, 1);
177 cvSVD(&MtM, &D, &Ut,
nullptr, CV_SVD_MODIFY_A | CV_SVD_U_T);
180 double l_6x10[6 * 10], rho[6];
181 CvMat L_6x10 = cvMat(6, 10, CV_64F, l_6x10);
182 CvMat Rho = cvMat(6, 1, CV_64F, rho);
184 compute_L_6x10(ut, l_6x10);
187 double Betas[4][4], rep_errors[4];
188 double Rs[4][3][3], ts[4][3];
190 find_betas_approx_1(&L_6x10, &Rho, Betas[1]);
191 gauss_newton(&L_6x10, &Rho, Betas[1]);
192 rep_errors[1] = compute_R_and_t(ut, Betas[1], Rs[1], ts[1]);
194 find_betas_approx_2(&L_6x10, &Rho, Betas[2]);
195 gauss_newton(&L_6x10, &Rho, Betas[2]);
196 rep_errors[2] = compute_R_and_t(ut, Betas[2], Rs[2], ts[2]);
198 find_betas_approx_3(&L_6x10, &Rho, Betas[3]);
199 gauss_newton(&L_6x10, &Rho, Betas[3]);
200 rep_errors[3] = compute_R_and_t(ut, Betas[3], Rs[3], ts[3]);
203 if (rep_errors[2] < rep_errors[1]) N = 2;
204 if (rep_errors[3] < rep_errors[N]) N = 3;
206 cv::Mat(3, 1, CV_64F, ts[N]).copyTo(
t);
207 cv::Mat(3, 3, CV_64F, Rs[N]).copyTo(
R);
211 const double R_src[3][3],
const double t_src[3],
double R_dst[3][3],
214 for (
int i = 0; i < 3; i++)
216 for (
int j = 0; j < 3; j++) R_dst[i][j] = R_src[i][j];
223 return (p1[0] - p2[0]) * (p1[0] - p2[0]) +
224 (p1[1] - p2[1]) * (p1[1] - p2[1]) +
225 (p1[2] - p2[2]) * (p1[2] - p2[2]);
235 double pc0[3], pw0[3];
237 pc0[0] = pc0[1] = pc0[2] = 0.0;
238 pw0[0] = pw0[1] = pw0[2] = 0.0;
240 for (
int i = 0; i < number_of_correspondences; i++)
242 const double* pc = &pcs[3 * i];
243 const double* pw = &pws[3 * i];
245 for (
int j = 0; j < 3; j++)
251 for (
int j = 0; j < 3; j++)
253 pc0[j] /= number_of_correspondences;
254 pw0[j] /= number_of_correspondences;
257 double abt[3 * 3], abt_d[3], abt_u[3 * 3], abt_v[3 * 3];
258 CvMat ABt = cvMat(3, 3, CV_64F, abt);
259 CvMat ABt_D = cvMat(3, 1, CV_64F, abt_d);
260 CvMat ABt_U = cvMat(3, 3, CV_64F, abt_u);
261 CvMat ABt_V = cvMat(3, 3, CV_64F, abt_v);
264 for (
int i = 0; i < number_of_correspondences; i++)
266 double* pc = &pcs[3 * i];
267 double* pw = &pws[3 * i];
269 for (
int j = 0; j < 3; j++)
271 abt[3 * j] += (pc[j] - pc0[j]) * (pw[0] - pw0[0]);
272 abt[3 * j + 1] += (pc[j] - pc0[j]) * (pw[1] - pw0[1]);
273 abt[3 * j + 2] += (pc[j] - pc0[j]) * (pw[2] - pw0[2]);
277 cvSVD(&ABt, &ABt_D, &ABt_U, &ABt_V, CV_SVD_MODIFY_A);
279 for (
int i = 0; i < 3; i++)
280 for (
int j = 0; j < 3; j++)
R[i][j] = dot(abt_u + 3 * i, abt_v + 3 * j);
283 R[0][0] *
R[1][1] *
R[2][2] +
R[0][1] *
R[1][2] *
R[2][0] +
284 R[0][2] *
R[1][0] *
R[2][1] -
R[0][2] *
R[1][1] *
R[2][0] -
285 R[0][1] *
R[1][0] *
R[2][2] -
R[0][0] *
R[1][2] *
R[2][1];
294 t[0] = pc0[0] - dot(
R[0], pw0);
295 t[1] = pc0[1] - dot(
R[1], pw0);
296 t[2] = pc0[2] - dot(
R[2], pw0);
303 for (
int i = 0; i < 4; i++)
304 for (
int j = 0; j < 3; j++) ccs[i][j] = -ccs[i][j];
306 for (
int i = 0; i < number_of_correspondences; i++)
308 pcs[3 * i] = -pcs[3 * i];
309 pcs[3 * i + 1] = -pcs[3 * i + 1];
310 pcs[3 * i + 2] = -pcs[3 * i + 2];
316 const double* ut,
const double* betas,
double R[3][3],
double t[3])
318 compute_ccs(betas, ut);
323 estimate_R_and_t(
R,
t);
325 return reprojection_error(
R,
t);
329 const double R[3][3],
const double t[3])
333 for (
int i = 0; i < number_of_correspondences; i++)
335 double* pw = &pws[3 * i];
336 double Xc = dot(
R[0], pw) +
t[0];
337 double Yc = dot(
R[1], pw) +
t[1];
338 double inv_Zc = 1.0 / (dot(
R[2], pw) +
t[2]);
339 double ue = uc + fu * Xc * inv_Zc;
340 double ve = vc + fv * Yc * inv_Zc;
341 double u = us[2 * i],
v = us[2 * i + 1];
343 sum2 += sqrt((u - ue) * (u - ue) + (
v - ve) * (
v - ve));
346 return sum2 / number_of_correspondences;
353 const CvMat* L_6x10,
const CvMat* Rho,
double* betas)
355 double l_6x4[6 * 4], b4[4];
356 CvMat L_6x4 = cvMat(6, 4, CV_64F, l_6x4);
357 CvMat B4 = cvMat(4, 1, CV_64F, b4);
359 for (
int i = 0; i < 6; i++)
361 cvmSet(&L_6x4, i, 0, cvmGet(L_6x10, i, 0));
362 cvmSet(&L_6x4, i, 1, cvmGet(L_6x10, i, 1));
363 cvmSet(&L_6x4, i, 2, cvmGet(L_6x10, i, 3));
364 cvmSet(&L_6x4, i, 3, cvmGet(L_6x10, i, 6));
367 cvSolve(&L_6x4, Rho, &B4, CV_SVD);
371 betas[0] = sqrt(-b4[0]);
372 betas[1] = -b4[1] / betas[0];
373 betas[2] = -b4[2] / betas[0];
374 betas[3] = -b4[3] / betas[0];
378 betas[0] = sqrt(b4[0]);
379 betas[1] = b4[1] / betas[0];
380 betas[2] = b4[2] / betas[0];
381 betas[3] = b4[3] / betas[0];
389 const CvMat* L_6x10,
const CvMat* Rho,
double* betas)
391 double l_6x3[6 * 3],
b3[3];
392 CvMat L_6x3 = cvMat(6, 3, CV_64F, l_6x3);
393 CvMat B3 = cvMat(3, 1, CV_64F,
b3);
395 for (
int i = 0; i < 6; i++)
397 cvmSet(&L_6x3, i, 0, cvmGet(L_6x10, i, 0));
398 cvmSet(&L_6x3, i, 1, cvmGet(L_6x10, i, 1));
399 cvmSet(&L_6x3, i, 2, cvmGet(L_6x10, i, 2));
402 cvSolve(&L_6x3, Rho, &B3, CV_SVD);
406 betas[0] = sqrt(-
b3[0]);
407 betas[1] = (
b3[2] < 0) ? sqrt(-
b3[2]) : 0.0;
411 betas[0] = sqrt(
b3[0]);
412 betas[1] = (
b3[2] > 0) ? sqrt(
b3[2]) : 0.0;
415 if (
b3[1] < 0) betas[0] = -betas[0];
425 const CvMat* L_6x10,
const CvMat* Rho,
double* betas)
427 double l_6x5[6 * 5], b5[5];
428 CvMat L_6x5 = cvMat(6, 5, CV_64F, l_6x5);
429 CvMat B5 = cvMat(5, 1, CV_64F, b5);
431 for (
int i = 0; i < 6; i++)
433 cvmSet(&L_6x5, i, 0, cvmGet(L_6x10, i, 0));
434 cvmSet(&L_6x5, i, 1, cvmGet(L_6x10, i, 1));
435 cvmSet(&L_6x5, i, 2, cvmGet(L_6x10, i, 2));
436 cvmSet(&L_6x5, i, 3, cvmGet(L_6x10, i, 3));
437 cvmSet(&L_6x5, i, 4, cvmGet(L_6x10, i, 4));
440 cvSolve(&L_6x5, Rho, &B5, CV_SVD);
444 betas[0] = sqrt(-b5[0]);
445 betas[1] = (b5[2] < 0) ? sqrt(-b5[2]) : 0.0;
449 betas[0] = sqrt(b5[0]);
450 betas[1] = (b5[2] > 0) ? sqrt(b5[2]) : 0.0;
452 if (b5[1] < 0) betas[0] = -betas[0];
453 betas[2] = b5[3] / betas[0];
468 for (
int i = 0; i < 4; i++)
471 for (
int j = 0; j < 6; j++)
473 dv[i][j][0] =
v[i][3 *
a] -
v[i][3 *
b];
474 dv[i][j][1] =
v[i][3 *
a + 1] -
v[i][3 *
b + 1];
475 dv[i][j][2] =
v[i][3 *
a + 2] -
v[i][3 *
b + 2];
486 for (
int i = 0; i < 6; i++)
488 double*
row = l_6x10 + 10 * i;
490 row[0] = dot(dv[0][i], dv[0][i]);
491 row[1] = 2.0f * dot(dv[0][i], dv[1][i]);
492 row[2] = dot(dv[1][i], dv[1][i]);
493 row[3] = 2.0f * dot(dv[0][i], dv[2][i]);
494 row[4] = 2.0f * dot(dv[1][i], dv[2][i]);
495 row[5] = dot(dv[2][i], dv[2][i]);
496 row[6] = 2.0f * dot(dv[0][i], dv[3][i]);
497 row[7] = 2.0f * dot(dv[1][i], dv[3][i]);
498 row[8] = 2.0f * dot(dv[2][i], dv[3][i]);
499 row[9] = dot(dv[3][i], dv[3][i]);
505 rho[0] = dist2(cws[0], cws[1]);
506 rho[1] = dist2(cws[0], cws[2]);
507 rho[2] = dist2(cws[0], cws[3]);
508 rho[3] = dist2(cws[1], cws[2]);
509 rho[4] = dist2(cws[1], cws[3]);
510 rho[5] = dist2(cws[2], cws[3]);
514 const double* l_6x10,
const double* rho,
const double betas[4], CvMat*
A,
517 for (
int i = 0; i < 6; i++)
519 const double* rowL = l_6x10 + i * 10;
520 double* rowA =
A->data.db + i * 4;
522 rowA[0] = 2 * rowL[0] * betas[0] + rowL[1] * betas[1] +
523 rowL[3] * betas[2] + rowL[6] * betas[3];
524 rowA[1] = rowL[1] * betas[0] + 2 * rowL[2] * betas[1] +
525 rowL[4] * betas[2] + rowL[7] * betas[3];
526 rowA[2] = rowL[3] * betas[0] + rowL[4] * betas[1] +
527 2 * rowL[5] * betas[2] + rowL[8] * betas[3];
528 rowA[3] = rowL[6] * betas[0] + rowL[7] * betas[1] + rowL[8] * betas[2] +
529 2 * rowL[9] * betas[3];
534 (rowL[0] * betas[0] * betas[0] + rowL[1] * betas[0] * betas[1] +
535 rowL[2] * betas[1] * betas[1] + rowL[3] * betas[0] * betas[2] +
536 rowL[4] * betas[1] * betas[2] + rowL[5] * betas[2] * betas[2] +
537 rowL[6] * betas[0] * betas[3] + rowL[7] * betas[1] * betas[3] +
538 rowL[8] * betas[2] * betas[3] +
539 rowL[9] * betas[3] * betas[3]));
544 const CvMat* L_6x10,
const CvMat* Rho,
double betas[4])
546 const int iterations_number = 5;
548 double a[6 * 4],
b[6],
x[4];
549 CvMat
A = cvMat(6, 4, CV_64F,
a);
550 CvMat B = cvMat(6, 1, CV_64F,
b);
551 CvMat X = cvMat(4, 1, CV_64F,
x);
553 for (
int k = 0; k < iterations_number; k++)
555 compute_A_and_b_gauss_newton(
556 L_6x10->data.db, Rho->data.db, betas, &
A, &B);
557 qr_solve(&
A, &B, &X);
558 for (
int i = 0; i < 4; i++) betas[i] +=
x[i];
564 const int nr =
A->rows;
565 const int nc =
A->cols;
567 if (max_nr != 0 && max_nr < nr)
579 double *pA =
A->data.db, *ppAkk = pA;
580 for (
int k = 0; k < nc; k++)
582 double *ppAik1 = ppAkk, eta = fabs(*ppAik1);
583 for (
int i = k + 1; i < nr; i++)
585 double elt = fabs(*ppAik1);
586 if (eta < elt) eta = elt;
598 double *ppAik2 = ppAkk, sum2 = 0.0, inv_eta = 1. / eta;
599 for (
int i = k; i < nr; i++)
602 sum2 += *ppAik2 * *ppAik2;
605 double sigma = sqrt(sum2);
606 if (*ppAkk < 0) sigma = -sigma;
608 A1[k] = sigma * *ppAkk;
609 A2[k] = -eta * sigma;
610 for (
int j = k + 1; j < nc; j++)
612 double *ppAik = ppAkk,
sum = 0;
613 for (
int i = k; i < nr; i++)
615 sum += *ppAik * ppAik[j - k];
618 double tau =
sum /
A1[k];
620 for (
int i = k; i < nr; i++)
622 ppAik[j - k] -= tau * *ppAik;
631 double *ppAjj = pA, *pb =
b->data.db;
632 for (
int j = 0; j < nc; j++)
634 double *ppAij = ppAjj, tau = 0;
635 for (
int i = j; i < nr; i++)
637 tau += *ppAij * pb[i];
642 for (
int i = j; i < nr; i++)
644 pb[i] -= tau * *ppAij;
651 double* pX = X->data.db;
652 pX[nc - 1] = pb[nc - 1] / A2[nc - 1];
653 for (
int i = nc - 2; i >= 0; i--)
655 double *ppAij = pA + i * nc + (i + 1),
sum = 0;
657 for (
int j = i + 1; j < nc; j++)
659 sum += *ppAij * pX[j];
662 pX[i] = (pb[i] -
sum) / A2[i];
void compute_A_and_b_gauss_newton(const double *l_6x10, const double *rho, const double cb[4], CvMat *A, CvMat *b)
Internal function.
void find_betas_approx_3(const CvMat *L_6x10, const CvMat *Rho, double *betas)
Internal function.
void compute_pose(cv::Mat &R, cv::Mat &t)
OpenCV wrapper to compute pose.
void compute_rho(double *rho)
Get distances between all object points taken 2 at a time(nC2)
~epnp()
Destructor for EPnP class.
double reprojection_error(const double R[3][3], const double t[3])
Function to compute reprojection error.
double dist2(const double *p1, const double *p2)
Squared distance between two vectors.
void gauss_newton(const CvMat *L_6x10, const CvMat *Rho, double current_betas[4])
Gauss Newton iterative algorithm.
void compute_barycentric_coordinates()
Convert from object space to relative object space (Barycentric coordinates)
void copy_R_and_t(const double R_dst[3][3], const double t_dst[3], double R_src[3][3], double t_src[3])
Copy function of output result.
void fill_M(CvMat *M, const int row, const double *alphas, const double u, const double v)
Generate the Matrix M.
void compute_pcs()
Internal function.
double dot(const double *v1, const double *v2)
Dot product of two OpenCV vectors.
CONTAINER::Scalar sum(const CONTAINER &v)
Computes the sum of all the elements.
Efficient PnP - Eigen Wrapper for OpenCV calib3d implementation.
void qr_solve(CvMat *A, CvMat *b, CvMat *X)
QR optimization algorithm.
void choose_control_points()
Function to select 4 control points from n points.
void estimate_R_and_t(double R[3][3], double t[3])
Helper function to compute_R_and_t()
void solve_for_sign()
Internal function.
void find_betas_approx_2(const CvMat *L_6x10, const CvMat *Rho, double *betas)
Internal function.
GLenum GLenum GLvoid * row
double compute_R_and_t(const double *ut, const double *betas, double R[3][3], double t[3])
Function to compute pose.
void compute_ccs(const double *betas, const double *ut)
Internal function.
GLfloat GLfloat GLfloat v2
epnp(const cv::Mat &cameraMatrix, const cv::Mat &opoints, const cv::Mat &ipoints)
Constructor for EPnP class.
GLubyte GLubyte GLubyte a
void compute_L_6x10(const double *ut, double *l_6x10)
Internal function.
void find_betas_approx_1(const CvMat *L_6x10, const CvMat *Rho, double *betas)
Internal function.