20 using namespace Eigen;
28 fovh =
M_PIf * 58.6f / 180.0f;
29 fovv =
M_PIf * 45.6f / 180.0f;
33 width = 640 / (cam_mode * downsample);
34 height = 480 / (cam_mode * downsample);
38 const unsigned int pyr_levels =
39 round(log(
float(
width / cols)) / log(2.f)) + ctf_levels;
40 depth.resize(pyr_levels);
41 depth_old.resize(pyr_levels);
42 depth_inter.resize(pyr_levels);
43 depth_warped.resize(pyr_levels);
44 xx.resize(pyr_levels);
45 xx_inter.resize(pyr_levels);
46 xx_old.resize(pyr_levels);
47 xx_warped.resize(pyr_levels);
48 yy.resize(pyr_levels);
49 yy_inter.resize(pyr_levels);
50 yy_old.resize(pyr_levels);
51 yy_warped.resize(pyr_levels);
52 transformations.resize(pyr_levels);
54 for (
unsigned int i = 0; i < pyr_levels; i++)
56 unsigned int s = pow(2.f,
int(i));
59 depth[i].resize(rows_i, cols_i);
60 depth_inter[i].resize(rows_i, cols_i);
61 depth_old[i].resize(rows_i, cols_i);
62 depth[i].assign(0.0f);
63 depth_old[i].assign(0.0f);
64 xx[i].resize(rows_i, cols_i);
65 xx_inter[i].resize(rows_i, cols_i);
66 xx_old[i].resize(rows_i, cols_i);
68 xx_old[i].assign(0.0f);
69 yy[i].resize(rows_i, cols_i);
70 yy_inter[i].resize(rows_i, cols_i);
71 yy_old[i].resize(rows_i, cols_i);
73 yy_old[i].assign(0.0f);
74 transformations[i].resize(4, 4);
78 depth_warped[i].resize(rows_i, cols_i);
79 xx_warped[i].resize(rows_i, cols_i);
80 yy_warped[i].resize(rows_i, cols_i);
88 previous_speed_const_weight = 0.05f;
89 previous_speed_eig_weight = 0.5f;
90 kai_loc_old.assign(0.f);
99 for (
unsigned int i = 0; i < 4; i++)
100 for (
unsigned int j = 0; j < 4; j++)
101 f_mask(i, j) = v_mask(i) * v_mask(j) / 36.f;
104 float v_mask2[5] = {1, 4, 6, 4, 1};
105 for (
unsigned int i = 0; i < 5; i++)
106 for (
unsigned int j = 0; j < 5; j++)
107 g_mask[i][j] = v_mask2[i] * v_mask2[j] / 256.f;
112 const float max_depth_dif = 0.1f;
115 depth_old.swap(
depth);
124 unsigned int pyr_levels =
125 round(log(
float(
width / cols)) / log(2.f)) + ctf_levels;
128 for (
unsigned int i = 0; i < pyr_levels; i++)
130 unsigned int s = pow(2.f,
int(i));
133 const int rows_i2 = 2 * rows_i;
134 const int cols_i2 = 2 * cols_i;
135 const int i_1 = i - 1;
137 if (i == 0)
depth[i].swap(depth_wf);
143 for (
unsigned int u = 0; u < cols_i; u++)
144 for (
unsigned int v = 0;
v < rows_i;
v++)
146 const int u2 = 2 * u;
147 const int v2 = 2 *
v;
148 const float dcenter =
depth[i_1](
v2,
u2);
151 if ((
v > 0) && (
v < rows_i - 1) && (u > 0) &&
159 for (
int l = -2; l < 3; l++)
160 for (
int k = -2; k < 3; k++)
162 const float abs_dif = abs(
164 if (abs_dif < max_depth_dif)
167 g_mask[2 + k][2 + l] *
168 (max_depth_dif - abs_dif);
178 float min_depth = 10.f;
179 for (
int l = -2; l < 3; l++)
180 for (
int k = -2; k < 3; k++)
182 const float d =
depth[i_1](
v2 + k,
u2 + l);
183 if ((d > 0.f) && (d < min_depth))
187 if (min_depth < 10.f)
188 depth[i](
v, u) = min_depth;
202 for (
int l = -2; l < 3; l++)
203 for (
int k = -2; k < 3; k++)
205 const int indv =
v2 + k, indu =
u2 + l;
206 if ((indv >= 0) && (indv < rows_i2) &&
207 (indu >= 0) && (indu < cols_i2))
209 const float abs_dif = abs(
210 depth[i_1](indv, indu) - dcenter);
211 if (abs_dif < max_depth_dif)
214 g_mask[2 + k][2 + l] *
215 (max_depth_dif - abs_dif);
218 aux_w *
depth[i_1](indv, indu);
226 float min_depth = 10.f;
227 for (
int l = -2; l < 3; l++)
228 for (
int k = -2; k < 3; k++)
230 const int indv =
v2 + k, indu =
u2 + l;
231 if ((indv >= 0) && (indv < rows_i2) &&
232 (indu >= 0) && (indu < cols_i2))
234 const float d =
depth[i_1](indv, indu);
235 if ((d > 0.f) && (d < min_depth))
240 if (min_depth < 10.f)
241 depth[i](
v, u) = min_depth;
250 const float inv_f_i = 2.f * tan(0.5f * fovh) / float(cols_i);
251 const float disp_u_i = 0.5f * (cols_i - 1);
252 const float disp_v_i = 0.5f * (rows_i - 1);
254 for (
unsigned int u = 0; u < cols_i; u++)
255 for (
unsigned int v = 0;
v < rows_i;
v++)
258 xx[i](
v, u) = (u - disp_u_i) *
depth[i](
v, u) * inv_f_i;
259 yy[i](
v, u) = (
v - disp_v_i) *
depth[i](
v, u) * inv_f_i;
271 const float max_depth_dif = 0.1f;
274 depth_old.swap(
depth);
283 unsigned int pyr_levels =
284 round(log(
float(
width / cols)) / log(2.f)) + ctf_levels;
287 for (
unsigned int i = 0; i < pyr_levels; i++)
289 unsigned int s = pow(2.f,
int(i));
294 const int i_1 = i - 1;
296 if (i == 0)
depth[i].swap(depth_wf);
302 for (
unsigned int u = 0; u < cols_i; u++)
303 for (
unsigned int v = 0;
v < rows_i;
v++)
305 const int u2 = 2 * u;
306 const int v2 = 2 *
v;
309 if ((
v > 0) && (
v < rows_i - 1) && (u > 0) &&
312 const Matrix4f d_block =
314 float depths[4] = {d_block(5), d_block(6), d_block(9),
320 for (
signed char k = 2; k >= 0; k--)
321 if (depths[k + 1] < depths[k])
322 std::swap(depths[k + 1], depths[k]);
323 for (
unsigned char k = 1; k < 3; k++)
324 if (depths[k] > depths[k + 1])
325 std::swap(depths[k + 1], depths[k]);
326 if (depths[2] < depths[1])
336 for (
unsigned char k = 0; k < 16; k++)
338 const float abs_dif = abs(d_block(k) - dcenter);
339 if (abs_dif < max_depth_dif)
342 f_mask(k) * (max_depth_dif - abs_dif);
344 sum += aux_w * d_block(k);
356 const Matrix2f d_block =
depth[i_1].block<2, 2>(
v2,
u2);
357 const float new_d = 0.25f * d_block.sumAll();
367 const float inv_f_i = 2.f * tan(0.5f * fovh) / float(cols_i);
368 const float disp_u_i = 0.5f * (cols_i - 1);
369 const float disp_v_i = 0.5f * (rows_i - 1);
371 for (
unsigned int u = 0; u < cols_i; u++)
372 for (
unsigned int v = 0;
v < rows_i;
v++)
375 xx[i](
v, u) = (u - disp_u_i) *
depth[i](
v, u) * inv_f_i;
376 yy[i](
v, u) = (
v - disp_v_i) *
depth[i](
v, u) * inv_f_i;
389 const float f = float(cols_i) / (2.f * tan(0.5f * fovh));
390 const float disp_u_i = 0.5f * float(cols_i - 1);
391 const float disp_v_i = 0.5f * float(rows_i - 1);
395 acu_trans.setIdentity();
396 for (
unsigned int i = 1; i <=
level; i++)
397 acu_trans = transformations[i - 1] * acu_trans;
399 MatrixXf wacu(rows_i, cols_i);
401 depth_warped[image_level].assign(0.f);
403 const float cols_lim = float(cols_i - 1);
404 const float rows_lim = float(rows_i - 1);
408 for (
unsigned int j = 0; j < cols_i; j++)
409 for (
unsigned int i = 0; i < rows_i; i++)
411 const float z =
depth[image_level](i, j);
416 const float depth_w = acu_trans(0, 0) *
z +
417 acu_trans(0, 1) * xx[image_level](i, j) +
418 acu_trans(0, 2) * yy[image_level](i, j) +
420 const float x_w = acu_trans(1, 0) *
z +
421 acu_trans(1, 1) * xx[image_level](i, j) +
422 acu_trans(1, 2) * yy[image_level](i, j) +
424 const float y_w = acu_trans(2, 0) *
z +
425 acu_trans(2, 1) * xx[image_level](i, j) +
426 acu_trans(2, 2) * yy[image_level](i, j) +
430 const float uwarp = f * x_w / depth_w + disp_u_i;
431 const float vwarp = f * y_w / depth_w + disp_v_i;
435 if ((uwarp >= 0.f) && (uwarp < cols_lim) && (vwarp >= 0.f) &&
438 const int uwarp_l = uwarp;
439 const int uwarp_r = uwarp_l + 1;
440 const int vwarp_d = vwarp;
441 const int vwarp_u = vwarp_d + 1;
442 const float delta_r = float(uwarp_r) - uwarp;
443 const float delta_l = uwarp - float(uwarp_l);
444 const float delta_u = float(vwarp_u) - vwarp;
445 const float delta_d = vwarp - float(vwarp_d);
448 if (abs(
round(uwarp) - uwarp) + abs(
round(vwarp) - vwarp) <
451 depth_warped[image_level](
round(vwarp),
round(uwarp)) +=
458 depth_warped[image_level](vwarp_u, uwarp_r) +=
460 wacu(vwarp_u, uwarp_r) += w_ur;
463 depth_warped[image_level](vwarp_u, uwarp_l) +=
465 wacu(vwarp_u, uwarp_l) += w_ul;
468 depth_warped[image_level](vwarp_d, uwarp_r) +=
470 wacu(vwarp_d, uwarp_r) += w_dr;
473 depth_warped[image_level](vwarp_d, uwarp_l) +=
475 wacu(vwarp_d, uwarp_l) += w_dl;
482 const float inv_f_i = 1.f / f;
483 for (
unsigned int u = 0; u < cols_i; u++)
484 for (
unsigned int v = 0;
v < rows_i;
v++)
486 if (wacu(
v, u) > 0.f)
488 depth_warped[image_level](
v, u) /= wacu(
v, u);
489 xx_warped[image_level](
v, u) =
490 (u - disp_u_i) * depth_warped[image_level](
v, u) * inv_f_i;
491 yy_warped[image_level](
v, u) =
492 (
v - disp_v_i) * depth_warped[image_level](
v, u) * inv_f_i;
496 depth_warped[image_level](
v, u) = 0.f;
497 xx_warped[image_level](
v, u) = 0.f;
498 yy_warped[image_level](
v, u) = 0.f;
505 null.resize(rows_i, cols_i);
507 num_valid_points = 0;
509 for (
unsigned int u = 0; u < cols_i; u++)
510 for (
unsigned int v = 0;
v < rows_i;
v++)
512 if ((depth_old[image_level](
v, u)) == 0.f ||
513 (depth_warped[image_level](
v, u) == 0.f))
515 depth_inter[image_level](
v, u) = 0.f;
516 xx_inter[image_level](
v, u) = 0.f;
517 yy_inter[image_level](
v, u) = 0.f;
522 depth_inter[image_level](
v, u) =
523 0.5f * (depth_old[image_level](
v, u) +
524 depth_warped[image_level](
v, u));
525 xx_inter[image_level](
v, u) =
527 (xx_old[image_level](
v, u) + xx_warped[image_level](
v, u));
528 yy_inter[image_level](
v, u) =
530 (yy_old[image_level](
v, u) + yy_warped[image_level](
v, u));
532 if ((u > 0) && (
v > 0) && (u < cols_i - 1) && (
v < rows_i - 1))
540 dt.resize(rows_i, cols_i);
542 du.resize(rows_i, cols_i);
544 dv.resize(rows_i, cols_i);
548 MatrixXf rx_ninv(rows_i, cols_i);
549 MatrixXf ry_ninv(rows_i, cols_i);
553 for (
unsigned int u = 0; u < cols_i - 1; u++)
554 for (
unsigned int v = 0;
v < rows_i;
v++)
555 if (
null(
v, u) ==
false)
557 rx_ninv(
v, u) = sqrtf(
559 xx_inter[image_level](
v, u + 1) -
560 xx_inter[image_level](
v, u)) +
562 depth_inter[image_level](
v, u + 1) -
563 depth_inter[image_level](
v, u)));
566 for (
unsigned int u = 0; u < cols_i; u++)
567 for (
unsigned int v = 0;
v < rows_i - 1;
v++)
568 if (
null(
v, u) ==
false)
570 ry_ninv(
v, u) = sqrtf(
572 yy_inter[image_level](
v + 1, u) -
573 yy_inter[image_level](
v, u)) +
575 depth_inter[image_level](
v + 1, u) -
576 depth_inter[image_level](
v, u)));
580 for (
unsigned int v = 0;
v < rows_i;
v++)
582 for (
unsigned int u = 1; u < cols_i - 1; u++)
583 if (
null(
v, u) ==
false)
585 (rx_ninv(
v, u - 1) * (depth_inter[image_level](
v, u + 1) -
586 depth_inter[image_level](
v, u)) +
587 rx_ninv(
v, u) * (depth_inter[image_level](
v, u) -
588 depth_inter[image_level](
v, u - 1))) /
589 (rx_ninv(
v, u) + rx_ninv(
v, u - 1));
592 du(
v, cols_i - 1) = du(
v, cols_i - 2);
595 for (
unsigned int u = 0; u < cols_i; u++)
597 for (
unsigned int v = 1;
v < rows_i - 1;
v++)
598 if (
null(
v, u) ==
false)
600 (ry_ninv(
v - 1, u) * (depth_inter[image_level](
v + 1, u) -
601 depth_inter[image_level](
v, u)) +
602 ry_ninv(
v, u) * (depth_inter[image_level](
v, u) -
603 depth_inter[image_level](
v - 1, u))) /
604 (ry_ninv(
v, u) + ry_ninv(
v - 1, u));
607 dv(rows_i - 1, u) = dv(rows_i - 2, u);
611 for (
unsigned int u = 0; u < cols_i; u++)
612 for (
unsigned int v = 0;
v < rows_i;
v++)
613 if (
null(
v, u) ==
false)
614 dt(
v, u) = fps * (depth_warped[image_level](
v, u) -
615 depth_old[image_level](
v, u));
620 weights.resize(rows_i, cols_i);
625 Matrix<float, 6, 1> kai_level = kai_loc_old;
628 acu_trans.setIdentity();
629 for (
unsigned int i = 0; i <
level; i++)
630 acu_trans = transformations[i] * acu_trans;
636 kai_level -= kai_level_acu.cast<
float>();
639 const float f_inv = float(cols_i) / (2.f * tan(0.5f * fovh));
640 const float kz2 = 8.122e-12f;
643 const float kduv = 20e-5f;
644 const float kdt = kduv /
square(fps);
645 const float k2dt = 5e-6f;
646 const float k2duv = 5e-6f;
648 for (
unsigned int u = 1; u < cols_i - 1; u++)
649 for (
unsigned int v = 1;
v < rows_i - 1;
v++)
650 if (
null(
v, u) ==
false)
654 const float z = depth_inter[image_level](
v, u);
655 const float inv_d = 1.f /
z;
658 const float z2 =
z *
z;
659 const float z4 = z2 * z2;
672 const float var44 = kz2 * z4 *
square(fps);
673 const float var55 = kz2 * z4 * 0.25f;
674 const float var66 = var55;
694 const float j4 = 1.f;
696 xx_inter[image_level](
v, u) * inv_d * inv_d * f_inv *
698 yy_inter[image_level](
v, u) * kai_level[4] -
699 xx_inter[image_level](
v, u) * kai_level[5]) +
701 (-kai_level[1] -
z * kai_level[5] +
702 yy_inter[image_level](
v, u) * kai_level[3]);
704 yy_inter[image_level](
v, u) * inv_d * inv_d * f_inv *
706 yy_inter[image_level](
v, u) * kai_level[4] -
707 xx_inter[image_level](
v, u) * kai_level[5]) +
709 (-kai_level[2] +
z * kai_level[4] -
710 xx_inter[image_level](
v, u) * kai_level[3]);
719 const float error_m =
720 j4 * j4 * var44 + j5 * j5 * var55 + j6 * j6 * var66;
724 const float ini_du = depth_old[image_level](
v, u + 1) -
725 depth_old[image_level](
v, u - 1);
726 const float ini_dv = depth_old[image_level](
v + 1, u) -
727 depth_old[image_level](
v - 1, u);
728 const float final_du = depth_warped[image_level](
v, u + 1) -
729 depth_warped[image_level](
v, u - 1);
730 const float final_dv = depth_warped[image_level](
v + 1, u) -
731 depth_warped[image_level](
v - 1, u);
733 const float dut = ini_du - final_du;
734 const float dvt = ini_dv - final_dv;
735 const float duu = du(
v, u + 1) - du(
v, u - 1);
736 const float dvv = dv(
v + 1, u) - dv(
v - 1, u);
741 const float error_l =
748 weights(
v, u) = sqrt(1.f / (error_m + error_l));
752 const float inv_max = 1.f /
weights.maximum();
758 MatrixXf A(num_valid_points, 6);
759 MatrixXf B(num_valid_points, 1);
760 unsigned int cont = 0;
767 const float f_inv = float(cols_i) / (2.f * tan(0.5f * fovh));
769 for (
unsigned int u = 1; u < cols_i - 1; u++)
770 for (
unsigned int v = 1;
v < rows_i - 1;
v++)
771 if (
null(
v, u) ==
false)
774 const float d = depth_inter[image_level](
v, u);
775 const float inv_d = 1.f / d;
776 const float x = xx_inter[image_level](
v, u);
777 const float y = yy_inter[image_level](
v, u);
778 const float dycomp = du(
v, u) * f_inv * inv_d;
779 const float dzcomp = dv(
v, u) * f_inv * inv_d;
784 tw * (1.f + dycomp *
x * inv_d + dzcomp *
y * inv_d);
785 A(cont, 1) = tw * (-dycomp);
786 A(cont, 2) = tw * (-dzcomp);
787 A(cont, 3) = tw * (dycomp *
y - dzcomp *
x);
788 A(cont, 4) = tw * (
y + dycomp * inv_d *
y *
x +
789 dzcomp * (
y *
y * inv_d + d));
790 A(cont, 5) = tw * (-
x - dycomp * (
x *
x * inv_d + d) -
791 dzcomp * inv_d *
y *
x);
792 B(cont, 0) = tw * (-dt(
v, u));
800 AtB.multiply_AtB(A, B);
801 MatrixXf Var = AtA.ldlt().solve(AtB);
805 for (
unsigned int k = 0; k < 6; k++)
res += Var(k) * A.col(k);
808 (1.f / float(num_valid_points - 6)) * AtA.inverse() *
res.squaredNorm();
822 buildCoordinatesPyramidFast();
824 buildCoordinatesPyramid();
827 for (
unsigned int i = 0; i < ctf_levels; i++)
830 transformations[i].setIdentity();
833 unsigned int s = pow(2.f,
int(ctf_levels - (i + 1)));
837 ctf_levels - i +
round(log(
float(
width / cols)) / log(2.f)) - 1;
842 depth_warped[image_level] =
depth[image_level];
843 xx_warped[image_level] = xx[image_level];
844 yy_warped[image_level] = yy[image_level];
853 calculateDepthDerivatives();
859 if (num_valid_points > 6) solveOneLevel();
862 filterLevelSolution();
869 execution_time = 1000.f * clock.
Tac();
877 if (eigensolver.info() != Success)
879 printf(
"\n Eigensolver couldn't find a solution. Pose is not updated");
886 Matrix<float, 6, 6> Bii;
887 Matrix<float, 6, 1> kai_b;
888 Bii = eigensolver.eigenvectors();
889 kai_b = Bii.colPivHouseholderQr().solve(kai_loc_level);
894 Matrix<float, 6, 1> kai_loc_sub = kai_loc_old;
899 acu_trans.setIdentity();
900 for (
unsigned int i = 0; i <
level; i++)
901 acu_trans = transformations[i] * acu_trans;
906 kai_loc_sub -= kai_level_acu.cast<
float>();
915 Matrix<float, 6, 1> kai_b_old;
916 kai_b_old = Bii.colPivHouseholderQr().solve(kai_loc_sub);
920 const float cf = previous_speed_eig_weight * expf(-
int(
level)),
921 df = previous_speed_const_weight * expf(-
int(
level));
922 Matrix<float, 6, 1> kai_b_fil;
923 for (
unsigned int i = 0; i < 6; i++)
926 (cf * eigensolver.eigenvalues()(i, 0) + df) * kai_b_old(i)) /
927 (1.f + cf * eigensolver.eigenvalues()(i) + df);
930 Matrix<float, 6, 1> kai_loc_fil =
931 Bii.inverse().colPivHouseholderQr().solve(kai_b_fil);
937 aux2 = aux1.
exp(aux_vel);
939 transformations[
level] = trans.cast<
float>();
947 acu_trans.setIdentity();
948 for (
unsigned int i = 1; i <= ctf_levels; i++)
949 acu_trans = transformations[i - 1] * acu_trans;
956 kai_loc = kai_level_acu.cast<
float>();
974 cam_pose.getRotationMatrix(inv_trans);
975 v_abs = inv_trans.cast<
float>() * kai_loc.topRows(3);
976 w_abs = inv_trans.cast<
float>() * kai_loc.bottomRows(3);
977 kai_abs.topRows<3>() = v_abs;
978 kai_abs.bottomRows<3>() = w_abs;
982 cam_oldpose = cam_pose;
985 cam_pose = cam_pose + pose_aux;
991 kai_loc_old.topRows<3>() =
992 inv_trans.inverse().cast<
float>() * kai_abs.topRows(3);
993 kai_loc_old.bottomRows<3>() =
994 inv_trans.inverse().cast<
float>() * kai_abs.bottomRows(3);
999 fovh =
M_PI * new_fovh / 180.0;
1000 fovv =
M_PI * new_fovv / 180.0;
1005 x.resize(rows, cols);
1006 y.resize(rows, cols);
1007 z.resize(rows, cols);
1015 MatrixXf& cur_du, MatrixXf& cur_dv, MatrixXf& cur_dt)
1017 cur_du.resize(rows, cols);
1018 cur_dv.resize(rows, cols);
1019 cur_dt.resize(rows, cols);
1028 w.resize(rows, cols);