13 #include <mrpt/otherlibs/do_opencv_includes.h>
39 dls(
const cv::Mat& opoints,
const cv::Mat& ipoints);
51 template <
typename Opo
intType,
typename Ipo
intType>
52 void init_points(
const cv::Mat& opoints,
const cv::Mat& ipoints)
54 for (
int i = 0; i <
N; i++)
56 p.at<
double>(0, i) = opoints.at<OpointType>(i, 0);
57 p.at<
double>(1, i) = opoints.at<OpointType>(i, 1);
58 p.at<
double>(2, i) = opoints.at<OpointType>(i, 2);
61 mn.at<
double>(0) +=
p.at<
double>(0, i);
62 mn.at<
double>(1) +=
p.at<
double>(1, i);
63 mn.at<
double>(2) +=
p.at<
double>(2, i);
66 double sr = std::pow(ipoints.at<IpointType>(i, 0), 2) +
67 std::pow(ipoints.at<IpointType>(i, 1), 2) + (double)1;
70 z.at<
double>(0, i) = ipoints.at<IpointType>(i, 0) / sr;
71 z.at<
double>(1, i) = ipoints.at<IpointType>(i, 1) / sr;
72 z.at<
double>(2, i) = (
double)1 / sr;
75 mn.at<
double>(0) /= (
double)
N;
76 mn.at<
double>(1) /= (
double)
N;
77 mn.at<
double>(2) /= (
double)
N;
111 const cv::Mat& Mtilde, cv::Mat& eigenval_real, cv::Mat& eigenval_imag,
112 cv::Mat& eigenvec_real, cv::Mat& eigenvec_imag);
129 const std::vector<double>&
a,
const std::vector<double>&
b,
130 const std::vector<double>&
c,
const std::vector<double>& u);
152 cv::Mat
skewsymm(
const cv::Mat* X1);
159 cv::Mat
rotx(
const double t);
166 cv::Mat
roty(
const double t);
173 cv::Mat
rotz(
const double t);
180 cv::Mat
mean(
const cv::Mat& M);
208 class EigenvalueDecomposition
218 cv::Mat _eigenvalues;
220 cv::Mat _eigenvectors;
227 template <
typename _Tp>
239 template <
typename _Tp>
240 _Tp* alloc_1d(
int m, _Tp
val)
242 _Tp* arr = alloc_1d<_Tp>(m);
243 for (
int i = 0; i < m; i++) arr[i] =
val;
253 template <
typename _Tp>
254 _Tp** alloc_2d(
int m,
int _n)
256 _Tp** arr =
new _Tp*[m];
257 for (
int i = 0; i < m; i++) arr[i] =
new _Tp[_n];
268 template <
typename _Tp>
269 _Tp** alloc_2d(
int m,
int _n, _Tp
val)
271 _Tp** arr = alloc_2d<_Tp>(m, _n);
272 for (
int i = 0; i < m; i++)
274 for (
int j = 0; j < _n; j++)
289 void cdiv(
double xr,
double xi,
double yr,
double yi)
292 if (std::abs(yr) > std::abs(yi))
296 cdivr = (xr +
r * xi) / dv;
297 cdivi = (xi -
r * xr) / dv;
303 cdivr = (
r * xr + xi) / dv;
304 cdivi = (
r * xi - xr) / dv;
323 double eps = std::pow(2.0, -52.0);
324 double exshift = 0.0;
325 double p = 0,
q = 0,
r = 0,
s = 0,
z = 0,
t,
w,
x,
y;
330 for (
int i = 0; i < nn; i++)
332 if (i < low || i > high)
337 for (
int j = std::max(i - 1, 0); j < nn; j++)
351 s = std::abs(H[l - 1][l - 1]) + std::abs(H[l][l]);
356 if (std::abs(H[l][l - 1]) <
eps *
s)
368 H[n1][n1] = H[n1][n1] + exshift;
376 else if (l == n1 - 1)
378 w = H[n1][n1 - 1] * H[n1 - 1][n1];
379 p = (H[n1 - 1][n1 - 1] - H[n1][n1]) / 2.0;
381 z = std::sqrt(std::abs(
q));
382 H[n1][n1] = H[n1][n1] + exshift;
383 H[n1 - 1][n1 - 1] = H[n1 - 1][n1 - 1] + exshift;
407 s = std::abs(
x) + std::abs(
z);
410 r = std::sqrt(
p *
p +
q *
q);
416 for (
int j = n1 - 1; j < nn; j++)
419 H[n1 - 1][j] =
q *
z +
p * H[n1][j];
420 H[n1][j] =
q * H[n1][j] -
p *
z;
425 for (
int i = 0; i <= n1; i++)
428 H[i][n1 - 1] =
q *
z +
p * H[i][n1];
429 H[i][n1] =
q * H[i][n1] -
p *
z;
434 for (
int i = low; i <= high; i++)
437 V[i][n1 - 1] =
q *
z +
p * V[i][n1];
438 V[i][n1] =
q * V[i][n1] -
p *
z;
464 y = H[n1 - 1][n1 - 1];
465 w = H[n1][n1 - 1] * H[n1 - 1][n1];
473 for (
int i = low; i <= n1; i++)
477 s = std::abs(H[n1][n1 - 1]) + std::abs(H[n1 - 1][n1 - 2]);
495 s =
x -
w / ((
y -
x) / 2.0 +
s);
496 for (
int i = low; i <= n1; i++)
514 p = (
r *
s -
w) / H[m + 1][m] + H[m][m + 1];
515 q = H[m + 1][m + 1] -
z -
r -
s;
517 s = std::abs(
p) + std::abs(
q) + std::abs(
r);
525 if (std::abs(H[m][m - 1]) * (std::abs(
q) + std::abs(
r)) <
527 (std::abs(H[m - 1][m - 1]) + std::abs(
z) +
528 std::abs(H[m + 1][m + 1]))))
535 for (
int i = m + 2; i <= n1; i++)
546 for (
int k = m; k <= n1 - 1; k++)
548 bool notlast = (k != n1 - 1);
553 r = (notlast ? H[k + 2][k - 1] : 0.0);
554 x = std::abs(
p) + std::abs(
q) + std::abs(
r);
566 s = std::sqrt(
p *
p +
q *
q +
r *
r);
575 H[k][k - 1] = -
s *
x;
579 H[k][k - 1] = -H[k][k - 1];
590 for (
int j = k; j < nn; j++)
592 p = H[k][j] +
q * H[k + 1][j];
595 p =
p +
r * H[k + 2][j];
596 H[k + 2][j] = H[k + 2][j] -
p *
z;
598 H[k][j] = H[k][j] -
p *
x;
599 H[k + 1][j] = H[k + 1][j] -
p *
y;
604 for (
int i = 0; i <=
std::min(n1, k + 3); i++)
606 p =
x * H[i][k] +
y * H[i][k + 1];
609 p =
p +
z * H[i][k + 2];
610 H[i][k + 2] = H[i][k + 2] -
p *
r;
612 H[i][k] = H[i][k] -
p;
613 H[i][k + 1] = H[i][k + 1] -
p *
q;
618 for (
int i = low; i <= high; i++)
620 p =
x * V[i][k] +
y * V[i][k + 1];
623 p =
p +
z * V[i][k + 2];
624 V[i][k + 2] = V[i][k + 2] -
p *
r;
626 V[i][k] = V[i][k] -
p;
627 V[i][k + 1] = V[i][k + 1] -
p *
q;
641 for (n1 = nn - 1; n1 >= 0; n1--)
652 for (
int i = n1 - 1; i >= 0; i--)
656 for (
int j = l; j <= n1; j++)
658 r =
r + H[i][j] * H[j][n1];
685 q = (d[i] -
p) * (d[i] -
p) + e[i] * e[i];
688 if (std::abs(
x) > std::abs(
z))
690 H[i + 1][n1] = (-
r -
w *
t) /
x;
694 H[i + 1][n1] = (-
s -
y *
t) /
z;
700 t = std::abs(H[i][n1]);
701 if ((
eps *
t) *
t > 1)
703 for (
int j = i; j <= n1; j++)
705 H[j][n1] = H[j][n1] /
t;
718 if (std::abs(H[n1][n1 - 1]) > std::abs(H[n1 - 1][n1]))
720 H[n1 - 1][n1 - 1] =
q / H[n1][n1 - 1];
721 H[n1 - 1][n1] = -(H[n1][n1] -
p) / H[n1][n1 - 1];
725 cdiv(0.0, -H[n1 - 1][n1], H[n1 - 1][n1 - 1] -
p,
q);
726 H[n1 - 1][n1 - 1] = cdivr;
727 H[n1 - 1][n1] = cdivi;
731 for (
int i = n1 - 2; i >= 0; i--)
736 for (
int j = l; j <= n1; j++)
738 ra = ra + H[i][j] * H[j][n1 - 1];
739 sa = sa + H[i][j] * H[j][n1];
754 cdiv(-ra, -sa,
w,
q);
755 H[i][n1 - 1] = cdivr;
765 (d[i] -
p) * (d[i] -
p) + e[i] * e[i] -
q *
q;
766 double vi = (d[i] -
p) * 2.0 *
q;
767 if (vr == 0.0 && vi == 0.0)
770 (std::abs(
w) + std::abs(
q) + std::abs(
x) +
771 std::abs(
y) + std::abs(
z));
774 x *
r -
z * ra +
q * sa,
775 x *
s -
z * sa -
q * ra, vr, vi);
776 H[i][n1 - 1] = cdivr;
778 if (std::abs(
x) > (std::abs(
z) + std::abs(
q)))
781 (-ra -
w * H[i][n1 - 1] +
q * H[i][n1]) /
x;
783 (-sa -
w * H[i][n1] -
q * H[i][n1 - 1]) /
x;
788 -
r -
y * H[i][n1 - 1], -
s -
y * H[i][n1],
z,
790 H[i + 1][n1 - 1] = cdivr;
791 H[i + 1][n1] = cdivi;
798 std::abs(H[i][n1 - 1]), std::abs(H[i][n1]));
799 if ((
eps *
t) *
t > 1)
801 for (
int j = i; j <= n1; j++)
803 H[j][n1 - 1] = H[j][n1 - 1] /
t;
804 H[j][n1] = H[j][n1] /
t;
814 for (
int i = 0; i < nn; i++)
816 if (i < low || i > high)
818 for (
int j = i; j < nn; j++)
827 for (
int j = nn - 1; j >= low; j--)
829 for (
int i = low; i <= high; i++)
832 for (
int k = low; k <=
std::min(j, high); k++)
834 z =
z + V[i][k] * H[k][j];
854 for (
int m = low + 1; m <= high - 1; m++)
859 for (
int i = m; i <= high; i++)
868 for (
int i = high; i >= m; i--)
870 ort[i] = H[i][m - 1] /
scale;
871 h += ort[i] * ort[i];
873 double g = std::sqrt(h);
884 for (
int j = m; j <
n; j++)
887 for (
int i = high; i >= m; i--)
889 f += ort[i] * H[i][j];
892 for (
int i = m; i <= high; i++)
894 H[i][j] -= f * ort[i];
898 for (
int i = 0; i <= high; i++)
901 for (
int j = high; j >= m; j--)
903 f += ort[j] * H[i][j];
906 for (
int j = m; j <= high; j++)
908 H[i][j] -= f * ort[j];
911 ort[m] =
scale * ort[m];
918 for (
int i = 0; i <
n; i++)
920 for (
int j = 0; j <
n; j++)
922 V[i][j] = (i == j ? 1.0 : 0.0);
926 for (
int m = high - 1; m >= low + 1; m--)
928 if (H[m][m - 1] != 0.0)
930 for (
int i = m + 1; i <= high; i++)
932 ort[i] = H[i][m - 1];
934 for (
int j = m; j <= high; j++)
937 for (
int i = m; i <= high; i++)
939 g += ort[i] * V[i][j];
942 g = (
g / ort[m]) / H[m][m - 1];
943 for (
int i = m; i <= high; i++)
945 V[i][j] +=
g * ort[i];
961 for (
int i = 0; i <
n; i++)
976 V = alloc_2d<double>(
n,
n, 0.0);
977 d = alloc_1d<double>(
n);
978 e = alloc_1d<double>(
n);
979 ort = alloc_1d<double>(
n);
985 _eigenvalues.create(1,
n, CV_64FC1);
986 for (
int i = 0; i <
n; i++)
988 _eigenvalues.at<
double>(0, i) = d[i];
991 _eigenvectors.create(
n,
n, CV_64FC1);
992 for (
int i = 0; i <
n; i++)
993 for (
int j = 0; j <
n; j++)
994 _eigenvectors.at<
double>(i, j) = V[i][j];
1001 EigenvalueDecomposition() :
n(0) {}
1008 EigenvalueDecomposition(cv::InputArray
src) { compute(
src); }
1014 void compute(cv::InputArray
src)
1024 src.getMat().convertTo(tmp, CV_64FC1);
1028 this->H = alloc_2d<double>(
n,
n);
1030 for (
int i = 0; i < tmp.rows; i++)
1032 for (
int j = 0; j < tmp.cols; j++)
1034 this->H[i][j] = tmp.at<
double>(i, j);
1045 ~EigenvalueDecomposition() {}
1050 cv::Mat eigenvalues() {
return _eigenvalues; }
1055 cv::Mat eigenvectors() {
return _eigenvectors; }
1066 #endif // OPENCV_Check