Point Cloud Library (PCL)  1.9.1-dev
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/common/io.h>
44 #include <pcl/common/utils.h>
45 
46 ///////////////////////////////////////////////////////////////////////////////////////////////
47 template <typename PointInT, typename IntensityT> inline void
49 {
50  track_width_ = width;
51  track_height_ = height;
52 }
53 
54 ///////////////////////////////////////////////////////////////////////////////////////////////
55 template <typename PointInT, typename IntensityT> inline void
57 {
58  if (keypoints->size () <= keypoints_nbr_)
59  keypoints_ = keypoints;
60  else
61  {
63  p->reserve (keypoints_nbr_);
64  for (std::size_t i = 0; i < keypoints_nbr_; ++i)
65  p->push_back (keypoints->points[i]);
66  keypoints_ = p;
67  }
68 
69  keypoints_status_.reset (new pcl::PointIndices);
70  keypoints_status_->indices.resize (keypoints_->size (), 0);
71 }
72 
73 ///////////////////////////////////////////////////////////////////////////////////////////////
74 template <typename PointInT, typename IntensityT> inline void
76 {
77  assert ((input_ || ref_) && "[pcl::tracking::PyramidalKLTTracker] CALL setInputCloud FIRST!");
78 
80  keypoints->reserve (keypoints_nbr_);
81  for (std::size_t i = 0; i < keypoints_nbr_; ++i)
82  {
83  pcl::PointUV uv;
84  uv.u = points->indices[i] % input_->width;
85  uv.v = points->indices[i] / input_->width;
86  keypoints->push_back (uv);
87  }
88  setPointsToTrack (keypoints);
89 }
90 
91 ///////////////////////////////////////////////////////////////////////////////////////////////
92 template <typename PointInT, typename IntensityT> bool
94 {
95  // std::cout << ">>> [PyramidalKLTTracker::initCompute]" << std::endl;
97  {
98  PCL_ERROR ("[pcl::tracking::%s::initCompute] PCLBase::Init failed.\n",
99  tracker_name_.c_str ());
100  return (false);
101  }
102 
103  if (!input_->isOrganized ())
104  {
105  PCL_ERROR ("[pcl::tracking::%s::initCompute] Need an organized point cloud to proceed!",
106  tracker_name_.c_str ());
107  return (false);
108  }
109 
110  if (!keypoints_ || keypoints_->empty ())
111  {
112  PCL_ERROR ("[pcl::tracking::%s::initCompute] No keypoints aborting!",
113  tracker_name_.c_str ());
114  return (false);
115  }
116 
117  // This is the first call
118  if (!ref_)
119  {
120  ref_ = input_;
121  // std::cout << "First run!!!" << std::endl;
122 
123  if ((track_height_ * track_width_)%2 == 0)
124  {
125  PCL_ERROR ("[pcl::tracking::%s::initCompute] Tracking window (%dx%d) must be odd!\n",
126  tracker_name_.c_str (), track_width_, track_height_);
127  return (false);
128  }
129 
130  if (track_height_ < 3 || track_width_ < 3)
131  {
132  PCL_ERROR ("[pcl::tracking::%s::initCompute] Tracking window (%dx%d) must be >= 3x3!\n",
133  tracker_name_.c_str (), track_width_, track_height_);
134  return (false);
135  }
136 
137  track_width_2_ = track_width_ / 2;
138  track_height_2_ = track_height_ / 2;
139 
140  if (nb_levels_ < 2)
141  {
142  PCL_ERROR ("[pcl::tracking::%s::initCompute] Number of pyramid levels should be at least 2!",
143  tracker_name_.c_str ());
144  return (false);
145  }
146 
147  if (nb_levels_ > 5)
148  {
149  PCL_ERROR ("[pcl::tracking::%s::initCompute] Number of pyramid levels should not exceed 5!",
150  tracker_name_.c_str ());
151  return (false);
152  }
153 
154  computePyramids (ref_, ref_pyramid_, pcl::BORDER_REFLECT_101);
155  return (true);
156  }
157  initialized_ = true;
158 
159  return (true);
160 }
161 
162 ///////////////////////////////////////////////////////////////////////////////////////////////
163 template <typename PointInT, typename IntensityT> void
165 {
166  // std::cout << ">>> derivatives" << std::endl;
167  ////////////////////////////////////////////////////////
168  // Use Shcarr operator to compute derivatives. //
169  // Vertical kernel +3 +10 +3 = [1 0 -1]T * [3 10 3] //
170  // 0 0 0 //
171  // -3 -10 -3 //
172  // Horizontal kernel +3 0 -3 = [3 10 3]T * [1 0 -1] //
173  // +10 0 -10 //
174  // +3 0 -3 //
175  ////////////////////////////////////////////////////////
176  if (grad_x.size () != src.size () || grad_x.width != src.width || grad_x.height != src.height)
177  grad_x = FloatImage (src.width, src.height);
178  if (grad_y.size () != src.size () || grad_y.width != src.width || grad_y.height != src.height)
179  grad_y = FloatImage (src.width, src.height);
180 
181  int height = src.height, width = src.width;
182  float *row0 = new float [src.width + 2];
183  float *row1 = new float [src.width + 2];
184  float *trow0 = row0; ++trow0;
185  float *trow1 = row1; ++trow1;
186  const float* src_ptr = &(src.points[0]);
187 
188  for (int y = 0; y < height; y++)
189  {
190  const float* srow0 = src_ptr + (y > 0 ? y-1 : height > 1 ? 1 : 0) * width;
191  const float* srow1 = src_ptr + y * width;
192  const float* srow2 = src_ptr + (y < height-1 ? y+1 : height > 1 ? height-2 : 0) * width;
193  float* grad_x_row = &(grad_x.points[y * width]);
194  float* grad_y_row = &(grad_y.points[y * width]);
195 
196  // do vertical convolution
197  for (int x = 0; x < width; x++)
198  {
199  trow0[x] = (srow0[x] + srow2[x])*3 + srow1[x]*10;
200  trow1[x] = srow2[x] - srow0[x];
201  }
202 
203  // make border
204  int x0 = width > 1 ? 1 : 0, x1 = width > 1 ? width-2 : 0;
205  trow0[-1] = trow0[x0]; trow0[width] = trow0[x1];
206  trow1[-1] = trow1[x0]; trow1[width] = trow1[x1];
207 
208  // do horizontal convolution and store results
209  for (int x = 0; x < width; x++)
210  {
211  grad_x_row[x] = trow0[x+1] - trow0[x-1];
212  grad_y_row[x] = (trow1[x+1] + trow1[x-1])*3 + trow1[x]*10;
213  }
214  }
215 }
216 
217 ///////////////////////////////////////////////////////////////////////////////////////////////
218 template <typename PointInT, typename IntensityT> void
220  FloatImageConstPtr& output) const
221 {
222  FloatImage smoothed (input->width, input->height);
223  convolve (input, smoothed);
224 
225  int width = (smoothed.width +1) / 2;
226  int height = (smoothed.height +1) / 2;
227  std::vector<int> ii (width);
228  for (int i = 0; i < width; ++i)
229  ii[i] = 2 * i;
230 
231  FloatImagePtr down (new FloatImage (width, height));
232 #ifdef _OPENMP
233 #pragma omp parallel for shared (output) firstprivate (ii) num_threads (threads_)
234 #endif
235  for (int j = 0; j < height; ++j)
236  {
237  int jj = 2*j;
238  for (int i = 0; i < width; ++i)
239  (*down) (i,j) = smoothed (ii[i],jj);
240  }
241 
242  output = down;
243 }
244 
245 ///////////////////////////////////////////////////////////////////////////////////////////////
246 template <typename PointInT, typename IntensityT> void
248  FloatImageConstPtr& output,
249  FloatImageConstPtr& output_grad_x,
250  FloatImageConstPtr& output_grad_y) const
251 {
252  downsample (input, output);
253  FloatImagePtr grad_x (new FloatImage (input->width, input->height));
254  FloatImagePtr grad_y (new FloatImage (input->width, input->height));
255  derivatives (*output, *grad_x, *grad_y);
256  output_grad_x = grad_x;
257  output_grad_y = grad_y;
258 }
259 
260 ///////////////////////////////////////////////////////////////////////////////////////////////
261 template <typename PointInT, typename IntensityT> void
263 {
264  FloatImagePtr tmp (new FloatImage (input->width, input->height));
265  convolveRows (input, *tmp);
266  convolveCols (tmp, output);
267 }
268 
269 ///////////////////////////////////////////////////////////////////////////////////////////////
270 template <typename PointInT, typename IntensityT> void
272 {
273  int width = input->width;
274  int height = input->height;
275  int last = input->width - kernel_size_2_;
276  int w = last - 1;
277 
278 #ifdef _OPENMP
279 #pragma omp parallel for shared (output) num_threads (threads_)
280 #endif
281  for (int j = 0; j < height; ++j)
282  {
283  for (int i = kernel_size_2_; i < last; ++i)
284  {
285  double result = 0;
286  for (int k = kernel_last_, l = i - kernel_size_2_; k > -1; --k, ++l)
287  result+= kernel_[k] * (*input) (l,j);
288 
289  output (i,j) = static_cast<float> (result);
290  }
291 
292  for (int i = last; i < width; ++i)
293  output (i,j) = output (w, j);
294 
295  for (int i = 0; i < kernel_size_2_; ++i)
296  output (i,j) = output (kernel_size_2_, j);
297  }
298 }
299 
300 ///////////////////////////////////////////////////////////////////////////////////////////////
301 template <typename PointInT, typename IntensityT> void
303 {
304  output = FloatImage (input->width, input->height);
305 
306  int width = input->width;
307  int height = input->height;
308  int last = input->height - kernel_size_2_;
309  int h = last -1;
310 
311 #ifdef _OPENMP
312 #pragma omp parallel for shared (output) num_threads (threads_)
313 #endif
314  for (int i = 0; i < width; ++i)
315  {
316  for (int j = kernel_size_2_; j < last; ++j)
317  {
318  double result = 0;
319  for (int k = kernel_last_, l = j - kernel_size_2_; k > -1; --k, ++l)
320  result += kernel_[k] * (*input) (i,l);
321  output (i,j) = static_cast<float> (result);
322  }
323 
324  for (int j = last; j < height; ++j)
325  output (i,j) = output (i,h);
326 
327  for (int j = 0; j < kernel_size_2_; ++j)
328  output (i,j) = output (i, kernel_size_2_);
329  }
330 }
331 
332 ///////////////////////////////////////////////////////////////////////////////////////////////
333 template <typename PointInT, typename IntensityT> void
335  std::vector<FloatImageConstPtr>& pyramid,
336  pcl::InterpolationType border_type) const
337 {
338  int step = 3;
339  pyramid.resize (step * nb_levels_);
340 
341  FloatImageConstPtr previous;
342  FloatImagePtr tmp (new FloatImage (input->width, input->height));
343 #ifdef _OPENMP
344 #pragma omp parallel for num_threads (threads_)
345 #endif
346  for (int i = 0; i < static_cast<int> (input->size ()); ++i)
347  tmp->points[i] = intensity_ (input->points[i]);
348  previous = tmp;
349 
350  FloatImagePtr img (new FloatImage (previous->width + 2*track_width_,
351  previous->height + 2*track_height_));
352 
353  pcl::copyPointCloud (*tmp, *img, track_height_, track_height_, track_width_, track_width_,
354  border_type, 0.f);
355  pyramid[0] = img;
356 
357  // compute first level gradients
358  FloatImagePtr g_x (new FloatImage (input->width, input->height));
359  FloatImagePtr g_y (new FloatImage (input->width, input->height));
360  derivatives (*img, *g_x, *g_y);
361  // copy to bigger clouds
362  FloatImagePtr grad_x (new FloatImage (previous->width + 2*track_width_,
363  previous->height + 2*track_height_));
364  pcl::copyPointCloud (*g_x, *grad_x, track_height_, track_height_, track_width_, track_width_,
365  pcl::BORDER_CONSTANT, 0.f);
366  pyramid[1] = grad_x;
367 
368  FloatImagePtr grad_y (new FloatImage (previous->width + 2*track_width_,
369  previous->height + 2*track_height_));
370  pcl::copyPointCloud (*g_y, *grad_y, track_height_, track_height_, track_width_, track_width_,
371  pcl::BORDER_CONSTANT, 0.f);
372  pyramid[2] = grad_y;
373 
374  for (int level = 1; level < nb_levels_; ++level)
375  {
376  // compute current level and current level gradients
377  FloatImageConstPtr current;
378  FloatImageConstPtr g_x;
379  FloatImageConstPtr g_y;
380  downsample (previous, current, g_x, g_y);
381  // copy to bigger clouds
382  FloatImagePtr image (new FloatImage (current->width + 2*track_width_,
383  current->height + 2*track_height_));
384  pcl::copyPointCloud (*current, *image, track_height_, track_height_, track_width_, track_width_,
385  border_type, 0.f);
386  pyramid[level*step] = image;
387  FloatImagePtr gradx (new FloatImage (g_x->width + 2*track_width_, g_x->height + 2*track_height_));
388  pcl::copyPointCloud (*g_x, *gradx, track_height_, track_height_, track_width_, track_width_,
389  pcl::BORDER_CONSTANT, 0.f);
390  pyramid[level*step + 1] = gradx;
391  FloatImagePtr grady (new FloatImage (g_y->width + 2*track_width_, g_y->height + 2*track_height_));
392  pcl::copyPointCloud (*g_y, *grady, track_height_, track_height_, track_width_, track_width_,
393  pcl::BORDER_CONSTANT, 0.f);
394  pyramid[level*step + 2] = grady;
395  // set the new level
396  previous = current;
397  }
398 }
399 
400 ///////////////////////////////////////////////////////////////////////////////////////////////
401 template <typename PointInT, typename IntensityT> void
403  const FloatImage& grad_x,
404  const FloatImage& grad_y,
405  const Eigen::Array2i& location,
406  const Eigen::Array4f& weight,
407  Eigen::ArrayXXf& win,
408  Eigen::ArrayXXf& grad_x_win,
409  Eigen::ArrayXXf& grad_y_win,
410  Eigen::Array3f &covariance) const
411 {
412  const int step = img.width;
413  covariance.setZero ();
414 
415  for (int y = 0; y < track_height_; y++)
416  {
417  const float* img_ptr = &(img.points[0]) + (y + location[1])*step + location[0];
418  const float* grad_x_ptr = &(grad_x.points[0]) + (y + location[1])*step + location[0];
419  const float* grad_y_ptr = &(grad_y.points[0]) + (y + location[1])*step + location[0];
420 
421  float* win_ptr = win.data () + y*win.cols ();
422  float* grad_x_win_ptr = grad_x_win.data () + y*grad_x_win.cols ();
423  float* grad_y_win_ptr = grad_y_win.data () + y*grad_y_win.cols ();
424 
425  for (int x =0; x < track_width_; ++x, ++grad_x_ptr, ++grad_y_ptr)
426  {
427  *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];
428  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];
429  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];
430  //!!! store those
431  *grad_x_win_ptr++ = ixval;
432  *grad_y_win_ptr++ = iyval;
433  //covariance components
434  covariance[0] += ixval*ixval;
435  covariance[1] += ixval*iyval;
436  covariance[2] += iyval*iyval;
437  }
438  }
439 }
440 
441 ///////////////////////////////////////////////////////////////////////////////////////////////
442 template <typename PointInT, typename IntensityT> void
444  const Eigen::ArrayXXf& prev_grad_x,
445  const Eigen::ArrayXXf& prev_grad_y,
446  const FloatImage& next,
447  const Eigen::Array2i& location,
448  const Eigen::Array4f& weight,
449  Eigen::Array2f &b) const
450 {
451  const int step = next.width;
452  b.setZero ();
453  for (int y = 0; y < track_height_; y++)
454  {
455  const float* next_ptr = &(next.points[0]) + (y + location[1])*step + location[0];
456  const float* prev_ptr = prev.data () + y*prev.cols ();
457  const float* prev_grad_x_ptr = prev_grad_x.data () + y*prev_grad_x.cols ();
458  const float* prev_grad_y_ptr = prev_grad_y.data () + y*prev_grad_y.cols ();
459 
460  for (int x = 0; x < track_width_; ++x, ++prev_grad_y_ptr, ++prev_grad_x_ptr)
461  {
462  float diff = next_ptr[x]*weight[0] + next_ptr[x+1]*weight[1]
463  + next_ptr[x+step]*weight[2] + next_ptr[x+step+1]*weight[3] - prev_ptr[x];
464  b[0] += *prev_grad_x_ptr * diff;
465  b[1] += *prev_grad_y_ptr * diff;
466  }
467  }
468 }
469 
470 ///////////////////////////////////////////////////////////////////////////////////////////////
471 template <typename PointInT, typename IntensityT> void
473  const PointCloudInConstPtr& input,
474  const std::vector<FloatImageConstPtr>& prev_pyramid,
475  const std::vector<FloatImageConstPtr>& pyramid,
476  const pcl::PointCloud<pcl::PointUV>::ConstPtr& prev_keypoints,
478  std::vector<int>& status,
479  Eigen::Affine3f& motion) const
480 {
481  std::vector<Eigen::Array2f, Eigen::aligned_allocator<Eigen::Array2f> > next_pts (prev_keypoints->size ());
482  Eigen::Array2f half_win ((track_width_-1)*0.5f, (track_height_-1)*0.5f);
483  pcl::TransformationFromCorrespondences transformation_computer;
484  const int nb_points = prev_keypoints->size ();
485  for (int level = nb_levels_ - 1; level >= 0; --level)
486  {
487  const FloatImage& prev = *(prev_pyramid[level*3]);
488  const FloatImage& next = *(pyramid[level*3]);
489  const FloatImage& grad_x = *(prev_pyramid[level*3+1]);
490  const FloatImage& grad_y = *(prev_pyramid[level*3+2]);
491 
492  Eigen::ArrayXXf prev_win (track_height_, track_width_);
493  Eigen::ArrayXXf grad_x_win (track_height_, track_width_);
494  Eigen::ArrayXXf grad_y_win (track_height_, track_width_);
495  float ratio (1./(1 << level));
496  for (int ptidx = 0; ptidx < nb_points; ptidx++)
497  {
498  Eigen::Array2f prev_pt (prev_keypoints->points[ptidx].u * ratio,
499  prev_keypoints->points[ptidx].v * ratio);
500  Eigen::Array2f next_pt;
501  if (level == nb_levels_ -1)
502  next_pt = prev_pt;
503  else
504  next_pt = next_pts[ptidx]*2.f;
505 
506  next_pts[ptidx] = next_pt;
507 
508  Eigen::Array2i iprev_point;
509  prev_pt -= half_win;
510  iprev_point[0] = std::floor (prev_pt[0]);
511  iprev_point[1] = std::floor (prev_pt[1]);
512 
513  if (iprev_point[0] < -track_width_ || (uint32_t) iprev_point[0] >= grad_x.width ||
514  iprev_point[1] < -track_height_ || (uint32_t) iprev_point[1] >= grad_y.height)
515  {
516  if (level == 0)
517  status [ptidx] = -1;
518  continue;
519  }
520 
521  float a = prev_pt[0] - iprev_point[0];
522  float b = prev_pt[1] - iprev_point[1];
523  Eigen::Array4f weight;
524  weight[0] = (1.f - a)*(1.f - b);
525  weight[1] = a*(1.f - b);
526  weight[2] = (1.f - a)*b;
527  weight[3] = 1 - weight[0] - weight[1] - weight[2];
528 
529  Eigen::Array3f covar = Eigen::Array3f::Zero ();
530  spatialGradient (prev, grad_x, grad_y, iprev_point, weight, prev_win, grad_x_win, grad_y_win, covar);
531 
532  float det = covar[0]*covar[2] - covar[1]*covar[1];
533  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;
534 
535  if (min_eigenvalue < min_eigenvalue_threshold_ || det < std::numeric_limits<float>::epsilon ())
536  {
537  status[ptidx] = -2;
538  continue;
539  }
540 
541  det = 1.f/det;
542  next_pt -= half_win;
543 
544  Eigen::Array2f prev_delta (0, 0);
545  for (unsigned int j = 0; j < max_iterations_; j++)
546  {
547  Eigen::Array2i inext_pt = next_pt.floor ().cast<int> ();
548 
549  if (inext_pt[0] < -track_width_ || (uint32_t) inext_pt[0] >= next.width ||
550  inext_pt[1] < -track_height_ || (uint32_t) inext_pt[1] >= next.height)
551  {
552  if (level == 0)
553  status[ptidx] = -1;
554  break;
555  }
556 
557  a = next_pt[0] - inext_pt[0];
558  b = next_pt[1] - inext_pt[1];
559  weight[0] = (1.f - a)*(1.f - b);
560  weight[1] = a*(1.f - b);
561  weight[2] = (1.f - a)*b;
562  weight[3] = 1 - weight[0] - weight[1] - weight[2];
563  // compute mismatch vector
564  Eigen::Array2f beta = Eigen::Array2f::Zero ();
565  mismatchVector (prev_win, grad_x_win, grad_y_win, next, inext_pt, weight, beta);
566  // optical flow resolution
567  Eigen::Vector2f delta ((covar[1]*beta[1] - covar[2]*beta[0])*det, (covar[1]*beta[0] - covar[0]*beta[1])*det);
568  // update position
569  next_pt[0] += delta[0]; next_pt[1] += delta[1];
570  next_pts[ptidx] = next_pt + half_win;
571 
572  if (delta.squaredNorm () <= epsilon_)
573  break;
574 
575  if (j > 0 && std::abs (delta[0] + prev_delta[0]) < 0.01 &&
576  std::abs (delta[1] + prev_delta[1]) < 0.01 )
577  {
578  next_pts[ptidx][0] -= delta[0]*0.5f;
579  next_pts[ptidx][1] -= delta[1]*0.5f;
580  break;
581  }
582  // update delta
583  prev_delta = delta;
584  }
585 
586  // update tracked points
587  if (level == 0 && !status[ptidx])
588  {
589  Eigen::Array2f next_point = next_pts[ptidx] - half_win;
590  Eigen::Array2i inext_point;
591 
592  inext_point[0] = std::floor (next_point[0]);
593  inext_point[1] = std::floor (next_point[1]);
594 
595  if (inext_point[0] < -track_width_ || (uint32_t) inext_point[0] >= next.width ||
596  inext_point[1] < -track_height_ || (uint32_t) inext_point[1] >= next.height)
597  {
598  status[ptidx] = -1;
599  continue;
600  }
601  // insert valid keypoint
602  pcl::PointUV n;
603  n.u = next_pts[ptidx][0];
604  n.v = next_pts[ptidx][1];
605  keypoints->push_back (n);
606  // add points pair to compute transformation
607  inext_point[0] = std::floor (next_pts[ptidx][0]);
608  inext_point[1] = std::floor (next_pts[ptidx][1]);
609  iprev_point[0] = std::floor (prev_keypoints->points[ptidx].u);
610  iprev_point[1] = std::floor (prev_keypoints->points[ptidx].v);
611  const PointInT& prev_pt = prev_input->points[iprev_point[1]*prev_input->width + iprev_point[0]];
612  const PointInT& next_pt = input->points[inext_point[1]*input->width + inext_point[0]];
613  transformation_computer.add (prev_pt.getVector3fMap (), next_pt.getVector3fMap (), 1.0);
614  }
615  }
616  }
617  motion = transformation_computer.getTransformation ();
618 }
619 
620 ///////////////////////////////////////////////////////////////////////////////////////////////
621 template <typename PointInT, typename IntensityT> void
623 {
624  if (!initialized_)
625  return;
626 
627  std::vector<FloatImageConstPtr> pyramid;
628  computePyramids (input_, pyramid, pcl::BORDER_REFLECT_101);
630  keypoints->reserve (keypoints_->size ());
631  std::vector<int> status (keypoints_->size (), 0);
632  track (ref_, input_, ref_pyramid_, pyramid, keypoints_, keypoints, status, motion_);
633  //swap reference and input
634  ref_ = input_;
635  ref_pyramid_ = pyramid;
636  keypoints_ = keypoints;
637  keypoints_status_->indices = status;
638 }
639 
640 #endif
typename PointCloudIn::ConstPtr PointCloudInConstPtr
Definition: pyramidal_klt.h:69
void reserve(size_t n)
Definition: point_cloud.h:462
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.
Definition: point_cloud.h:423
void convolve(const FloatImageConstPtr &input, FloatImage &output) const
Separately convolve image with decomposable convolution kernel.
FloatImage::ConstPtr FloatImageConstPtr
Definition: pyramidal_klt.h:72
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.
Definition: point_cloud.h:493
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.
bool initCompute() override
This method should get called before starting the actual computation.
uint32_t height
The point cloud height (if organized as an image-structure).
Definition: point_cloud.h:428
boost::shared_ptr< const ::pcl::PointIndices > PointIndicesConstPtr
Definition: PointIndices.h:27
uint32_t width
The point cloud width (if organized as an image-structure).
Definition: point_cloud.h:426
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
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:69
boost::shared_ptr< PointCloud< PointT > > Ptr
Definition: point_cloud.h:441
Eigen::Affine3f getTransformation()
Calculate the transformation that will best transform the points into their correspondences.
InterpolationType
Definition: io.h:229
Calculates a transformation based on corresponding 3D points.
void computeTracking() override
Abstract tracking method.
boost::shared_ptr< const PointCloud< PointT > > ConstPtr
Definition: point_cloud.h:442
void resize(size_t n)
Resize the cloud.
Definition: point_cloud.h:468
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
Define methods for measuring time spent in code blocks.
size_t size() const
Definition: point_cloud.h:461