193 float* row0 =
new float[src.
width + 2];
194 float* row1 =
new float[src.
width + 2];
199 const float* src_ptr = &(src[0]);
201 for (
int y = 0; y < height; y++) {
202 const float* srow0 = src_ptr + (y > 0 ? y - 1 : height > 1 ? 1 : 0) * width;
203 const float* srow1 = src_ptr + y * width;
205 src_ptr + (y < height - 1 ? y + 1 : height > 1 ? height - 2 : 0) * width;
206 float* grad_x_row = &(grad_x[y * width]);
207 float* grad_y_row = &(grad_y[y * width]);
210 for (
int x = 0; x < width; x++) {
211 trow0[x] = (srow0[x] + srow2[x]) * 3 + srow1[x] * 10;
212 trow1[x] = srow2[x] - srow0[x];
216 int x0 = width > 1 ? 1 : 0, x1 = width > 1 ? width - 2 : 0;
217 trow0[-1] = trow0[x0];
218 trow0[width] = trow0[x1];
219 trow1[-1] = trow1[x0];
220 trow1[width] = trow1[x1];
223 for (
int x = 0; x < width; x++) {
224 grad_x_row[x] = trow0[x + 1] - trow0[x - 1];
225 grad_y_row[x] = (trow1[x + 1] + trow1[x - 1]) * 3 + trow1[x] * 10;
476 const Eigen::Array2i& location,
477 const Eigen::Array4f& weight,
478 Eigen::ArrayXXf& win,
479 Eigen::ArrayXXf& grad_x_win,
480 Eigen::ArrayXXf& grad_y_win,
481 Eigen::Array3f& covariance)
const
483 const int step = img.
width;
484 covariance.setZero();
487 const float* img_ptr = &(img[0]) + (y + location[1]) * step + location[0];
488 const float* grad_x_ptr = &(grad_x[0]) + (y + location[1]) * step + location[0];
489 const float* grad_y_ptr = &(grad_y[0]) + (y + location[1]) * step + location[0];
491 float* win_ptr = win.data() + y * win.cols();
492 float* grad_x_win_ptr = grad_x_win.data() + y * grad_x_win.cols();
493 float* grad_y_win_ptr = grad_y_win.data() + y * grad_y_win.cols();
495 for (
int x = 0; x <
track_width_; ++x, ++grad_x_ptr, ++grad_y_ptr) {
496 *win_ptr++ = img_ptr[x] * weight[0] + img_ptr[x + 1] * weight[1] +
497 img_ptr[x + step] * weight[2] + img_ptr[x + step + 1] * weight[3];
498 float ixval = grad_x_ptr[0] * weight[0] + grad_x_ptr[1] * weight[1] +
499 grad_x_ptr[step] * weight[2] + grad_x_ptr[step + 1] * weight[3];
500 float iyval = grad_y_ptr[0] * weight[0] + grad_y_ptr[1] * weight[1] +
501 grad_y_ptr[step] * weight[2] + grad_y_ptr[step + 1] * weight[3];
503 *grad_x_win_ptr++ = ixval;
504 *grad_y_win_ptr++ = iyval;
506 covariance[0] += ixval * ixval;
507 covariance[1] += ixval * iyval;
508 covariance[2] += iyval * iyval;
517 const Eigen::ArrayXXf& prev,
518 const Eigen::ArrayXXf& prev_grad_x,
519 const Eigen::ArrayXXf& prev_grad_y,
521 const Eigen::Array2i& location,
522 const Eigen::Array4f& weight,
523 Eigen::Array2f& b)
const
525 const int step = next.
width;
528 const float* next_ptr = &(next[0]) + (y + location[1]) * step + location[0];
529 const float* prev_ptr = prev.data() + y * prev.cols();
530 const float* prev_grad_x_ptr = prev_grad_x.data() + y * prev_grad_x.cols();
531 const float* prev_grad_y_ptr = prev_grad_y.data() + y * prev_grad_y.cols();
533 for (
int x = 0; x <
track_width_; ++x, ++prev_grad_y_ptr, ++prev_grad_x_ptr) {
534 float diff = next_ptr[x] * weight[0] + next_ptr[x + 1] * weight[1] +
535 next_ptr[x + step] * weight[2] + next_ptr[x + step + 1] * weight[3] -
537 b[0] += *prev_grad_x_ptr * diff;
538 b[1] += *prev_grad_y_ptr * diff;
549 const std::vector<FloatImageConstPtr>& prev_pyramid,
550 const std::vector<FloatImageConstPtr>& pyramid,
553 std::vector<int>& status,
554 Eigen::Affine3f& motion)
const
556 std::vector<Eigen::Array2f, Eigen::aligned_allocator<Eigen::Array2f>> next_pts(
557 prev_keypoints->
size());
560 const int nb_points = prev_keypoints->
size();
561 for (
int level =
nb_levels_ - 1; level >= 0; --level) {
562 const FloatImage& prev = *(prev_pyramid[level * 3]);
563 const FloatImage& next = *(pyramid[level * 3]);
564 const FloatImage& grad_x = *(prev_pyramid[level * 3 + 1]);
565 const FloatImage& grad_y = *(prev_pyramid[level * 3 + 2]);
570 float ratio(1. / (1 << level));
571 for (
int ptidx = 0; ptidx < nb_points; ptidx++) {
572 Eigen::Array2f prev_pt((*prev_keypoints)[ptidx].u * ratio,
573 (*prev_keypoints)[ptidx].v * ratio);
574 Eigen::Array2f next_pt;
578 next_pt = next_pts[ptidx] * 2.f;
580 next_pts[ptidx] = next_pt;
582 Eigen::Array2i iprev_point;
584 iprev_point[0] = std::floor(prev_pt[0]);
585 iprev_point[1] = std::floor(prev_pt[1]);
588 (std::uint32_t)iprev_point[0] >= grad_x.
width ||
590 (std::uint32_t)iprev_point[1] >= grad_y.
height) {
596 float a = prev_pt[0] - iprev_point[0];
597 float b = prev_pt[1] - iprev_point[1];
598 Eigen::Array4f weight;
599 weight[0] = (1.f - a) * (1.f - b);
600 weight[1] = a * (1.f - b);
601 weight[2] = (1.f - a) * b;
602 weight[3] = 1 - weight[0] - weight[1] - weight[2];
604 Eigen::Array3f covar = Eigen::Array3f::Zero();
615 float det = covar[0] * covar[2] - covar[1] * covar[1];
616 float min_eigenvalue = (covar[2] + covar[0] -
617 std::sqrt((covar[0] - covar[2]) * (covar[0] - covar[2]) +
618 4.f * covar[1] * covar[1])) /
622 det < std::numeric_limits<float>::epsilon()) {
630 Eigen::Array2f prev_delta(0, 0);
632 Eigen::Array2i inext_pt = next_pt.floor().cast<
int>();
641 a = next_pt[0] - inext_pt[0];
642 b = next_pt[1] - inext_pt[1];
643 weight[0] = (1.f - a) * (1.f - b);
644 weight[1] = a * (1.f - b);
645 weight[2] = (1.f - a) * b;
646 weight[3] = 1 - weight[0] - weight[1] - weight[2];
648 Eigen::Array2f beta = Eigen::Array2f::Zero();
649 mismatchVector(prev_win, grad_x_win, grad_y_win, next, inext_pt, weight, beta);
651 Eigen::Vector2f delta((covar[1] * beta[1] - covar[2] * beta[0]) * det,
652 (covar[1] * beta[0] - covar[0] * beta[1]) * det);
654 next_pt[0] += delta[0];
655 next_pt[1] += delta[1];
656 next_pts[ptidx] = next_pt + half_win;
658 if (delta.squaredNorm() <=
epsilon_)
661 if (j > 0 && std::abs(delta[0] + prev_delta[0]) < 0.01 &&
662 std::abs(delta[1] + prev_delta[1]) < 0.01) {
663 next_pts[ptidx][0] -= delta[0] * 0.5f;
664 next_pts[ptidx][1] -= delta[1] * 0.5f;
672 if (level == 0 && !status[ptidx]) {
673 Eigen::Array2f next_point = next_pts[ptidx] - half_win;
674 Eigen::Array2i inext_point;
676 inext_point[0] = std::floor(next_point[0]);
677 inext_point[1] = std::floor(next_point[1]);
680 (std::uint32_t)inext_point[0] >= next.
width ||
682 (std::uint32_t)inext_point[1] >= next.
height) {
688 n.
u = next_pts[ptidx][0];
689 n.
v = next_pts[ptidx][1];
692 inext_point[0] = std::floor(next_pts[ptidx][0]);
693 inext_point[1] = std::floor(next_pts[ptidx][1]);
694 iprev_point[0] = std::floor((*prev_keypoints)[ptidx].u);
695 iprev_point[1] = std::floor((*prev_keypoints)[ptidx].v);
696 const PointInT& prev_pt =
697 (*prev_input)[iprev_point[1] * prev_input->width + iprev_point[0]];
698 const PointInT& next_pt =
699 (*input)[inext_point[1] * input->width + inext_point[0]];
700 transformation_computer.
add(
701 prev_pt.getVector3fMap(), next_pt.getVector3fMap(), 1.0);
virtual void spatialGradient(const FloatImage &img, const FloatImage &grad_x, const FloatImage &grad_y, const Eigen::Array2i &location, const Eigen::Array4f &weights, Eigen::ArrayXXf &win, Eigen::ArrayXXf &grad_x_win, Eigen::ArrayXXf &grad_y_win, Eigen::Array3f &covariance) const
extract the patch from the previous image, previous image gradients surrounding pixel alocation while...