38 #ifndef PCL_TRACKING_IMPL_PYRAMIDAL_KLT_HPP 39 #define PCL_TRACKING_IMPL_PYRAMIDAL_KLT_HPP 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;
229 std::vector<int> ii (width);
230 for (
int i = 0; i < width; ++i)
235 #pragma omp parallel for shared (output) firstprivate (ii) num_threads (threads_) 237 for (
int j = 0; j < height; ++j)
240 for (
int i = 0; i < width; ++i)
241 (*down) (i,j) = smoothed (ii[i],jj);
248 template <
typename Po
intInT,
typename IntensityT>
void 254 downsample (input, output);
257 derivatives (*output, *grad_x, *grad_y);
258 output_grad_x = grad_x;
259 output_grad_y = grad_y;
263 template <
typename Po
intInT,
typename IntensityT>
void 267 convolveRows (input, *tmp);
268 convolveCols (tmp, output);
272 template <
typename Po
intInT,
typename IntensityT>
void 275 int width = input->width;
276 int height = input->height;
277 int last = input->width - kernel_size_2_;
281 #pragma omp parallel for shared (output) num_threads (threads_) 283 for (
int j = 0; j < height; ++j)
285 for (
int i = kernel_size_2_; i < last; ++i)
288 for (
int k = kernel_last_, l = i - kernel_size_2_; k > -1; --k, ++l)
289 result+= kernel_[k] * (*input) (l,j);
291 output (i,j) =
static_cast<float> (result);
294 for (
int i = last; i < width; ++i)
295 output (i,j) = output (w, j);
297 for (
int i = 0; i < kernel_size_2_; ++i)
298 output (i,j) = output (kernel_size_2_, j);
303 template <
typename Po
intInT,
typename IntensityT>
void 306 output =
FloatImage (input->width, input->height);
308 int width = input->width;
309 int height = input->height;
310 int last = input->height - kernel_size_2_;
314 #pragma omp parallel for shared (output) num_threads (threads_) 316 for (
int i = 0; i < width; ++i)
318 for (
int j = kernel_size_2_; j < last; ++j)
321 for (
int k = kernel_last_, l = j - kernel_size_2_; k > -1; --k, ++l)
322 result += kernel_[k] * (*input) (i,l);
323 output (i,j) =
static_cast<float> (result);
326 for (
int j = last; j < height; ++j)
327 output (i,j) = output (i,h);
329 for (
int j = 0; j < kernel_size_2_; ++j)
330 output (i,j) = output (i, kernel_size_2_);
335 template <
typename Po
intInT,
typename IntensityT>
void 337 std::vector<FloatImageConstPtr>& pyramid,
341 pyramid.resize (step * nb_levels_);
346 #pragma omp parallel for num_threads (threads_) 348 for (
int i = 0; i < static_cast<int> (input->size ()); ++i)
349 tmp->points[i] = intensity_ (input->points[i]);
353 previous->height + 2*track_height_));
362 derivatives (*img, *g_x, *g_y);
365 previous->height + 2*track_height_));
366 pcl::copyPointCloud (*g_x, *grad_x, track_height_, track_height_, track_width_, track_width_,
371 previous->height + 2*track_height_));
372 pcl::copyPointCloud (*g_y, *grad_y, track_height_, track_height_, track_width_, track_width_,
376 for (
int level = 1; level < nb_levels_; ++level)
382 downsample (previous, current, g_x, g_y);
385 current->height + 2*track_height_));
386 pcl::copyPointCloud (*current, *image, track_height_, track_height_, track_width_, track_width_,
388 pyramid[level*step] = image;
390 pcl::copyPointCloud (*g_x, *gradx, track_height_, track_height_, track_width_, track_width_,
392 pyramid[level*step + 1] = gradx;
394 pcl::copyPointCloud (*g_y, *grady, track_height_, track_height_, track_width_, track_width_,
396 pyramid[level*step + 2] = grady;
403 template <
typename Po
intInT,
typename IntensityT>
void 407 const Eigen::Array2i& location,
408 const Eigen::Array4f& weight,
409 Eigen::ArrayXXf& win,
410 Eigen::ArrayXXf& grad_x_win,
411 Eigen::ArrayXXf& grad_y_win,
412 Eigen::Array3f &covariance)
const 414 const int step = img.
width;
415 covariance.setZero ();
417 for (
int y = 0; y < track_height_; y++)
419 const float* img_ptr = &(img.
points[0]) + (y + location[1])*step + location[0];
420 const float* grad_x_ptr = &(grad_x.
points[0]) + (y + location[1])*step + location[0];
421 const float* grad_y_ptr = &(grad_y.
points[0]) + (y + location[1])*step + location[0];
423 float* win_ptr = win.data () + y*win.cols ();
424 float* grad_x_win_ptr = grad_x_win.data () + y*grad_x_win.cols ();
425 float* grad_y_win_ptr = grad_y_win.data () + y*grad_y_win.cols ();
427 for (
int x =0; x < track_width_; ++x, ++grad_x_ptr, ++grad_y_ptr)
429 *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];
430 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];
431 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];
433 *grad_x_win_ptr++ = ixval;
434 *grad_y_win_ptr++ = iyval;
436 covariance[0] += ixval*ixval;
437 covariance[1] += ixval*iyval;
438 covariance[2] += iyval*iyval;
444 template <
typename Po
intInT,
typename IntensityT>
void 446 const Eigen::ArrayXXf& prev_grad_x,
447 const Eigen::ArrayXXf& prev_grad_y,
449 const Eigen::Array2i& location,
450 const Eigen::Array4f& weight,
451 Eigen::Array2f &b)
const 453 const int step = next.
width;
455 for (
int y = 0; y < track_height_; y++)
457 const float* next_ptr = &(next.
points[0]) + (y + location[1])*step + location[0];
458 const float* prev_ptr = prev.data () + y*prev.cols ();
459 const float* prev_grad_x_ptr = prev_grad_x.data () + y*prev_grad_x.cols ();
460 const float* prev_grad_y_ptr = prev_grad_y.data () + y*prev_grad_y.cols ();
462 for (
int x = 0; x < track_width_; ++x, ++prev_grad_y_ptr, ++prev_grad_x_ptr)
464 float diff = next_ptr[x]*weight[0] + next_ptr[x+1]*weight[1]
465 + next_ptr[x+step]*weight[2] + next_ptr[x+step+1]*weight[3] - prev_ptr[x];
466 b[0] += *prev_grad_x_ptr * diff;
467 b[1] += *prev_grad_y_ptr * diff;
473 template <
typename Po
intInT,
typename IntensityT>
void 476 const std::vector<FloatImageConstPtr>& prev_pyramid,
477 const std::vector<FloatImageConstPtr>& pyramid,
480 std::vector<int>& status,
481 Eigen::Affine3f& motion)
const 483 std::vector<Eigen::Array2f, Eigen::aligned_allocator<Eigen::Array2f> > next_pts (prev_keypoints->
size ());
484 Eigen::Array2f half_win ((track_width_-1)*0.5f, (track_height_-1)*0.5f);
486 const int nb_points = prev_keypoints->
size ();
487 for (
int level = nb_levels_ - 1; level >= 0; --level)
489 const FloatImage& prev = *(prev_pyramid[level*3]);
491 const FloatImage& grad_x = *(prev_pyramid[level*3+1]);
492 const FloatImage& grad_y = *(prev_pyramid[level*3+2]);
494 Eigen::ArrayXXf prev_win (track_height_, track_width_);
495 Eigen::ArrayXXf grad_x_win (track_height_, track_width_);
496 Eigen::ArrayXXf grad_y_win (track_height_, track_width_);
497 float ratio (1./(1 << level));
498 for (
int ptidx = 0; ptidx < nb_points; ptidx++)
500 Eigen::Array2f prev_pt (prev_keypoints->
points[ptidx].u * ratio,
501 prev_keypoints->
points[ptidx].v * ratio);
502 Eigen::Array2f next_pt;
503 if (level == nb_levels_ -1)
506 next_pt = next_pts[ptidx]*2.f;
508 next_pts[ptidx] = next_pt;
510 Eigen::Array2i iprev_point, inext_pt;
512 iprev_point[0] = floor (prev_pt[0]);
513 iprev_point[1] = floor (prev_pt[1]);
515 if (iprev_point[0] < -track_width_ || (uint32_t) iprev_point[0] >= grad_x.
width ||
516 iprev_point[1] < -track_height_ || (uint32_t) iprev_point[1] >= grad_y.
height)
523 float a = prev_pt[0] - iprev_point[0];
524 float b = prev_pt[1] - iprev_point[1];
525 Eigen::Array4f weight;
526 weight[0] = (1.f - a)*(1.f - b);
527 weight[1] = a*(1.f - b);
528 weight[2] = (1.f - a)*b;
529 weight[3] = 1 - weight[0] - weight[1] - weight[2];
531 Eigen::Array3f covar = Eigen::Array3f::Zero ();
532 spatialGradient (prev, grad_x, grad_y, iprev_point, weight, prev_win, grad_x_win, grad_y_win, covar);
534 float det = covar[0]*covar[2] - covar[1]*covar[1];
535 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;
537 if (min_eigenvalue < min_eigenvalue_threshold_ || det < std::numeric_limits<float>::epsilon ())
546 Eigen::Array2f prev_delta;
547 for (
unsigned int j = 0; j < max_iterations_; j++)
549 inext_pt[0] = floor (next_pt[0]);
550 inext_pt[1] = floor (next_pt[1]);
552 if (inext_pt[0] < -track_width_ || (uint32_t) inext_pt[0] >= next.
width ||
553 inext_pt[1] < -track_height_ || (uint32_t) inext_pt[1] >= next.
height)
560 a = next_pt[0] - inext_pt[0];
561 b = next_pt[1] - inext_pt[1];
562 weight[0] = (1.f - a)*(1.f - b);
563 weight[1] = a*(1.f - b);
564 weight[2] = (1.f - a)*b;
565 weight[3] = 1 - weight[0] - weight[1] - weight[2];
567 Eigen::Array2f beta = Eigen::Array2f::Zero ();
568 mismatchVector (prev_win, grad_x_win, grad_y_win, next, inext_pt, weight, beta);
570 Eigen::Vector2f delta ((covar[1]*beta[1] - covar[2]*beta[0])*det, (covar[1]*beta[0] - covar[0]*beta[1])*det);
572 next_pt[0] += delta[0]; next_pt[1] += delta[1];
573 next_pts[ptidx] = next_pt + half_win;
575 if (delta.squaredNorm () <= epsilon_)
578 if (j > 0 && std::abs (delta[0] + prev_delta[0]) < 0.01 &&
579 std::abs (delta[1] + prev_delta[1]) < 0.01 )
581 next_pts[ptidx][0] -= delta[0]*0.5f;
582 next_pts[ptidx][1] -= delta[1]*0.5f;
590 if (level == 0 && !status[ptidx])
592 Eigen::Array2f next_point = next_pts[ptidx] - half_win;
593 Eigen::Array2i inext_point;
595 inext_point[0] = floor (next_point[0]);
596 inext_point[1] = floor (next_point[1]);
598 if (inext_point[0] < -track_width_ || (uint32_t) inext_point[0] >= next.
width ||
599 inext_point[1] < -track_height_ || (uint32_t) inext_point[1] >= next.
height)
606 n.
u = next_pts[ptidx][0];
607 n.
v = next_pts[ptidx][1];
610 inext_point[0] = floor (next_pts[ptidx][0]);
611 inext_point[1] = floor (next_pts[ptidx][1]);
612 iprev_point[0] = floor (prev_keypoints->
points[ptidx].u);
613 iprev_point[1] = floor (prev_keypoints->
points[ptidx].v);
614 const PointInT& prev_pt = prev_input->points[iprev_point[1]*prev_input->width + iprev_point[0]];
615 const PointInT& next_pt = input->points[inext_pt[1]*input->width + inext_pt[0]];
616 transformation_computer.
add (prev_pt.getVector3fMap (), next_pt.getVector3fMap (), 1.0);
624 template <
typename Po
intInT,
typename IntensityT>
void 630 std::vector<FloatImageConstPtr> pyramid;
633 keypoints->
reserve (keypoints_->size ());
634 std::vector<int> status (keypoints_->size (), 0);
635 track (ref_, input_, ref_pyramid_, pyramid, keypoints_, keypoints, status, motion_);
638 ref_pyramid_ = pyramid;
639 keypoints_ = keypoints;
640 keypoints_status_->indices = status;
void derivatives(const FloatImage &src, FloatImage &grad_x, FloatImage &grad_y) const
compute Scharr derivatives of a source cloud.
std::vector< PointT, Eigen::aligned_allocator< PointT > > points
The point data.
void convolve(const FloatImageConstPtr &input, FloatImage &output) const
Separately convolve image with decomposable convolution kernel.
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 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
virtual void computePyramids(const PointCloudInConstPtr &input, std::vector< FloatImageConstPtr > &pyramid, pcl::InterpolationType border_type) const
Compute the pyramidal representation of an image.
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.
void convolveCols(const FloatImageConstPtr &input, FloatImage &output) const
Convolve image columns.
uint32_t height
The point cloud height (if organized as an image-structure).
boost::shared_ptr< PointCloud< PointT > > Ptr
uint32_t width
The point cloud width (if organized as an image-structure).
void convolveRows(const FloatImageConstPtr &input, FloatImage &output) const
Convolve image rows.
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.
void downsample(const FloatImageConstPtr &input, FloatImageConstPtr &output) const
downsample input
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
FloatImage::ConstPtr FloatImageConstPtr
FloatImage::Ptr FloatImagePtr
PointCloudIn::ConstPtr PointCloudInConstPtr
void resize(size_t n)
Resize the cloud.
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
Define methods for measuring time spent in code blocks.