Point Cloud Library (PCL)  1.7.2
pyramidal_klt.hpp
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2014-, Open Perception, Inc.
6  *
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * * Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * * Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  * * Neither the name of Willow Garage, Inc. nor the names of its
20  * contributors may be used to endorse or promote products derived
21  * from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  *
36  */
37 
38 #ifndef PCL_TRACKING_IMPL_PYRAMIDAL_KLT_HPP
39 #define PCL_TRACKING_IMPL_PYRAMIDAL_KLT_HPP
40 
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>
46 
47 ///////////////////////////////////////////////////////////////////////////////////////////////
48 template <typename PointInT, typename IntensityT> inline void
50 {
51  track_width_ = width;
52  track_height_ = height;
53 }
54 
55 ///////////////////////////////////////////////////////////////////////////////////////////////
56 template <typename PointInT, typename IntensityT> inline void
58 {
59  if (keypoints->size () <= keypoints_nbr_)
60  keypoints_ = keypoints;
61  else
62  {
64  p->reserve (keypoints_nbr_);
65  for (std::size_t i = 0; i < keypoints_nbr_; ++i)
66  p->push_back (keypoints->points[i]);
67  keypoints_ = p;
68  }
69 
70  keypoints_status_.reset (new pcl::PointIndices);
71  keypoints_status_->indices.resize (keypoints_->size (), 0);
72 }
73 
74 ///////////////////////////////////////////////////////////////////////////////////////////////
75 template <typename PointInT, typename IntensityT> inline void
77 {
78  assert ((input_ || ref_) && "[pcl::tracking::PyramidalKLTTracker] CALL setInputCloud FIRST!");
79 
81  keypoints->reserve (keypoints_nbr_);
82  for (std::size_t i = 0; i < keypoints_nbr_; ++i)
83  {
84  pcl::PointUV uv;
85  uv.u = points->indices[i] % input_->width;
86  uv.v = points->indices[i] / input_->width;
87  keypoints->push_back (uv);
88  }
89  setPointsToTrack (keypoints);
90 }
91 
92 ///////////////////////////////////////////////////////////////////////////////////////////////
93 template <typename PointInT, typename IntensityT> bool
95 {
96  // std::cout << ">>> [PyramidalKLTTracker::initCompute]" << std::endl;
98  {
99  PCL_ERROR ("[pcl::tracking::%s::initCompute] PCLBase::Init failed.\n",
100  tracker_name_.c_str ());
101  return (false);
102  }
103 
104  if (!input_->isOrganized ())
105  {
106  PCL_ERROR ("[pcl::tracking::%s::initCompute] Need an organized point cloud to proceed!",
107  tracker_name_.c_str ());
108  return (false);
109  }
110 
111  if (!keypoints_ || keypoints_->empty ())
112  {
113  PCL_ERROR ("[pcl::tracking::%s::initCompute] No keypoints aborting!",
114  tracker_name_.c_str ());
115  return (false);
116  }
117 
118  // This is the first call
119  if (!ref_)
120  {
121  ref_ = input_;
122  // std::cout << "First run!!!" << std::endl;
123 
124  if ((track_height_ * track_width_)%2 == 0)
125  {
126  PCL_ERROR ("[pcl::tracking::%s::initCompute] Tracking window (%dx%d) must be odd!\n",
127  tracker_name_.c_str (), track_width_, track_height_);
128  return (false);
129  }
130 
131  if (track_height_ < 3 || track_width_ < 3)
132  {
133  PCL_ERROR ("[pcl::tracking::%s::initCompute] Tracking window (%dx%d) must be >= 3x3!\n",
134  tracker_name_.c_str (), track_width_, track_height_);
135  return (false);
136  }
137 
138  track_width_2_ = track_width_ / 2;
139  track_height_2_ = track_height_ / 2;
140 
141  if (nb_levels_ < 2)
142  {
143  PCL_ERROR ("[pcl::tracking::%s::initCompute] Number of pyramid levels should be at least 2!",
144  tracker_name_.c_str ());
145  return (false);
146  }
147 
148  if (nb_levels_ > 5)
149  {
150  PCL_ERROR ("[pcl::tracking::%s::initCompute] Number of pyramid levels should not exceed 5!",
151  tracker_name_.c_str ());
152  return (false);
153  }
154 
155  computePyramids (ref_, ref_pyramid_, pcl::BORDER_REFLECT_101);
156  return (true);
157  }
158  else
159  initialized_ = true;
160 
161  return (true);
162 }
163 
164 ///////////////////////////////////////////////////////////////////////////////////////////////
165 template <typename PointInT, typename IntensityT> void
167 {
168  // std::cout << ">>> derivatives" << std::endl;
169  ////////////////////////////////////////////////////////
170  // Use Shcarr operator to compute derivatives. //
171  // Vertical kernel +3 +10 +3 = [1 0 -1]T * [3 10 3] //
172  // 0 0 0 //
173  // -3 -10 -3 //
174  // Horizontal kernel +3 0 -3 = [3 10 3]T * [1 0 -1] //
175  // +10 0 -10 //
176  // +3 0 -3 //
177  ////////////////////////////////////////////////////////
178  if (grad_x.size () != src.size () || grad_x.width != src.width || grad_x.height != src.height)
179  grad_x = FloatImage (src.width, src.height);
180  if (grad_y.size () != src.size () || grad_y.width != src.width || grad_y.height != src.height)
181  grad_y = FloatImage (src.width, src.height);
182 
183  int height = src.height, width = src.width;
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]);
189 
190  for (int y = 0; y < height; y++)
191  {
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]);
197 
198  // do vertical convolution
199  for (int x = 0; x < width; x++)
200  {
201  trow0[x] = (srow0[x] + srow2[x])*3 + srow1[x]*10;
202  trow1[x] = srow2[x] - srow0[x];
203  }
204 
205  // make border
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];
209 
210  // do horizontal convolution and store results
211  for (int x = 0; x < width; x++)
212  {
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;
215  }
216  }
217 }
218 
219 ///////////////////////////////////////////////////////////////////////////////////////////////
220 template <typename PointInT, typename IntensityT> void
222  FloatImageConstPtr& output) const
223 {
224  FloatImage smoothed (input->width, input->height);
225  convolve (input, smoothed);
226 
227  int width = (smoothed.width +1) / 2;
228  int height = (smoothed.height +1) / 2;
229 
230  int *ii = new int[width];
231  int *ii_ptr = ii;
232  for (int i = 0; i < width; ++i)
233  *ii_ptr++ = 2*i;
234 
235  FloatImagePtr down (new FloatImage (width, height));
236 #ifdef _OPENMP
237 #pragma omp parallel for shared (output) private (ii) num_threads (threads_)
238 #endif
239  for (int j = 0; j < height; ++j)
240  {
241  int jj = 2*j;
242  for (int i = 0; i < width; ++i)
243  (*down) (i,j) = smoothed (ii[i],jj);
244  }
245 
246  output = down;
247 }
248 
249 ///////////////////////////////////////////////////////////////////////////////////////////////
250 template <typename PointInT, typename IntensityT> void
252  FloatImageConstPtr& output,
253  FloatImageConstPtr& output_grad_x,
254  FloatImageConstPtr& output_grad_y) const
255 {
256  downsample (input, output);
257  FloatImagePtr grad_x (new FloatImage (input->width, input->height));
258  FloatImagePtr grad_y (new FloatImage (input->width, input->height));
259  derivatives (*output, *grad_x, *grad_y);
260  output_grad_x = grad_x;
261  output_grad_y = grad_y;
262 }
263 
264 ///////////////////////////////////////////////////////////////////////////////////////////////
265 template <typename PointInT, typename IntensityT> void
267 {
268  FloatImagePtr tmp (new FloatImage (input->width, input->height));
269  convolveRows (input, *tmp);
270  convolveCols (tmp, output);
271 }
272 
273 ///////////////////////////////////////////////////////////////////////////////////////////////
274 template <typename PointInT, typename IntensityT> void
276 {
277  int width = input->width;
278  int height = input->height;
279  int last = input->width - kernel_size_2_;
280  int w = last - 1;
281 
282 #ifdef _OPENMP
283 #pragma omp parallel for shared (output) num_threads (threads_)
284 #endif
285  for (int j = 0; j < height; ++j)
286  {
287  for (int i = kernel_size_2_; i < last; ++i)
288  {
289  double result = 0;
290  for (int k = kernel_last_, l = i - kernel_size_2_; k > -1; --k, ++l)
291  result+= kernel_[k] * (*input) (l,j);
292 
293  output (i,j) = static_cast<float> (result);
294  }
295 
296  for (int i = last; i < width; ++i)
297  output (i,j) = output (w, j);
298 
299  for (int i = 0; i < kernel_size_2_; ++i)
300  output (i,j) = output (kernel_size_2_, j);
301  }
302 }
303 
304 ///////////////////////////////////////////////////////////////////////////////////////////////
305 template <typename PointInT, typename IntensityT> void
307 {
308  output = FloatImage (input->width, input->height);
309 
310  int width = input->width;
311  int height = input->height;
312  int last = input->height - kernel_size_2_;
313  int h = last -1;
314 
315 #ifdef _OPENMP
316 #pragma omp parallel for shared (output) num_threads (threads_)
317 #endif
318  for (int i = 0; i < width; ++i)
319  {
320  for (int j = kernel_size_2_; j < last; ++j)
321  {
322  double result = 0;
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);
326  }
327 
328  for (int j = last; j < height; ++j)
329  output (i,j) = output (i,h);
330 
331  for (int j = 0; j < kernel_size_2_; ++j)
332  output (i,j) = output (i, kernel_size_2_);
333  }
334 }
335 
336 ///////////////////////////////////////////////////////////////////////////////////////////////
337 template <typename PointInT, typename IntensityT> void
339  std::vector<FloatImageConstPtr>& pyramid,
340  pcl::InterpolationType border_type) const
341 {
342  int step = 3;
343  pyramid.resize (step * nb_levels_);
344 
345  FloatImageConstPtr previous;
346  FloatImagePtr tmp (new FloatImage (input->width, input->height));
347 #ifdef _OPENMP
348 #pragma omp parallel for num_threads (threads_)
349 #endif
350  for (int i = 0; i < static_cast<int> (input->size ()); ++i)
351  tmp->points[i] = intensity_ (input->points[i]);
352  previous = tmp;
353 
354  FloatImagePtr img (new FloatImage (previous->width + 2*track_width_,
355  previous->height + 2*track_height_));
356 
357  pcl::copyPointCloud (*tmp, *img, track_height_, track_height_, track_width_, track_width_,
358  border_type, 0.f);
359  pyramid[0] = img;
360 
361  // compute first level gradients
362  FloatImagePtr g_x (new FloatImage (input->width, input->height));
363  FloatImagePtr g_y (new FloatImage (input->width, input->height));
364  derivatives (*img, *g_x, *g_y);
365  // copy to bigger clouds
366  FloatImagePtr grad_x (new FloatImage (previous->width + 2*track_width_,
367  previous->height + 2*track_height_));
368  pcl::copyPointCloud (*g_x, *grad_x, track_height_, track_height_, track_width_, track_width_,
369  pcl::BORDER_CONSTANT, 0.f);
370  pyramid[1] = grad_x;
371 
372  FloatImagePtr grad_y (new FloatImage (previous->width + 2*track_width_,
373  previous->height + 2*track_height_));
374  pcl::copyPointCloud (*g_y, *grad_y, track_height_, track_height_, track_width_, track_width_,
375  pcl::BORDER_CONSTANT, 0.f);
376  pyramid[2] = grad_y;
377 
378  for (int level = 1; level < nb_levels_; ++level)
379  {
380  // compute current level and current level gradients
381  FloatImageConstPtr current;
382  FloatImageConstPtr g_x;
383  FloatImageConstPtr g_y;
384  downsample (previous, current, g_x, g_y);
385  // copy to bigger clouds
386  FloatImagePtr image (new FloatImage (current->width + 2*track_width_,
387  current->height + 2*track_height_));
388  pcl::copyPointCloud (*current, *image, track_height_, track_height_, track_width_, track_width_,
389  border_type, 0.f);
390  pyramid[level*step] = image;
391  FloatImagePtr gradx (new FloatImage (g_x->width + 2*track_width_, g_x->height + 2*track_height_));
392  pcl::copyPointCloud (*g_x, *gradx, track_height_, track_height_, track_width_, track_width_,
393  pcl::BORDER_CONSTANT, 0.f);
394  pyramid[level*step + 1] = gradx;
395  FloatImagePtr grady (new FloatImage (g_y->width + 2*track_width_, g_y->height + 2*track_height_));
396  pcl::copyPointCloud (*g_y, *grady, track_height_, track_height_, track_width_, track_width_,
397  pcl::BORDER_CONSTANT, 0.f);
398  pyramid[level*step + 2] = grady;
399  // set the new level
400  previous = current;
401  }
402 }
403 
404 ///////////////////////////////////////////////////////////////////////////////////////////////
405 template <typename PointInT, typename IntensityT> void
407  const FloatImage& grad_x,
408  const FloatImage& grad_y,
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
415 {
416  const int step = img.width;
417  covariance.setZero ();
418 
419  for (int y = 0; y < track_height_; y++)
420  {
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];
424 
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 ();
428 
429  for (int x =0; x < track_width_; ++x, ++grad_x_ptr, ++grad_y_ptr)
430  {
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];
434  //!!! store those
435  *grad_x_win_ptr++ = ixval;
436  *grad_y_win_ptr++ = iyval;
437  //covariance components
438  covariance[0] += ixval*ixval;
439  covariance[1] += ixval*iyval;
440  covariance[2] += iyval*iyval;
441  }
442  }
443 }
444 
445 ///////////////////////////////////////////////////////////////////////////////////////////////
446 template <typename PointInT, typename IntensityT> void
448  const Eigen::ArrayXXf& prev_grad_x,
449  const Eigen::ArrayXXf& prev_grad_y,
450  const FloatImage& next,
451  const Eigen::Array2i& location,
452  const Eigen::Array4f& weight,
453  Eigen::Array2f &b) const
454 {
455  const int step = next.width;
456  b.setZero ();
457  for (int y = 0; y < track_height_; y++)
458  {
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 ();
463 
464  for (int x = 0; x < track_width_; ++x, ++prev_grad_y_ptr, ++prev_grad_x_ptr)
465  {
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;
470  }
471  }
472 }
473 
474 ///////////////////////////////////////////////////////////////////////////////////////////////
475 template <typename PointInT, typename IntensityT> void
477  const PointCloudInConstPtr& input,
478  const std::vector<FloatImageConstPtr>& prev_pyramid,
479  const std::vector<FloatImageConstPtr>& pyramid,
480  const pcl::PointCloud<pcl::PointUV>::ConstPtr& prev_keypoints,
482  std::vector<int>& status,
483  Eigen::Affine3f& motion) const
484 {
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);
487  pcl::TransformationFromCorrespondences transformation_computer;
488  const int nb_points = prev_keypoints->size ();
489  for (int level = nb_levels_ - 1; level >= 0; --level)
490  {
491  const FloatImage& prev = *(prev_pyramid[level*3]);
492  const FloatImage& next = *(pyramid[level*3]);
493  const FloatImage& grad_x = *(prev_pyramid[level*3+1]);
494  const FloatImage& grad_y = *(prev_pyramid[level*3+2]);
495 
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++)
501  {
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)
506  next_pt = prev_pt;
507  else
508  next_pt = next_pts[ptidx]*2.f;
509 
510  next_pts[ptidx] = next_pt;
511 
512  Eigen::Array2i iprev_point, inext_pt;
513  prev_pt -= half_win;
514  iprev_point[0] = floor (prev_pt[0]);
515  iprev_point[1] = floor (prev_pt[1]);
516 
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)
519  {
520  if (level == 0)
521  status [ptidx] = -1;
522  continue;
523  }
524 
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];
532 
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);
535 
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;
538 
539  if (min_eigenvalue < min_eigenvalue_threshold_ || det < std::numeric_limits<float>::epsilon ())
540  {
541  status[ptidx] = -2;
542  continue;
543  }
544 
545  det = 1.f/det;
546  next_pt -= half_win;
547 
548  Eigen::Array2f prev_delta;
549  for (int j = 0; j < max_iterations_; j++)
550  {
551  inext_pt[0] = floor (next_pt[0]);
552  inext_pt[1] = floor (next_pt[1]);
553 
554  if (inext_pt[0] < -track_width_ || inext_pt[0] >= next.width ||
555  inext_pt[1] < -track_height_ || inext_pt[1] >= next.height)
556  {
557  if (level == 0)
558  status[ptidx] = -1;
559  break;
560  }
561 
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];
568  // compute mismatch vector
569  Eigen::Array2f beta = Eigen::Array2f::Zero ();
570  mismatchVector (prev_win, grad_x_win, grad_y_win, next, inext_pt, weight, beta);
571  // optical flow resolution
572  Eigen::Vector2f delta ((covar[1]*beta[1] - covar[2]*beta[0])*det, (covar[1]*beta[0] - covar[0]*beta[1])*det);
573  // update position
574  next_pt[0] += delta[0]; next_pt[1] += delta[1];
575  next_pts[ptidx] = next_pt + half_win;
576 
577  if (delta.squaredNorm () <= epsilon_)
578  break;
579 
580  if (j > 0 && std::abs (delta[0] + prev_delta[0]) < 0.01 &&
581  std::abs (delta[1] + prev_delta[1]) < 0.01 )
582  {
583  next_pts[ptidx][0] -= delta[0]*0.5f;
584  next_pts[ptidx][1] -= delta[1]*0.5f;
585  break;
586  }
587  // update delta
588  prev_delta = delta;
589  }
590 
591  // update tracked points
592  if (level == 0 && !status[ptidx])
593  {
594  Eigen::Array2f next_point = next_pts[ptidx] - half_win;
595  Eigen::Array2i inext_point;
596 
597  inext_point[0] = floor (next_point[0]);
598  inext_point[1] = floor (next_point[1]);
599 
600  if (inext_point[0] < -track_width_ || inext_point[0] >= next.width ||
601  inext_point[1] < -track_height_ || inext_point[1] >= next.height)
602  {
603  status[ptidx] = -1;
604  continue;
605  }
606  // insert valid keypoint
607  pcl::PointUV n;
608  n.u = next_pts[ptidx][0];
609  n.v = next_pts[ptidx][1];
610  keypoints->push_back (n);
611  // add points pair to compute transformation
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);
619  }
620  }
621  }
622  motion = transformation_computer.getTransformation ();
623 }
624 
625 ///////////////////////////////////////////////////////////////////////////////////////////////
626 template <typename PointInT, typename IntensityT> void
628 {
629  if (!initialized_)
630  return;
631 
632  std::vector<FloatImageConstPtr> pyramid;
633  computePyramids (input_, pyramid, pcl::BORDER_REFLECT_101);
635  keypoints->reserve (keypoints_->size ());
636  std::vector<int> status (keypoints_->size (), 0);
637  track (ref_, input_, ref_pyramid_, pyramid, keypoints_, keypoints, status, motion_);
638  //swap reference and input
639  ref_ = input_;
640  ref_pyramid_ = pyramid;
641  keypoints_ = keypoints;
642  keypoints_status_->indices = status;
643 }
644 
645 #endif
boost::shared_ptr< PointCloud< PointT > > Ptr
Definition: point_cloud.h:428
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.
uint32_t width
The point cloud width (if organized as an image-structure).
Definition: point_cloud.h:413
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
size_t size() const
Definition: point_cloud.h:440
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 convolve(const FloatImageConstPtr &input, FloatImage &output) const
Separately convolve image with decomposable convolution kernel.
std::vector< PointT, Eigen::aligned_allocator< PointT > > points
The point data.
Definition: point_cloud.h:410
boost::shared_ptr< const PointCloud< PointT > > ConstPtr
Definition: point_cloud.h:429
virtual void track(const PointCloudInConstPtr &previous_input, const PointCloudInConstPtr &current_input, const std::vector< FloatImageConstPtr > &previous_pyramid, const std::vector< FloatImageConstPtr > &current_pyramid, const pcl::PointCloud< pcl::PointUV >::ConstPtr &previous_keypoints, pcl::PointCloud< pcl::PointUV >::Ptr &current_keypoints, std::vector< int > &status, Eigen::Affine3f &motion) const
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.
void push_back(const PointT &pt)
Insert a new point in the cloud, at the end of the container.
Definition: point_cloud.h:472
void add(const Eigen::Vector3f &point, const Eigen::Vector3f &corresponding_point, float weight=1.0)
Add a new sample.
PCL base class.
Definition: pcl_base.h:68
virtual void computeTracking()
Abstract tracking method.
Eigen::Affine3f getTransformation()
Calculate the transformation that will best transform the points into their correspondences.
boost::shared_ptr< ::pcl::PointIndices const > PointIndicesConstPtr
Definition: PointIndices.h:27
void reserve(size_t n)
Definition: point_cloud.h:441
uint32_t height
The point cloud height (if organized as an image-structure).
Definition: point_cloud.h:415
FloatImage::ConstPtr FloatImageConstPtr
Definition: pyramidal_klt.h:72
InterpolationType
Definition: io.h:227
PointCloud represents the base class in PCL for storing collections of 3D points. ...
void derivatives(const FloatImage &src, FloatImage &grad_x, FloatImage &grad_y) const
compute Scharr derivatives of a source cloud.
Calculates a transformation based on corresponding 3D points.
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
Definition: pyramidal_klt.h:69
void resize(size_t n)
Resize the cloud.
Definition: point_cloud.h:447