13 #include <mrpt/otherlibs/do_opencv_includes.h> 35 dls(
const cv::Mat& opoints,
const cv::Mat& ipoints);
47 template <
typename Opo
intType,
typename Ipo
intType>
48 void init_points(
const cv::Mat& opoints,
const cv::Mat& ipoints)
50 for (
int i = 0; i <
N; i++)
52 p.at<
double>(0, i) = opoints.at<OpointType>(i, 0);
53 p.at<
double>(1, i) = opoints.at<OpointType>(i, 1);
54 p.at<
double>(2, i) = opoints.at<OpointType>(i, 2);
57 mn.at<
double>(0) +=
p.at<
double>(0, i);
58 mn.at<
double>(1) +=
p.at<
double>(1, i);
59 mn.at<
double>(2) +=
p.at<
double>(2, i);
62 double sr = std::pow(ipoints.at<IpointType>(i, 0), 2) +
63 std::pow(ipoints.at<IpointType>(i, 1), 2) + (double)1;
66 z.at<
double>(0, i) = ipoints.at<IpointType>(i, 0) / sr;
67 z.at<
double>(1, i) = ipoints.at<IpointType>(i, 1) / sr;
68 z.at<
double>(2, i) = (
double)1 / sr;
71 mn.at<
double>(0) /= (
double)
N;
72 mn.at<
double>(1) /= (
double)
N;
73 mn.at<
double>(2) /= (
double)
N;
107 const cv::Mat& Mtilde, cv::Mat& eigenval_real, cv::Mat& eigenval_imag,
108 cv::Mat& eigenvec_real, cv::Mat& eigenvec_imag);
125 const std::vector<double>&
a,
const std::vector<double>&
b,
126 const std::vector<double>&
c,
const std::vector<double>& u);
148 cv::Mat
skewsymm(
const cv::Mat* X1);
155 cv::Mat
rotx(
const double t);
162 cv::Mat
roty(
const double t);
169 cv::Mat
rotz(
const double t);
176 cv::Mat
mean(
const cv::Mat& M);
204 class EigenvalueDecomposition
214 cv::Mat _eigenvalues;
216 cv::Mat _eigenvectors;
223 template <
typename _Tp>
235 template <
typename _Tp>
236 _Tp* alloc_1d(
int m, _Tp
val)
238 _Tp* arr = alloc_1d<_Tp>(m);
239 for (
int i = 0; i < m; i++) arr[i] =
val;
249 template <
typename _Tp>
250 _Tp** alloc_2d(
int m,
int _n)
252 _Tp** arr =
new _Tp*[m];
253 for (
int i = 0; i < m; i++) arr[i] =
new _Tp[_n];
264 template <
typename _Tp>
265 _Tp** alloc_2d(
int m,
int _n, _Tp
val)
267 _Tp** arr = alloc_2d<_Tp>(m, _n);
268 for (
int i = 0; i < m; i++)
270 for (
int j = 0; j < _n; j++)
285 void cdiv(
double xr,
double xi,
double yr,
double yi)
288 if (std::abs(yr) > std::abs(yi))
292 cdivr = (xr +
r * xi) / dv;
293 cdivi = (xi -
r * xr) / dv;
299 cdivr = (
r * xr + xi) / dv;
300 cdivi = (
r * xi - xr) / dv;
319 double eps = std::pow(2.0, -52.0);
320 double exshift = 0.0;
321 double p = 0,
q = 0,
r = 0,
s = 0,
z = 0,
t,
w,
x,
y;
326 for (
int i = 0; i < nn; i++)
328 if (i < low || i > high)
333 for (
int j = std::max(i - 1, 0); j < nn; j++)
347 s = std::abs(H[l - 1][l - 1]) + std::abs(H[l][l]);
352 if (std::abs(H[l][l - 1]) <
eps *
s)
364 H[n1][n1] = H[n1][n1] + exshift;
372 else if (l == n1 - 1)
374 w = H[n1][n1 - 1] * H[n1 - 1][n1];
375 p = (H[n1 - 1][n1 - 1] - H[n1][n1]) / 2.0;
377 z = std::sqrt(std::abs(
q));
378 H[n1][n1] = H[n1][n1] + exshift;
379 H[n1 - 1][n1 - 1] = H[n1 - 1][n1 - 1] + exshift;
403 s = std::abs(
x) + std::abs(
z);
406 r = std::sqrt(
p *
p +
q *
q);
412 for (
int j = n1 - 1; j < nn; j++)
415 H[n1 - 1][j] =
q *
z +
p * H[n1][j];
416 H[n1][j] =
q * H[n1][j] -
p *
z;
421 for (
int i = 0; i <= n1; i++)
424 H[i][n1 - 1] =
q *
z +
p * H[i][n1];
425 H[i][n1] =
q * H[i][n1] -
p *
z;
430 for (
int i = low; i <= high; i++)
433 V[i][n1 - 1] =
q *
z +
p * V[i][n1];
434 V[i][n1] =
q * V[i][n1] -
p *
z;
460 y = H[n1 - 1][n1 - 1];
461 w = H[n1][n1 - 1] * H[n1 - 1][n1];
469 for (
int i = low; i <= n1; i++)
473 s = std::abs(H[n1][n1 - 1]) + std::abs(H[n1 - 1][n1 - 2]);
491 s =
x -
w / ((
y -
x) / 2.0 +
s);
492 for (
int i = low; i <= n1; i++)
510 p = (
r *
s -
w) / H[m + 1][m] + H[m][m + 1];
511 q = H[m + 1][m + 1] -
z -
r -
s;
513 s = std::abs(
p) + std::abs(
q) + std::abs(
r);
521 if (std::abs(H[m][m - 1]) * (std::abs(
q) + std::abs(
r)) <
523 (std::abs(H[m - 1][m - 1]) + std::abs(
z) +
524 std::abs(H[m + 1][m + 1]))))
531 for (
int i = m + 2; i <= n1; i++)
542 for (
int k = m; k <= n1 - 1; k++)
544 bool notlast = (k != n1 - 1);
549 r = (notlast ? H[k + 2][k - 1] : 0.0);
550 x = std::abs(
p) + std::abs(
q) + std::abs(
r);
562 s = std::sqrt(
p *
p +
q *
q +
r *
r);
571 H[k][k - 1] = -
s *
x;
575 H[k][k - 1] = -H[k][k - 1];
586 for (
int j = k; j < nn; j++)
588 p = H[k][j] +
q * H[k + 1][j];
591 p =
p +
r * H[k + 2][j];
592 H[k + 2][j] = H[k + 2][j] -
p *
z;
594 H[k][j] = H[k][j] -
p *
x;
595 H[k + 1][j] = H[k + 1][j] -
p *
y;
600 for (
int i = 0; i <=
std::min(n1, k + 3); i++)
602 p =
x * H[i][k] +
y * H[i][k + 1];
605 p =
p +
z * H[i][k + 2];
606 H[i][k + 2] = H[i][k + 2] -
p *
r;
608 H[i][k] = H[i][k] -
p;
609 H[i][k + 1] = H[i][k + 1] -
p *
q;
614 for (
int i = low; i <= high; i++)
616 p =
x * V[i][k] +
y * V[i][k + 1];
619 p =
p +
z * V[i][k + 2];
620 V[i][k + 2] = V[i][k + 2] -
p *
r;
622 V[i][k] = V[i][k] -
p;
623 V[i][k + 1] = V[i][k + 1] -
p *
q;
637 for (n1 = nn - 1; n1 >= 0; n1--)
648 for (
int i = n1 - 1; i >= 0; i--)
652 for (
int j = l; j <= n1; j++)
654 r =
r + H[i][j] * H[j][n1];
681 q = (d[i] -
p) * (d[i] -
p) + e[i] * e[i];
684 if (std::abs(
x) > std::abs(
z))
686 H[i + 1][n1] = (-
r -
w *
t) /
x;
690 H[i + 1][n1] = (-
s -
y *
t) /
z;
696 t = std::abs(H[i][n1]);
697 if ((
eps *
t) *
t > 1)
699 for (
int j = i; j <= n1; j++)
701 H[j][n1] = H[j][n1] /
t;
714 if (std::abs(H[n1][n1 - 1]) > std::abs(H[n1 - 1][n1]))
716 H[n1 - 1][n1 - 1] =
q / H[n1][n1 - 1];
717 H[n1 - 1][n1] = -(H[n1][n1] -
p) / H[n1][n1 - 1];
721 cdiv(0.0, -H[n1 - 1][n1], H[n1 - 1][n1 - 1] -
p,
q);
722 H[n1 - 1][n1 - 1] = cdivr;
723 H[n1 - 1][n1] = cdivi;
727 for (
int i = n1 - 2; i >= 0; i--)
732 for (
int j = l; j <= n1; j++)
734 ra = ra + H[i][j] * H[j][n1 - 1];
735 sa = sa + H[i][j] * H[j][n1];
750 cdiv(-ra, -sa,
w,
q);
751 H[i][n1 - 1] = cdivr;
761 (d[i] -
p) * (d[i] -
p) + e[i] * e[i] -
q *
q;
762 double vi = (d[i] -
p) * 2.0 *
q;
763 if (vr == 0.0 && vi == 0.0)
766 (std::abs(
w) + std::abs(
q) + std::abs(
x) +
767 std::abs(
y) + std::abs(
z));
770 x *
r -
z * ra +
q * sa,
771 x *
s -
z * sa -
q * ra, vr, vi);
772 H[i][n1 - 1] = cdivr;
774 if (std::abs(
x) > (std::abs(
z) + std::abs(
q)))
777 (-ra -
w * H[i][n1 - 1] +
q * H[i][n1]) /
x;
779 (-sa -
w * H[i][n1] -
q * H[i][n1 - 1]) /
x;
784 -
r -
y * H[i][n1 - 1], -
s -
y * H[i][n1],
z,
786 H[i + 1][n1 - 1] = cdivr;
787 H[i + 1][n1] = cdivi;
794 std::abs(H[i][n1 - 1]), std::abs(H[i][n1]));
795 if ((
eps *
t) *
t > 1)
797 for (
int j = i; j <= n1; j++)
799 H[j][n1 - 1] = H[j][n1 - 1] /
t;
800 H[j][n1] = H[j][n1] /
t;
810 for (
int i = 0; i < nn; i++)
812 if (i < low || i > high)
814 for (
int j = i; j < nn; j++)
823 for (
int j = nn - 1; j >= low; j--)
825 for (
int i = low; i <= high; i++)
828 for (
int k = low; k <=
std::min(j, high); k++)
830 z =
z + V[i][k] * H[k][j];
850 for (
int m = low + 1; m <= high - 1; m++)
855 for (
int i = m; i <= high; i++)
864 for (
int i = high; i >= m; i--)
866 ort[i] = H[i][m - 1] /
scale;
867 h += ort[i] * ort[i];
869 double g = std::sqrt(h);
880 for (
int j = m; j <
n; j++)
883 for (
int i = high; i >= m; i--)
885 f += ort[i] * H[i][j];
888 for (
int i = m; i <= high; i++)
890 H[i][j] -= f * ort[i];
894 for (
int i = 0; i <= high; i++)
897 for (
int j = high; j >= m; j--)
899 f += ort[j] * H[i][j];
902 for (
int j = m; j <= high; j++)
904 H[i][j] -= f * ort[j];
907 ort[m] =
scale * ort[m];
914 for (
int i = 0; i <
n; i++)
916 for (
int j = 0; j <
n; j++)
918 V[i][j] = (i == j ? 1.0 : 0.0);
922 for (
int m = high - 1; m >= low + 1; m--)
924 if (H[m][m - 1] != 0.0)
926 for (
int i = m + 1; i <= high; i++)
928 ort[i] = H[i][m - 1];
930 for (
int j = m; j <= high; j++)
933 for (
int i = m; i <= high; i++)
935 g += ort[i] * V[i][j];
938 g = (
g / ort[m]) / H[m][m - 1];
939 for (
int i = m; i <= high; i++)
941 V[i][j] +=
g * ort[i];
957 for (
int i = 0; i <
n; i++)
972 V = alloc_2d<double>(
n,
n, 0.0);
973 d = alloc_1d<double>(
n);
974 e = alloc_1d<double>(
n);
975 ort = alloc_1d<double>(
n);
981 _eigenvalues.create(1,
n, CV_64FC1);
982 for (
int i = 0; i <
n; i++)
984 _eigenvalues.at<
double>(0, i) = d[i];
987 _eigenvectors.create(
n,
n, CV_64FC1);
988 for (
int i = 0; i <
n; i++)
989 for (
int j = 0; j <
n; j++)
990 _eigenvectors.at<
double>(i, j) = V[i][j];
997 EigenvalueDecomposition() :
n(0) {}
1004 EigenvalueDecomposition(cv::InputArray
src) { compute(
src); }
1010 void compute(cv::InputArray
src)
1020 src.getMat().convertTo(tmp, CV_64FC1);
1024 this->H = alloc_2d<double>(
n,
n);
1026 for (
int i = 0; i < tmp.rows; i++)
1028 for (
int j = 0; j < tmp.cols; j++)
1030 this->H[i][j] = tmp.at<
double>(i, j);
1041 ~EigenvalueDecomposition() {}
1046 cv::Mat eigenvalues() {
return _eigenvalues; }
1051 cv::Mat eigenvectors() {
return _eigenvectors; }
1060 #endif // OPENCV_Check bool is_empty(const cv::Mat *v)
Check for negative values in vector v.
bool compute_pose(cv::Mat &R, cv::Mat &t)
OpenCV function for computing pose.
void build_coeff_matrix(const cv::Mat &pp, cv::Mat &Mtilde, cv::Mat &D)
Build the Maucaulay matrix co-efficients.
Perspective n Point (PnP) Algorithms toolkit for MRPT mrpt_vision_grp.
GLenum GLenum GLenum GLenum GLenum scale
void fill_coeff(const cv::Mat *D)
Fill the hessian functions.
std::vector< double > f1coeff
number of input points
GLdouble GLdouble GLdouble GLdouble q
cv::Mat roty(const double t)
Rotation matrix along y-axis by angle t.
std::vector< double > f2coeff
void compute_eigenvec(const cv::Mat &Mtilde, cv::Mat &eigenval_real, cv::Mat &eigenval_imag, cv::Mat &eigenvec_real, cv::Mat &eigenvec_imag)
Eigen Value Decomposition.
std::vector< cv::Mat > t_est_
GLubyte GLubyte GLubyte GLubyte w
cv::Mat rotz(const double t)
Rotation matrix along z-axis by angle t.
std::vector< cv::Mat > C_est_
coefficient for coefficients matrix
std::vector< double > cost_
std::vector< double > f3coeff
cv::Mat cayley_LS_M(const std::vector< double > &a, const std::vector< double > &b, const std::vector< double > &c, const std::vector< double > &u)
Fill the Maucaulay matrix with co-efficients.
cv::Mat Hessian(const double s[])
Compute the Hessian matrix for the polynomial co-efficient vector s.
double cost__
optimal found solution
cv::Mat rotx(const double t)
Rotation matrix along x-axis by angle t.
cv::Mat LeftMultVec(const cv::Mat &v)
Create a matrix from vector.
GLdouble GLdouble GLdouble r
cv::Mat mean(const cv::Mat &M)
Column-wise mean of matrix M.
cv::Mat cayley2rotbar(const cv::Mat &s)
Cayley parameters to Rotation Matrix.
void run_kernel(const cv::Mat &pp)
Main function to run DLS-PnP.
void init_points(const cv::Mat &opoints, const cv::Mat &ipoints)
Initialization of object points and image points.
GLubyte GLubyte GLubyte a
dls(const cv::Mat &opoints, const cv::Mat &ipoints)
Constructor for DLS class.
bool positive_eigenvalues(const cv::Mat *eigenvalues)
check for positive eigenvalues
CONTAINER::Scalar norm(const CONTAINER &v)
cv::Mat skewsymm(const cv::Mat *X1)
Create a skwy-symmetric matrix from a vector.
cv::Mat C_est__
optimal candidates