38 #ifndef PCL_TRACKING_IMPL_PYRAMIDAL_KLT_HPP
39 #define PCL_TRACKING_IMPL_PYRAMIDAL_KLT_HPP
41 #include <pcl/common/time.h>
42 #include <pcl/common/utils.h>
43 #include <pcl/tracking/boost.h>
44 #include <pcl/common/io.h>
45 #include <pcl/common/utils.h>
48 template <
typename Po
intInT,
typename IntensityT>
inline void
52 track_height_ = height;
56 template <
typename Po
intInT,
typename IntensityT>
inline void
59 if (keypoints->
size () <= keypoints_nbr_)
60 keypoints_ = keypoints;
65 for (std::size_t i = 0; i < keypoints_nbr_; ++i)
71 keypoints_status_->indices.
resize (keypoints_->size (), 0);
75 template <
typename Po
intInT,
typename IntensityT>
inline void
78 assert ((input_ || ref_) &&
"[pcl::tracking::PyramidalKLTTracker] CALL setInputCloud FIRST!");
81 keypoints->
reserve (keypoints_nbr_);
82 for (std::size_t i = 0; i < keypoints_nbr_; ++i)
85 uv.
u = points->indices[i] % input_->width;
86 uv.
v = points->indices[i] / input_->width;
89 setPointsToTrack (keypoints);
93 template <
typename Po
intInT,
typename IntensityT>
bool
99 PCL_ERROR (
"[pcl::tracking::%s::initCompute] PCLBase::Init failed.\n",
100 tracker_name_.c_str ());
104 if (!input_->isOrganized ())
106 PCL_ERROR (
"[pcl::tracking::%s::initCompute] Need an organized point cloud to proceed!",
107 tracker_name_.c_str ());
111 if (!keypoints_ || keypoints_->empty ())
113 PCL_ERROR (
"[pcl::tracking::%s::initCompute] No keypoints aborting!",
114 tracker_name_.c_str ());
124 if ((track_height_ * track_width_)%2 == 0)
126 PCL_ERROR (
"[pcl::tracking::%s::initCompute] Tracking window (%dx%d) must be odd!\n",
127 tracker_name_.c_str (), track_width_, track_height_);
131 if (track_height_ < 3 || track_width_ < 3)
133 PCL_ERROR (
"[pcl::tracking::%s::initCompute] Tracking window (%dx%d) must be >= 3x3!\n",
134 tracker_name_.c_str (), track_width_, track_height_);
138 track_width_2_ = track_width_ / 2;
139 track_height_2_ = track_height_ / 2;
143 PCL_ERROR (
"[pcl::tracking::%s::initCompute] Number of pyramid levels should be at least 2!",
144 tracker_name_.c_str ());
150 PCL_ERROR (
"[pcl::tracking::%s::initCompute] Number of pyramid levels should not exceed 5!",
151 tracker_name_.c_str ());
165 template <
typename Po
intInT,
typename IntensityT>
void
184 float *row0 =
new float [src.
width + 2];
185 float *row1 =
new float [src.
width + 2];
186 float *trow0 = row0; ++trow0;
187 float *trow1 = row1; ++trow1;
188 const float* src_ptr = &(src.
points[0]);
190 for (
int y = 0; y < height; y++)
192 const float* srow0 = src_ptr + (y > 0 ? y-1 : height > 1 ? 1 : 0) * width;
193 const float* srow1 = src_ptr + y * width;
194 const float* srow2 = src_ptr + (y < height-1 ? y+1 : height > 1 ? height-2 : 0) * width;
195 float* grad_x_row = &(grad_x.
points[y * width]);
196 float* grad_y_row = &(grad_y.
points[y * width]);
199 for (
int x = 0; x < width; x++)
201 trow0[x] = (srow0[x] + srow2[x])*3 + srow1[x]*10;
202 trow1[x] = srow2[x] - srow0[x];
206 int x0 = width > 1 ? 1 : 0, x1 = width > 1 ? width-2 : 0;
207 trow0[-1] = trow0[x0]; trow0[width] = trow0[x1];
208 trow1[-1] = trow1[x0]; trow1[width] = trow1[x1];
211 for (
int x = 0; x < width; x++)
213 grad_x_row[x] = trow0[x+1] - trow0[x-1];
214 grad_y_row[x] = (trow1[x+1] + trow1[x-1])*3 + trow1[x]*10;
220 template <
typename Po
intInT,
typename IntensityT>
void
224 FloatImage smoothed (input->width, input->height);
225 convolve (input, smoothed);
227 int width = (smoothed.
width +1) / 2;
228 int height = (smoothed.
height +1) / 2;
230 int *ii =
new int[width];
232 for (
int i = 0; i < width; ++i)
237 #pragma omp parallel for shared (output) private (ii) num_threads (threads_)
239 for (
int j = 0; j < height; ++j)
242 for (
int i = 0; i < width; ++i)
243 (*down) (i,j) = smoothed (ii[i],jj);
250 template <
typename Po
intInT,
typename IntensityT>
void
256 downsample (input, output);
259 derivatives (*output, *grad_x, *grad_y);
260 output_grad_x = grad_x;
261 output_grad_y = grad_y;
265 template <
typename Po
intInT,
typename IntensityT>
void
269 convolveRows (input, *tmp);
270 convolveCols (tmp, output);
274 template <
typename Po
intInT,
typename IntensityT>
void
277 int width = input->width;
278 int height = input->height;
279 int last = input->width - kernel_size_2_;
283 #pragma omp parallel for shared (output) num_threads (threads_)
285 for (
int j = 0; j < height; ++j)
287 for (
int i = kernel_size_2_; i < last; ++i)
290 for (
int k = kernel_last_, l = i - kernel_size_2_; k > -1; --k, ++l)
291 result+= kernel_[k] * (*input) (l,j);
293 output (i,j) =
static_cast<float> (result);
296 for (
int i = last; i < width; ++i)
297 output (i,j) = output (w, j);
299 for (
int i = 0; i < kernel_size_2_; ++i)
300 output (i,j) = output (kernel_size_2_, j);
305 template <
typename Po
intInT,
typename IntensityT>
void
308 output =
FloatImage (input->width, input->height);
310 int width = input->width;
311 int height = input->height;
312 int last = input->height - kernel_size_2_;
316 #pragma omp parallel for shared (output) num_threads (threads_)
318 for (
int i = 0; i < width; ++i)
320 for (
int j = kernel_size_2_; j < last; ++j)
323 for (
int k = kernel_last_, l = j - kernel_size_2_; k > -1; --k, ++l)
324 result += kernel_[k] * (*input) (i,l);
325 output (i,j) =
static_cast<float> (result);
328 for (
int j = last; j < height; ++j)
329 output (i,j) = output (i,h);
331 for (
int j = 0; j < kernel_size_2_; ++j)
332 output (i,j) = output (i, kernel_size_2_);
337 template <
typename Po
intInT,
typename IntensityT>
void
339 std::vector<FloatImageConstPtr>& pyramid,
343 pyramid.resize (step * nb_levels_);
348 #pragma omp parallel for num_threads (threads_)
350 for (
int i = 0; i < static_cast<int> (input->size ()); ++i)
351 tmp->points[i] = intensity_ (input->points[i]);
355 previous->height + 2*track_height_));
364 derivatives (*img, *g_x, *g_y);
367 previous->height + 2*track_height_));
368 pcl::copyPointCloud (*g_x, *grad_x, track_height_, track_height_, track_width_, track_width_,
373 previous->height + 2*track_height_));
374 pcl::copyPointCloud (*g_y, *grad_y, track_height_, track_height_, track_width_, track_width_,
378 for (
int level = 1; level < nb_levels_; ++level)
384 downsample (previous, current, g_x, g_y);
387 current->height + 2*track_height_));
388 pcl::copyPointCloud (*current, *image, track_height_, track_height_, track_width_, track_width_,
390 pyramid[level*step] = image;
392 pcl::copyPointCloud (*g_x, *gradx, track_height_, track_height_, track_width_, track_width_,
394 pyramid[level*step + 1] = gradx;
396 pcl::copyPointCloud (*g_y, *grady, track_height_, track_height_, track_width_, track_width_,
398 pyramid[level*step + 2] = grady;
405 template <
typename Po
intInT,
typename IntensityT>
void
409 const Eigen::Array2i& location,
410 const Eigen::Array4f& weight,
411 Eigen::ArrayXXf& win,
412 Eigen::ArrayXXf& grad_x_win,
413 Eigen::ArrayXXf& grad_y_win,
414 Eigen::Array3f &covariance)
const
416 const int step = img.
width;
417 covariance.setZero ();
419 for (
int y = 0; y < track_height_; y++)
421 const float* img_ptr = &(img.
points[0]) + (y + location[1])*step + location[0];
422 const float* grad_x_ptr = &(grad_x.
points[0]) + (y + location[1])*step + location[0];
423 const float* grad_y_ptr = &(grad_y.
points[0]) + (y + location[1])*step + location[0];
425 float* win_ptr = win.data () + y*win.cols ();
426 float* grad_x_win_ptr = grad_x_win.data () + y*grad_x_win.cols ();
427 float* grad_y_win_ptr = grad_y_win.data () + y*grad_y_win.cols ();
429 for (
int x =0; x < track_width_; ++x, ++grad_x_ptr, ++grad_y_ptr)
431 *win_ptr++ = img_ptr[x]*weight[0] + img_ptr[x+1]*weight[1] + img_ptr[x+step]*weight[2] + img_ptr[x+step+1]*weight[3];
432 float ixval = grad_x_ptr[0]*weight[0] + grad_x_ptr[1]*weight[1] + grad_x_ptr[step]*weight[2] + grad_x_ptr[step+1]*weight[3];
433 float iyval = grad_y_ptr[0]*weight[0] + grad_y_ptr[1]*weight[1] + grad_y_ptr[step]*weight[2] + grad_y_ptr[step+1]*weight[3];
435 *grad_x_win_ptr++ = ixval;
436 *grad_y_win_ptr++ = iyval;
438 covariance[0] += ixval*ixval;
439 covariance[1] += ixval*iyval;
440 covariance[2] += iyval*iyval;
446 template <
typename Po
intInT,
typename IntensityT>
void
448 const Eigen::ArrayXXf& prev_grad_x,
449 const Eigen::ArrayXXf& prev_grad_y,
451 const Eigen::Array2i& location,
452 const Eigen::Array4f& weight,
453 Eigen::Array2f &b)
const
455 const int step = next.
width;
457 for (
int y = 0; y < track_height_; y++)
459 const float* next_ptr = &(next.
points[0]) + (y + location[1])*step + location[0];
460 const float* prev_ptr = prev.data () + y*prev.cols ();
461 const float* prev_grad_x_ptr = prev_grad_x.data () + y*prev_grad_x.cols ();
462 const float* prev_grad_y_ptr = prev_grad_y.data () + y*prev_grad_y.cols ();
464 for (
int x = 0; x < track_width_; ++x, ++prev_grad_y_ptr, ++prev_grad_x_ptr)
466 float diff = next_ptr[x]*weight[0] + next_ptr[x+1]*weight[1]
467 + next_ptr[x+step]*weight[2] + next_ptr[x+step+1]*weight[3] - prev_ptr[x];
468 b[0] += *prev_grad_x_ptr * diff;
469 b[1] += *prev_grad_y_ptr * diff;
475 template <
typename Po
intInT,
typename IntensityT>
void
478 const std::vector<FloatImageConstPtr>& prev_pyramid,
479 const std::vector<FloatImageConstPtr>& pyramid,
482 std::vector<int>& status,
483 Eigen::Affine3f& motion)
const
485 std::vector<Eigen::Array2f> next_pts (prev_keypoints->
size ());
486 Eigen::Array2f half_win ((track_width_-1)*0.5f, (track_height_-1)*0.5f);
488 const int nb_points = prev_keypoints->
size ();
489 for (
int level = nb_levels_ - 1; level >= 0; --level)
491 const FloatImage& prev = *(prev_pyramid[level*3]);
493 const FloatImage& grad_x = *(prev_pyramid[level*3+1]);
494 const FloatImage& grad_y = *(prev_pyramid[level*3+2]);
496 Eigen::ArrayXXf prev_win (track_height_, track_width_);
497 Eigen::ArrayXXf grad_x_win (track_height_, track_width_);
498 Eigen::ArrayXXf grad_y_win (track_height_, track_width_);
499 float ratio (1./(1 << level));
500 for (
int ptidx = 0; ptidx < nb_points; ptidx++)
502 Eigen::Array2f prev_pt (prev_keypoints->
points[ptidx].u * ratio,
503 prev_keypoints->
points[ptidx].v * ratio);
504 Eigen::Array2f next_pt;
505 if (level == nb_levels_ -1)
508 next_pt = next_pts[ptidx]*2.f;
510 next_pts[ptidx] = next_pt;
512 Eigen::Array2i iprev_point, inext_pt;
514 iprev_point[0] = floor (prev_pt[0]);
515 iprev_point[1] = floor (prev_pt[1]);
517 if (iprev_point[0] < -track_width_ || iprev_point[0] >= grad_x.
width ||
518 iprev_point[1] < -track_height_ || iprev_point[1] >= grad_y.
height)
525 float a = prev_pt[0] - iprev_point[0];
526 float b = prev_pt[1] - iprev_point[1];
527 Eigen::Array4f weight;
528 weight[0] = (1.f - a)*(1.f - b);
529 weight[1] = a*(1.f - b);
530 weight[2] = (1.f - a)*b;
531 weight[3] = 1 - weight[0] - weight[1] - weight[2];
533 Eigen::Array3f covar = Eigen::Array3f::Zero ();
534 spatialGradient (prev, grad_x, grad_y, iprev_point, weight, prev_win, grad_x_win, grad_y_win, covar);
536 float det = covar[0]*covar[2] - covar[1]*covar[1];
537 float min_eigenvalue = (covar[2] + covar[0] - std::sqrt ((covar[0]-covar[2])*(covar[0]-covar[2]) + 4.f*covar[1]*covar[1]))/2.f;
539 if (min_eigenvalue < min_eigenvalue_threshold_ || det < std::numeric_limits<float>::epsilon ())
548 Eigen::Array2f prev_delta;
549 for (
int j = 0; j < max_iterations_; j++)
551 inext_pt[0] = floor (next_pt[0]);
552 inext_pt[1] = floor (next_pt[1]);
554 if (inext_pt[0] < -track_width_ || inext_pt[0] >= next.
width ||
555 inext_pt[1] < -track_height_ || inext_pt[1] >= next.
height)
562 a = next_pt[0] - inext_pt[0];
563 b = next_pt[1] - inext_pt[1];
564 weight[0] = (1.f - a)*(1.f - b);
565 weight[1] = a*(1.f - b);
566 weight[2] = (1.f - a)*b;
567 weight[3] = 1 - weight[0] - weight[1] - weight[2];
569 Eigen::Array2f beta = Eigen::Array2f::Zero ();
570 mismatchVector (prev_win, grad_x_win, grad_y_win, next, inext_pt, weight, beta);
572 Eigen::Vector2f delta ((covar[1]*beta[1] - covar[2]*beta[0])*det, (covar[1]*beta[0] - covar[0]*beta[1])*det);
574 next_pt[0] += delta[0]; next_pt[1] += delta[1];
575 next_pts[ptidx] = next_pt + half_win;
577 if (delta.squaredNorm () <= epsilon_)
580 if (j > 0 && std::abs (delta[0] + prev_delta[0]) < 0.01 &&
581 std::abs (delta[1] + prev_delta[1]) < 0.01 )
583 next_pts[ptidx][0] -= delta[0]*0.5f;
584 next_pts[ptidx][1] -= delta[1]*0.5f;
592 if (level == 0 && !status[ptidx])
594 Eigen::Array2f next_point = next_pts[ptidx] - half_win;
595 Eigen::Array2i inext_point;
597 inext_point[0] = floor (next_point[0]);
598 inext_point[1] = floor (next_point[1]);
600 if (inext_point[0] < -track_width_ || inext_point[0] >= next.
width ||
601 inext_point[1] < -track_height_ || inext_point[1] >= next.
height)
608 n.
u = next_pts[ptidx][0];
609 n.
v = next_pts[ptidx][1];
612 inext_point[0] = floor (next_pts[ptidx][0]);
613 inext_point[1] = floor (next_pts[ptidx][1]);
614 iprev_point[0] = floor (prev_keypoints->
points[ptidx].u);
615 iprev_point[1] = floor (prev_keypoints->
points[ptidx].v);
616 const PointInT& prev_pt = prev_input->points[iprev_point[1]*prev_input->width + iprev_point[0]];
617 const PointInT& next_pt = input->points[inext_pt[1]*input->width + inext_pt[0]];
618 transformation_computer.
add (prev_pt.getVector3fMap (), next_pt.getVector3fMap (), 1.0);
626 template <
typename Po
intInT,
typename IntensityT>
void
632 std::vector<FloatImageConstPtr> pyramid;
635 keypoints->
reserve (keypoints_->size ());
636 std::vector<int> status (keypoints_->size (), 0);
637 track (ref_, input_, ref_pyramid_, pyramid, keypoints_, keypoints, status, motion_);
640 ref_pyramid_ = pyramid;
641 keypoints_ = keypoints;
642 keypoints_status_->indices = status;
void mismatchVector(const Eigen::ArrayXXf &prev, const Eigen::ArrayXXf &prev_grad_x, const Eigen::ArrayXXf &prev_grad_y, const FloatImage &next, const Eigen::Array2i &location, const Eigen::Array4f &weights, Eigen::Array2f &b) const
void convolveRows(const FloatImageConstPtr &input, FloatImage &output) const
Convolve image rows.
std::vector< PointT, Eigen::aligned_allocator< PointT > > points
The point data.
void convolveCols(const FloatImageConstPtr &input, FloatImage &output) const
Convolve image columns.
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...
void downsample(const FloatImageConstPtr &input, FloatImageConstPtr &output) const
downsample input
void push_back(const PointT &pt)
Insert a new point in the cloud, at the end of the container.
PCL_EXPORTS void copyPointCloud(const pcl::PCLPointCloud2 &cloud_in, const std::vector< int > &indices, pcl::PCLPointCloud2 &cloud_out)
Extract the indices of a given point cloud as a new point cloud.
uint32_t height
The point cloud height (if organized as an image-structure).
void convolve(const FloatImageConstPtr &input, FloatImage &output) const
Separately convolve image with decomposable convolution kernel.
boost::shared_ptr< PointCloud< PointT > > Ptr
virtual void track(const PointCloudInConstPtr &previous_input, const PointCloudInConstPtr ¤t_input, const std::vector< FloatImageConstPtr > &previous_pyramid, const std::vector< FloatImageConstPtr > ¤t_pyramid, const pcl::PointCloud< pcl::PointUV >::ConstPtr &previous_keypoints, pcl::PointCloud< pcl::PointUV >::Ptr ¤t_keypoints, std::vector< int > &status, Eigen::Affine3f &motion) const
uint32_t width
The point cloud width (if organized as an image-structure).
A 2D point structure representing pixel image coordinates.
void setTrackingWindowSize(int width, int height)
set the tracking window size
void setPointsToTrack(const pcl::PointIndicesConstPtr &points)
Provide a pointer to points to track.
virtual bool initCompute()
This method should get called before starting the actual computation.
boost::shared_ptr< const PointCloud< PointT > > ConstPtr
virtual void computeTracking()
Abstract tracking method.
boost::shared_ptr< ::pcl::PointIndices const > PointIndicesConstPtr
PointCloud represents the base class in PCL for storing collections of 3D points. ...
FloatImage::ConstPtr FloatImageConstPtr
void derivatives(const FloatImage &src, FloatImage &grad_x, FloatImage &grad_y) const
compute Scharr derivatives of a source cloud.
FloatImage::Ptr FloatImagePtr
virtual void computePyramids(const PointCloudInConstPtr &input, std::vector< FloatImageConstPtr > &pyramid, pcl::InterpolationType border_type) const
Compute the pyramidal representation of an image.
PointCloudIn::ConstPtr PointCloudInConstPtr
void resize(size_t n)
Resize the cloud.