MRPT  2.0.1
epnp.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include <mrpt/config.h>
11 #include <iostream>
12 #include "vision-precomp.h" // Precompiled headers
13 
14 #if MRPT_HAS_OPENCV
15 #include <mrpt/3rdparty/do_opencv_includes.h>
16 using namespace cv;
17 
18 #include "epnp.h"
19 
21  const cv::Mat& cameraMatrix, const cv::Mat& opoints, const cv::Mat& ipoints)
22 {
23  if (cameraMatrix.depth() == CV_32F)
24  init_camera_parameters<float>(cameraMatrix);
25  else
26  init_camera_parameters<double>(cameraMatrix);
27 
28  number_of_correspondences = std::max(
29  opoints.checkVector(3, CV_32F), opoints.checkVector(3, CV_64F));
30 
31  pws.resize(3 * number_of_correspondences);
32  us.resize(2 * number_of_correspondences);
33 
34  if (opoints.depth() == ipoints.depth())
35  {
36  if (opoints.depth() == CV_32F)
37  init_points<float, float>(opoints, ipoints);
38  else
39  init_points<double, double>(opoints, ipoints);
40  }
41  else if (opoints.depth() == CV_32F)
42  init_points<float, double>(opoints, ipoints);
43  else
44  init_points<double, float>(opoints, ipoints);
45 
46  alphas.resize(4 * number_of_correspondences);
47  pcs.resize(3 * number_of_correspondences);
48 
49  max_nr = 0;
50  A1 = nullptr;
51  A2 = nullptr;
52 }
53 
55 {
56  if (A1) delete[] A1;
57  if (A2) delete[] A2;
58 }
59 
61 {
62  // Take C0 as the reference points centroid:
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];
66 
67  for (int j = 0; j < 3; j++) cws[0][j] /= number_of_correspondences;
68 
69  // Take C1, C2, and C3 from PCA on the reference points:
70  CvMat* PW0 = cvCreateMat(number_of_correspondences, 3, CV_64F);
71 
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);
76 
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];
80 
81  cvMulTransposed(PW0, &PW0tPW0, 1);
82  cvSVD(&PW0tPW0, &DC, &UCt, nullptr, CV_SVD_MODIFY_A | CV_SVD_U_T);
83 
84  cvReleaseMat(&PW0);
85 
86  for (int i = 1; i < 4; i++)
87  {
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];
91  }
92 }
93 
95 {
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);
99 
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];
102 
103  cvInvert(&CC, &CC_inv, CV_SVD);
104  double* ci = cc_inv;
105  for (int i = 0; i < number_of_correspondences; i++)
106  {
107  double* pi = &pws[0] + 3 * i;
108  double* a = &alphas[0] + 4 * i;
109 
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];
115  }
116 }
117 
119  CvMat* M, const int row, const double* as, const double u, const double v)
120 {
121  double* M1 = M->data.db + row * 12;
122  double* M2 = M1 + 12;
123 
124  for (int i = 0; i < 4; i++)
125  {
126  M1[3 * i] = as[i] * fu;
127  M1[3 * i + 1] = 0.0;
128  M1[3 * i + 2] = as[i] * (uc - u);
129 
130  M2[3 * i] = 0.0;
131  M2[3 * i + 1] = as[i] * fv;
132  M2[3 * i + 2] = as[i] * (vc - v);
133  }
134 }
135 
136 void mrpt::vision::pnp::epnp::compute_ccs(const double* betas, const double* ut)
137 {
138  for (int i = 0; i < 4; i++) ccs[i][0] = ccs[i][1] = ccs[i][2] = 0.0f;
139 
140  for (int i = 0; i < 4; i++)
141  {
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];
145  }
146 }
147 
149 {
150  for (int i = 0; i < number_of_correspondences; i++)
151  {
152  double* a = &alphas[0] + 4 * i;
153  double* pc = &pcs[0] + 3 * i;
154 
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] +
157  a[3] * ccs[3][j];
158  }
159 }
160 
161 void mrpt::vision::pnp::epnp::compute_pose(cv::Mat& R, cv::Mat& t)
162 {
163  choose_control_points();
164  compute_barycentric_coordinates();
165 
166  CvMat* M = cvCreateMat(2 * number_of_correspondences, 12, CV_64F);
167 
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]);
170 
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);
175 
176  cvMulTransposed(M, &MtM, 1);
177  cvSVD(&MtM, &D, &Ut, nullptr, CV_SVD_MODIFY_A | CV_SVD_U_T);
178  cvReleaseMat(&M);
179 
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);
183 
184  compute_L_6x10(ut, l_6x10);
185  compute_rho(rho);
186 
187  double Betas[4][4], rep_errors[4];
188  double Rs[4][3][3], ts[4][3];
189 
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]);
193 
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]);
197 
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]);
201 
202  int N = 1;
203  if (rep_errors[2] < rep_errors[1]) N = 2;
204  if (rep_errors[3] < rep_errors[N]) N = 3;
205 
206  cv::Mat(3, 1, CV_64F, ts[N]).copyTo(t);
207  cv::Mat(3, 3, CV_64F, Rs[N]).copyTo(R);
208 }
209 
211  const double R_src[3][3], const double t_src[3], double R_dst[3][3],
212  double t_dst[3])
213 {
214  for (int i = 0; i < 3; i++)
215  {
216  for (int j = 0; j < 3; j++) R_dst[i][j] = R_src[i][j];
217  t_dst[i] = t_src[i];
218  }
219 }
220 
221 double mrpt::vision::pnp::epnp::dist2(const double* p1, const double* p2)
222 {
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]);
226 }
227 
228 double mrpt::vision::pnp::epnp::dot(const double* v1, const double* v2)
229 {
230  return v1[0] * v2[0] + v1[1] * v2[1] + v1[2] * v2[2];
231 }
232 
233 void mrpt::vision::pnp::epnp::estimate_R_and_t(double R[3][3], double t[3])
234 {
235  double pc0[3], pw0[3];
236 
237  pc0[0] = pc0[1] = pc0[2] = 0.0;
238  pw0[0] = pw0[1] = pw0[2] = 0.0;
239 
240  for (int i = 0; i < number_of_correspondences; i++)
241  {
242  const double* pc = &pcs[3 * i];
243  const double* pw = &pws[3 * i];
244 
245  for (int j = 0; j < 3; j++)
246  {
247  pc0[j] += pc[j];
248  pw0[j] += pw[j];
249  }
250  }
251  for (int j = 0; j < 3; j++)
252  {
253  pc0[j] /= number_of_correspondences;
254  pw0[j] /= number_of_correspondences;
255  }
256 
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);
262 
263  cvSetZero(&ABt);
264  for (int i = 0; i < number_of_correspondences; i++)
265  {
266  double* pc = &pcs[3 * i];
267  double* pw = &pws[3 * i];
268 
269  for (int j = 0; j < 3; j++)
270  {
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]);
274  }
275  }
276 
277  cvSVD(&ABt, &ABt_D, &ABt_U, &ABt_V, CV_SVD_MODIFY_A);
278 
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);
281 
282  const double det =
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];
286 
287  if (det < 0)
288  {
289  R[2][0] = -R[2][0];
290  R[2][1] = -R[2][1];
291  R[2][2] = -R[2][2];
292  }
293 
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);
297 }
298 
300 {
301  if (pcs[2] < 0.0)
302  {
303  for (int i = 0; i < 4; i++)
304  for (int j = 0; j < 3; j++) ccs[i][j] = -ccs[i][j];
305 
306  for (int i = 0; i < number_of_correspondences; i++)
307  {
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];
311  }
312  }
313 }
314 
316  const double* ut, const double* betas, double R[3][3], double t[3])
317 {
318  compute_ccs(betas, ut);
319  compute_pcs();
320 
321  solve_for_sign();
322 
323  estimate_R_and_t(R, t);
324 
325  return reprojection_error(R, t);
326 }
327 
329  const double R[3][3], const double t[3])
330 {
331  double sum2 = 0.0;
332 
333  for (int i = 0; i < number_of_correspondences; i++)
334  {
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];
342 
343  sum2 += sqrt((u - ue) * (u - ue) + (v - ve) * (v - ve));
344  }
345 
346  return sum2 / number_of_correspondences;
347 }
348 
349 // betas10 = [B11 B12 B22 B13 B23 B33 B14 B24 B34 B44]
350 // betas_approx_1 = [B11 B12 B13 B14]
351 
353  const CvMat* L_6x10, const CvMat* Rho, double* betas)
354 {
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);
358 
359  for (int i = 0; i < 6; i++)
360  {
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));
365  }
366 
367  cvSolve(&L_6x4, Rho, &B4, CV_SVD);
368 
369  if (b4[0] < 0)
370  {
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];
375  }
376  else
377  {
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];
382  }
383 }
384 
385 // betas10 = [B11 B12 B22 B13 B23 B33 B14 B24 B34 B44]
386 // betas_approx_2 = [B11 B12 B22 ]
387 
389  const CvMat* L_6x10, const CvMat* Rho, double* betas)
390 {
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);
394 
395  for (int i = 0; i < 6; i++)
396  {
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));
400  }
401 
402  cvSolve(&L_6x3, Rho, &B3, CV_SVD);
403 
404  if (b3[0] < 0)
405  {
406  betas[0] = sqrt(-b3[0]);
407  betas[1] = (b3[2] < 0) ? sqrt(-b3[2]) : 0.0;
408  }
409  else
410  {
411  betas[0] = sqrt(b3[0]);
412  betas[1] = (b3[2] > 0) ? sqrt(b3[2]) : 0.0;
413  }
414 
415  if (b3[1] < 0) betas[0] = -betas[0];
416 
417  betas[2] = 0.0;
418  betas[3] = 0.0;
419 }
420 
421 // betas10 = [B11 B12 B22 B13 B23 B33 B14 B24 B34 B44]
422 // betas_approx_3 = [B11 B12 B22 B13 B23 ]
423 
425  const CvMat* L_6x10, const CvMat* Rho, double* betas)
426 {
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);
430 
431  for (int i = 0; i < 6; i++)
432  {
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));
438  }
439 
440  cvSolve(&L_6x5, Rho, &B5, CV_SVD);
441 
442  if (b5[0] < 0)
443  {
444  betas[0] = sqrt(-b5[0]);
445  betas[1] = (b5[2] < 0) ? sqrt(-b5[2]) : 0.0;
446  }
447  else
448  {
449  betas[0] = sqrt(b5[0]);
450  betas[1] = (b5[2] > 0) ? sqrt(b5[2]) : 0.0;
451  }
452  if (b5[1] < 0) betas[0] = -betas[0];
453  betas[2] = b5[3] / betas[0];
454  betas[3] = 0.0;
455 }
456 
457 void mrpt::vision::pnp::epnp::compute_L_6x10(const double* ut, double* l_6x10)
458 {
459  const double* v[4];
460 
461  v[0] = ut + 12 * 11;
462  v[1] = ut + 12 * 10;
463  v[2] = ut + 12 * 9;
464  v[3] = ut + 12 * 8;
465 
466  double dv[4][6][3];
467 
468  for (int i = 0; i < 4; i++)
469  {
470  int a = 0, b = 1;
471  for (int j = 0; j < 6; j++)
472  {
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];
476 
477  b++;
478  if (b > 3)
479  {
480  a++;
481  b = a + 1;
482  }
483  }
484  }
485 
486  for (int i = 0; i < 6; i++)
487  {
488  double* row = l_6x10 + 10 * i;
489 
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]);
500  }
501 }
502 
504 {
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]);
511 }
512 
514  const double* l_6x10, const double* rho, const double betas[4], CvMat* A,
515  CvMat* b)
516 {
517  for (int i = 0; i < 6; i++)
518  {
519  const double* rowL = l_6x10 + i * 10;
520  double* rowA = A->data.db + i * 4;
521 
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];
530 
531  cvmSet(
532  b, i, 0,
533  rho[i] -
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]));
540  }
541 }
542 
544  const CvMat* L_6x10, const CvMat* Rho, double betas[4])
545 {
546  const int iterations_number = 5;
547 
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);
552 
553  for (int k = 0; k < iterations_number; k++)
554  {
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];
559  }
560 }
561 
562 void mrpt::vision::pnp::epnp::qr_solve(CvMat* A, CvMat* b, CvMat* X)
563 {
564  const int nr = A->rows;
565  const int nc = A->cols;
566 
567  if (max_nr != 0 && max_nr < nr)
568  {
569  delete[] A1;
570  delete[] A2;
571  }
572  if (max_nr < nr)
573  {
574  max_nr = nr;
575  A1 = new double[nr];
576  A2 = new double[nr];
577  }
578 
579  double *pA = A->data.db, *ppAkk = pA;
580  for (int k = 0; k < nc; k++)
581  {
582  double *ppAik1 = ppAkk, eta = fabs(*ppAik1);
583  for (int i = k + 1; i < nr; i++)
584  {
585  double elt = fabs(*ppAik1);
586  if (eta < elt) eta = elt;
587  ppAik1 += nc;
588  }
589  if (eta == 0)
590  {
591  A1[k] = A2[k] = 0.0;
592  // cerr << "God damnit, A is singular, this shouldn't happen." <<
593  // endl;
594  return;
595  }
596  else
597  {
598  double *ppAik2 = ppAkk, sum2 = 0.0, inv_eta = 1. / eta;
599  for (int i = k; i < nr; i++)
600  {
601  *ppAik2 *= inv_eta;
602  sum2 += *ppAik2 * *ppAik2;
603  ppAik2 += nc;
604  }
605  double sigma = sqrt(sum2);
606  if (*ppAkk < 0) sigma = -sigma;
607  *ppAkk += sigma;
608  A1[k] = sigma * *ppAkk;
609  A2[k] = -eta * sigma;
610  for (int j = k + 1; j < nc; j++)
611  {
612  double *ppAik = ppAkk, sum = 0;
613  for (int i = k; i < nr; i++)
614  {
615  sum += *ppAik * ppAik[j - k];
616  ppAik += nc;
617  }
618  double tau = sum / A1[k];
619  ppAik = ppAkk;
620  for (int i = k; i < nr; i++)
621  {
622  ppAik[j - k] -= tau * *ppAik;
623  ppAik += nc;
624  }
625  }
626  }
627  ppAkk += nc + 1;
628  }
629 
630  // b <- Qt b
631  double *ppAjj = pA, *pb = b->data.db;
632  for (int j = 0; j < nc; j++)
633  {
634  double *ppAij = ppAjj, tau = 0;
635  for (int i = j; i < nr; i++)
636  {
637  tau += *ppAij * pb[i];
638  ppAij += nc;
639  }
640  tau /= A1[j];
641  ppAij = ppAjj;
642  for (int i = j; i < nr; i++)
643  {
644  pb[i] -= tau * *ppAij;
645  ppAij += nc;
646  }
647  ppAjj += nc + 1;
648  }
649 
650  // X = R-1 b
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--)
654  {
655  double *ppAij = pA + i * nc + (i + 1), sum = 0;
656 
657  for (int j = i + 1; j < nc; j++)
658  {
659  sum += *ppAij * pX[j];
660  ppAij++;
661  }
662  pX[i] = (pb[i] - sum) / A2[i];
663  }
664 }
665 #endif
void compute_A_and_b_gauss_newton(const double *l_6x10, const double *rho, const double cb[4], CvMat *A, CvMat *b)
Internal function.
Definition: epnp.cpp:513
void find_betas_approx_3(const CvMat *L_6x10, const CvMat *Rho, double *betas)
Internal function.
Definition: epnp.cpp:424
void compute_pose(cv::Mat &R, cv::Mat &t)
OpenCV wrapper to compute pose.
Definition: epnp.cpp:161
void compute_rho(double *rho)
Get distances between all object points taken 2 at a time(nC2)
Definition: epnp.cpp:503
~epnp()
Destructor for EPnP class.
Definition: epnp.cpp:54
double reprojection_error(const double R[3][3], const double t[3])
Function to compute reprojection error.
Definition: epnp.cpp:328
double dist2(const double *p1, const double *p2)
Squared distance between two vectors.
Definition: epnp.cpp:221
void gauss_newton(const CvMat *L_6x10, const CvMat *Rho, double current_betas[4])
Gauss Newton iterative algorithm.
Definition: epnp.cpp:543
void compute_barycentric_coordinates()
Convert from object space to relative object space (Barycentric coordinates)
Definition: epnp.cpp:94
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.
Definition: epnp.cpp:210
void fill_M(CvMat *M, const int row, const double *alphas, const double u, const double v)
Generate the Matrix M.
Definition: epnp.cpp:118
Definition: img/CImage.h:23
void compute_pcs()
Internal function.
Definition: epnp.cpp:148
double dot(const double *v1, const double *v2)
Dot product of two OpenCV vectors.
Definition: epnp.cpp:228
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.
Definition: epnp.cpp:562
void choose_control_points()
Function to select 4 control points from n points.
Definition: epnp.cpp:60
void estimate_R_and_t(double R[3][3], double t[3])
Helper function to compute_R_and_t()
Definition: epnp.cpp:233
void solve_for_sign()
Internal function.
Definition: epnp.cpp:299
const float R
void find_betas_approx_2(const CvMat *L_6x10, const CvMat *Rho, double *betas)
Internal function.
Definition: epnp.cpp:388
double compute_R_and_t(const double *ut, const double *betas, double R[3][3], double t[3])
Function to compute pose.
Definition: epnp.cpp:315
void compute_ccs(const double *betas, const double *ut)
Internal function.
Definition: epnp.cpp:136
epnp(const cv::Mat &cameraMatrix, const cv::Mat &opoints, const cv::Mat &ipoints)
Constructor for EPnP class.
Definition: epnp.cpp:20
void compute_L_6x10(const double *ut, double *l_6x10)
Internal function.
Definition: epnp.cpp:457
void find_betas_approx_1(const CvMat *L_6x10, const CvMat *Rho, double *betas)
Internal function.
Definition: epnp.cpp:352



Page generated by Doxygen 1.8.14 for MRPT 2.0.1 Git: 0fef1a6d7 Fri Apr 3 23:00:21 2020 +0200 at vie abr 3 23:20:28 CEST 2020