Point Cloud Library (PCL)  1.9.1-dev
color_gradient_modality.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2010-2011, Willow Garage, 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 #pragma once
39 
40 #include <pcl/recognition/quantizable_modality.h>
41 
42 #include <pcl/pcl_base.h>
43 #include <pcl/point_cloud.h>
44 #include <pcl/point_types.h>
45 #include <pcl/recognition/point_types.h>
46 #include <pcl/filters/convolution.h>
47 
48 #include <list>
49 
50 namespace pcl
51 {
52 
53  /** \brief Modality based on max-RGB gradients.
54  * \author Stefan Holzer
55  */
56  template <typename PointInT>
58  : public QuantizableModality, public PCLBase<PointInT>
59  {
60  protected:
62 
63  /** \brief Candidate for a feature (used in feature extraction methods). */
64  struct Candidate
65  {
66  /** \brief The gradient. */
68 
69  /** \brief The x-position. */
70  int x;
71  /** \brief The y-position. */
72  int y;
73 
74  /** \brief Operator for comparing to candidates (by magnitude of the gradient).
75  * \param[in] rhs the candidate to compare with.
76  */
77  bool operator< (const Candidate & rhs) const
78  {
79  return (gradient.magnitude > rhs.gradient.magnitude);
80  }
81  };
82 
83  public:
85 
86  /** \brief Different methods for feature selection/extraction. */
88  {
90  MASK_BORDER_EQUALLY, // this gives templates most equally to the OpenCV implementation
92  };
93 
94  /** \brief Constructor. */
96  /** \brief Destructor. */
98 
99  /** \brief Sets the threshold for the gradient magnitude which is used when quantizing the data.
100  * Gradients with a smaller magnitude are ignored.
101  * \param[in] threshold the new gradient magnitude threshold.
102  */
103  inline void
104  setGradientMagnitudeThreshold (const float threshold)
105  {
106  gradient_magnitude_threshold_ = threshold;
107  }
108 
109  /** \brief Sets the threshold for the gradient magnitude which is used for feature extraction.
110  * Gradients with a smaller magnitude are ignored.
111  * \param[in] threshold the new gradient magnitude threshold.
112  */
113  inline void
115  {
116  gradient_magnitude_threshold_feature_extraction_ = threshold;
117  }
118 
119  /** \brief Sets the feature selection method.
120  * \param[in] method the feature selection method.
121  */
122  inline void
124  {
125  feature_selection_method_ = method;
126  }
127 
128  /** \brief Sets the spreading size for spreading the quantized data. */
129  inline void
130  setSpreadingSize (const size_t spreading_size)
131  {
132  spreading_size_ = spreading_size;
133  }
134 
135  /** \brief Sets whether variable feature numbers for feature extraction is enabled.
136  * \param[in] enabled enables/disables variable feature numbers for feature extraction.
137  */
138  inline void
139  setVariableFeatureNr (const bool enabled)
140  {
141  variable_feature_nr_ = enabled;
142  }
143 
144  /** \brief Returns a reference to the internally computed quantized map. */
145  inline QuantizedMap &
146  getQuantizedMap () override
147  {
148  return (filtered_quantized_color_gradients_);
149  }
150 
151  /** \brief Returns a reference to the internally computed spread quantized map. */
152  inline QuantizedMap &
154  {
155  return (spreaded_filtered_quantized_color_gradients_);
156  }
157 
158  /** \brief Returns a point cloud containing the max-RGB gradients. */
161  {
162  return (color_gradients_);
163  }
164 
165  /** \brief Extracts features from this modality within the specified mask.
166  * \param[in] mask defines the areas where features are searched in.
167  * \param[in] nr_features defines the number of features to be extracted
168  * (might be less if not sufficient information is present in the modality).
169  * \param[in] modalityIndex the index which is stored in the extracted features.
170  * \param[out] features the destination for the extracted features.
171  */
172  void
173  extractFeatures (const MaskMap & mask, size_t nr_features, size_t modalityIndex,
174  std::vector<QuantizedMultiModFeature> & features) const override;
175 
176  /** \brief Extracts all possible features from the modality within the specified mask.
177  * \param[in] mask defines the areas where features are searched in.
178  * \param[in] nr_features IGNORED (TODO: remove this parameter).
179  * \param[in] modalityIndex the index which is stored in the extracted features.
180  * \param[out] features the destination for the extracted features.
181  */
182  void
183  extractAllFeatures (const MaskMap & mask, size_t nr_features, size_t modalityIndex,
184  std::vector<QuantizedMultiModFeature> & features) const override;
185 
186  /** \brief Provide a pointer to the input dataset (overwrites the PCLBase::setInputCloud method)
187  * \param cloud the const boost shared pointer to a PointCloud message
188  */
189  void
190  setInputCloud (const typename PointCloudIn::ConstPtr & cloud) override
191  {
192  input_ = cloud;
193  }
194 
195  /** \brief Processes the input data (smoothing, computing gradients, quantizing, filtering, spreading). */
196  virtual void
197  processInputData ();
198 
199  /** \brief Processes the input data assuming that everything up to filtering is already done/available
200  * (so only spreading is performed). */
201  virtual void
203 
204  protected:
205 
206  /** \brief Computes the Gaussian kernel used for smoothing.
207  * \param[in] kernel_size the size of the Gaussian kernel.
208  * \param[in] sigma the sigma.
209  * \param[out] kernel_values the destination for the values of the kernel. */
210  void
211  computeGaussianKernel (const size_t kernel_size, const float sigma, std::vector <float> & kernel_values);
212 
213  /** \brief Computes the max-RGB gradients for the specified cloud.
214  * \param[in] cloud the cloud for which the gradients are computed.
215  */
216  void
218 
219  /** \brief Computes the max-RGB gradients for the specified cloud using sobel.
220  * \param[in] cloud the cloud for which the gradients are computed.
221  */
222  void
224 
225  /** \brief Quantizes the color gradients. */
226  void
228 
229  /** \brief Filters the quantized gradients. */
230  void
232 
233  /** \brief Erodes a mask.
234  * \param[in] mask_in the mask which will be eroded.
235  * \param[out] mask_out the destination for the eroded mask.
236  */
237  static void
238  erode (const pcl::MaskMap & mask_in, pcl::MaskMap & mask_out);
239 
240  private:
241 
242  /** \brief Determines whether variable numbers of features are extracted or not. */
243  bool variable_feature_nr_;
244 
245  /** \brief Stores a smoothed version of the input cloud. */
246  pcl::PointCloud<pcl::RGB>::Ptr smoothed_input_;
247 
248  /** \brief Defines which feature selection method is used. */
249  FeatureSelectionMethod feature_selection_method_;
250 
251  /** \brief The threshold applied on the gradient magnitudes (for quantization). */
252  float gradient_magnitude_threshold_;
253  /** \brief The threshold applied on the gradient magnitudes for feature extraction. */
254  float gradient_magnitude_threshold_feature_extraction_;
255 
256  /** \brief The point cloud which holds the max-RGB gradients. */
257  pcl::PointCloud<pcl::GradientXY> color_gradients_;
258 
259  /** \brief The spreading size. */
260  size_t spreading_size_;
261 
262  /** \brief The map which holds the quantized max-RGB gradients. */
263  pcl::QuantizedMap quantized_color_gradients_;
264  /** \brief The map which holds the filtered quantized data. */
265  pcl::QuantizedMap filtered_quantized_color_gradients_;
266  /** \brief The map which holds the spread quantized data. */
267  pcl::QuantizedMap spreaded_filtered_quantized_color_gradients_;
268 
269  };
270 
271 }
272 
273 //////////////////////////////////////////////////////////////////////////////////////////////
274 template <typename PointInT>
277  : variable_feature_nr_ (false)
278  , smoothed_input_ (new pcl::PointCloud<pcl::RGB> ())
279  , feature_selection_method_ (DISTANCE_MAGNITUDE_SCORE)
280  , gradient_magnitude_threshold_ (10.0f)
281  , gradient_magnitude_threshold_feature_extraction_ (55.0f)
282  , spreading_size_ (8)
283 {
284 }
285 
286 //////////////////////////////////////////////////////////////////////////////////////////////
287 template <typename PointInT>
290 {
291 }
292 
293 //////////////////////////////////////////////////////////////////////////////////////////////
294 template <typename PointInT> void
296 computeGaussianKernel (const size_t kernel_size, const float sigma, std::vector <float> & kernel_values)
297 {
298  // code taken from OpenCV
299  const int n = int (kernel_size);
300  const int SMALL_GAUSSIAN_SIZE = 7;
301  static const float small_gaussian_tab[][SMALL_GAUSSIAN_SIZE] =
302  {
303  {1.f},
304  {0.25f, 0.5f, 0.25f},
305  {0.0625f, 0.25f, 0.375f, 0.25f, 0.0625f},
306  {0.03125f, 0.109375f, 0.21875f, 0.28125f, 0.21875f, 0.109375f, 0.03125f}
307  };
308 
309  const float* fixed_kernel = n % 2 == 1 && n <= SMALL_GAUSSIAN_SIZE && sigma <= 0 ?
310  small_gaussian_tab[n>>1] : nullptr;
311 
312  //CV_Assert( ktype == CV_32F || ktype == CV_64F );
313  /*Mat kernel(n, 1, ktype);*/
314  kernel_values.resize (n);
315  float* cf = &(kernel_values[0]);
316  //double* cd = (double*)kernel.data;
317 
318  double sigmaX = sigma > 0 ? sigma : ((n-1)*0.5 - 1)*0.3 + 0.8;
319  double scale2X = -0.5/(sigmaX*sigmaX);
320  double sum = 0;
321 
322  for( int i = 0; i < n; i++ )
323  {
324  double x = i - (n-1)*0.5;
325  double t = fixed_kernel ? double (fixed_kernel[i]) : std::exp (scale2X*x*x);
326 
327  cf[i] = float (t);
328  sum += cf[i];
329  }
330 
331  sum = 1./sum;
332  for ( int i = 0; i < n; i++ )
333  {
334  cf[i] = float (cf[i]*sum);
335  }
336 }
337 
338 //////////////////////////////////////////////////////////////////////////////////////////////
339 template <typename PointInT>
340 void
343 {
344  // compute gaussian kernel values
345  const size_t kernel_size = 7;
346  std::vector<float> kernel_values;
347  computeGaussianKernel (kernel_size, 0.0f, kernel_values);
348 
349  // smooth input
351  Eigen::ArrayXf gaussian_kernel(kernel_size);
352  //gaussian_kernel << 1.f/16, 1.f/8, 3.f/16, 2.f/8, 3.f/16, 1.f/8, 1.f/16;
353  //gaussian_kernel << 16.f/1600.f, 32.f/1600.f, 64.f/1600.f, 128.f/1600.f, 256.f/1600.f, 128.f/1600.f, 64.f/1600.f, 32.f/1600.f, 16.f/1600.f;
354  gaussian_kernel << kernel_values[0], kernel_values[1], kernel_values[2], kernel_values[3], kernel_values[4], kernel_values[5], kernel_values[6];
355 
357 
358  const uint32_t width = input_->width;
359  const uint32_t height = input_->height;
360 
361  rgb_input_->resize (width*height);
362  rgb_input_->width = width;
363  rgb_input_->height = height;
364  rgb_input_->is_dense = input_->is_dense;
365  for (size_t row_index = 0; row_index < height; ++row_index)
366  {
367  for (size_t col_index = 0; col_index < width; ++col_index)
368  {
369  (*rgb_input_) (col_index, row_index).r = (*input_) (col_index, row_index).r;
370  (*rgb_input_) (col_index, row_index).g = (*input_) (col_index, row_index).g;
371  (*rgb_input_) (col_index, row_index).b = (*input_) (col_index, row_index).b;
372  }
373  }
374 
375  convolution.setInputCloud (rgb_input_);
376  convolution.setKernel (gaussian_kernel);
377 
378  convolution.convolve (*smoothed_input_);
379 
380  // extract color gradients
381  computeMaxColorGradientsSobel (smoothed_input_);
382 
383  // quantize gradients
385 
386  // filter quantized gradients to get only dominants one + thresholding
388 
389  // spread filtered quantized gradients
390  //spreadFilteredQunatizedColorGradients ();
391  pcl::QuantizedMap::spreadQuantizedMap (filtered_quantized_color_gradients_,
392  spreaded_filtered_quantized_color_gradients_,
393  spreading_size_);
394 }
395 
396 //////////////////////////////////////////////////////////////////////////////////////////////
397 template <typename PointInT>
398 void
401 {
402  // spread filtered quantized gradients
403  //spreadFilteredQunatizedColorGradients ();
404  pcl::QuantizedMap::spreadQuantizedMap (filtered_quantized_color_gradients_,
405  spreaded_filtered_quantized_color_gradients_,
406  spreading_size_);
407 }
408 
409 //////////////////////////////////////////////////////////////////////////////////////////////
410 template <typename PointInT>
412 extractFeatures (const MaskMap & mask, const size_t nr_features, const size_t modality_index,
413  std::vector<QuantizedMultiModFeature> & features) const
414 {
415  const size_t width = mask.getWidth ();
416  const size_t height = mask.getHeight ();
417 
418  std::list<Candidate> list1;
419  std::list<Candidate> list2;
420 
421 
422  if (feature_selection_method_ == DISTANCE_MAGNITUDE_SCORE)
423  {
424  for (size_t row_index = 0; row_index < height; ++row_index)
425  {
426  for (size_t col_index = 0; col_index < width; ++col_index)
427  {
428  if (mask (col_index, row_index) != 0)
429  {
430  const GradientXY & gradient = color_gradients_ (col_index, row_index);
431  if (gradient.magnitude > gradient_magnitude_threshold_feature_extraction_
432  && filtered_quantized_color_gradients_ (col_index, row_index) != 0)
433  {
434  Candidate candidate;
435  candidate.gradient = gradient;
436  candidate.x = static_cast<int> (col_index);
437  candidate.y = static_cast<int> (row_index);
438 
439  list1.push_back (candidate);
440  }
441  }
442  }
443  }
444 
445  list1.sort();
446 
447  if (variable_feature_nr_)
448  {
449  list2.push_back (*(list1.begin ()));
450  //while (list2.size () != nr_features)
451  bool feature_selection_finished = false;
452  while (!feature_selection_finished)
453  {
454  float best_score = 0.0f;
455  typename std::list<Candidate>::iterator best_iter = list1.end ();
456  for (typename std::list<Candidate>::iterator iter1 = list1.begin (); iter1 != list1.end (); ++iter1)
457  {
458  // find smallest distance
459  float smallest_distance = std::numeric_limits<float>::max ();
460  for (typename std::list<Candidate>::iterator iter2 = list2.begin (); iter2 != list2.end (); ++iter2)
461  {
462  const float dx = static_cast<float> (iter1->x) - static_cast<float> (iter2->x);
463  const float dy = static_cast<float> (iter1->y) - static_cast<float> (iter2->y);
464 
465  const float distance = dx*dx + dy*dy;
466 
467  if (distance < smallest_distance)
468  {
469  smallest_distance = distance;
470  }
471  }
472 
473  const float score = smallest_distance * iter1->gradient.magnitude;
474 
475  if (score > best_score)
476  {
477  best_score = score;
478  best_iter = iter1;
479  }
480  }
481 
482 
483  float min_min_sqr_distance = std::numeric_limits<float>::max ();
484  float max_min_sqr_distance = 0;
485  for (typename std::list<Candidate>::iterator iter2 = list2.begin (); iter2 != list2.end (); ++iter2)
486  {
487  float min_sqr_distance = std::numeric_limits<float>::max ();
488  for (typename std::list<Candidate>::iterator iter3 = list2.begin (); iter3 != list2.end (); ++iter3)
489  {
490  if (iter2 == iter3)
491  continue;
492 
493  const float dx = static_cast<float> (iter2->x) - static_cast<float> (iter3->x);
494  const float dy = static_cast<float> (iter2->y) - static_cast<float> (iter3->y);
495 
496  const float sqr_distance = dx*dx + dy*dy;
497 
498  if (sqr_distance < min_sqr_distance)
499  {
500  min_sqr_distance = sqr_distance;
501  }
502 
503  //std::cerr << min_sqr_distance;
504  }
505  //std::cerr << std::endl;
506 
507  // check current feature
508  {
509  const float dx = static_cast<float> (iter2->x) - static_cast<float> (best_iter->x);
510  const float dy = static_cast<float> (iter2->y) - static_cast<float> (best_iter->y);
511 
512  const float sqr_distance = dx*dx + dy*dy;
513 
514  if (sqr_distance < min_sqr_distance)
515  {
516  min_sqr_distance = sqr_distance;
517  }
518  }
519 
520  if (min_sqr_distance < min_min_sqr_distance)
521  min_min_sqr_distance = min_sqr_distance;
522  if (min_sqr_distance > max_min_sqr_distance)
523  max_min_sqr_distance = min_sqr_distance;
524 
525  //std::cerr << min_sqr_distance << ", " << min_min_sqr_distance << ", " << max_min_sqr_distance << std::endl;
526  }
527 
528  if (best_iter != list1.end ())
529  {
530  //std::cerr << "feature_index: " << list2.size () << std::endl;
531  //std::cerr << "min_min_sqr_distance: " << min_min_sqr_distance << std::endl;
532  //std::cerr << "max_min_sqr_distance: " << max_min_sqr_distance << std::endl;
533 
534  if (min_min_sqr_distance < 50)
535  {
536  feature_selection_finished = true;
537  break;
538  }
539 
540  list2.push_back (*best_iter);
541  }
542  }
543  }
544  else
545  {
546  if (list1.size () <= nr_features)
547  {
548  for (typename std::list<Candidate>::iterator iter1 = list1.begin (); iter1 != list1.end (); ++iter1)
549  {
550  QuantizedMultiModFeature feature;
551 
552  feature.x = iter1->x;
553  feature.y = iter1->y;
554  feature.modality_index = modality_index;
555  feature.quantized_value = filtered_quantized_color_gradients_ (iter1->x, iter1->y);
556 
557  features.push_back (feature);
558  }
559  return;
560  }
561 
562  list2.push_back (*(list1.begin ()));
563  while (list2.size () != nr_features)
564  {
565  float best_score = 0.0f;
566  typename std::list<Candidate>::iterator best_iter = list1.end ();
567  for (typename std::list<Candidate>::iterator iter1 = list1.begin (); iter1 != list1.end (); ++iter1)
568  {
569  // find smallest distance
570  float smallest_distance = std::numeric_limits<float>::max ();
571  for (typename std::list<Candidate>::iterator iter2 = list2.begin (); iter2 != list2.end (); ++iter2)
572  {
573  const float dx = static_cast<float> (iter1->x) - static_cast<float> (iter2->x);
574  const float dy = static_cast<float> (iter1->y) - static_cast<float> (iter2->y);
575 
576  const float distance = dx*dx + dy*dy;
577 
578  if (distance < smallest_distance)
579  {
580  smallest_distance = distance;
581  }
582  }
583 
584  const float score = smallest_distance * iter1->gradient.magnitude;
585 
586  if (score > best_score)
587  {
588  best_score = score;
589  best_iter = iter1;
590  }
591  }
592 
593  if (best_iter != list1.end ())
594  {
595  list2.push_back (*best_iter);
596  }
597  else
598  {
599  break;
600  }
601  }
602  }
603  }
604  else if (feature_selection_method_ == MASK_BORDER_HIGH_GRADIENTS || feature_selection_method_ == MASK_BORDER_EQUALLY)
605  {
606  MaskMap eroded_mask;
607  erode (mask, eroded_mask);
608 
609  MaskMap diff_mask;
610  MaskMap::getDifferenceMask (mask, eroded_mask, diff_mask);
611 
612  for (size_t row_index = 0; row_index < height; ++row_index)
613  {
614  for (size_t col_index = 0; col_index < width; ++col_index)
615  {
616  if (diff_mask (col_index, row_index) != 0)
617  {
618  const GradientXY & gradient = color_gradients_ (col_index, row_index);
619  if ((feature_selection_method_ == MASK_BORDER_EQUALLY || gradient.magnitude > gradient_magnitude_threshold_feature_extraction_)
620  && filtered_quantized_color_gradients_ (col_index, row_index) != 0)
621  {
622  Candidate candidate;
623  candidate.gradient = gradient;
624  candidate.x = static_cast<int> (col_index);
625  candidate.y = static_cast<int> (row_index);
626 
627  list1.push_back (candidate);
628  }
629  }
630  }
631  }
632 
633  list1.sort();
634 
635  if (list1.size () <= nr_features)
636  {
637  for (typename std::list<Candidate>::iterator iter1 = list1.begin (); iter1 != list1.end (); ++iter1)
638  {
639  QuantizedMultiModFeature feature;
640 
641  feature.x = iter1->x;
642  feature.y = iter1->y;
643  feature.modality_index = modality_index;
644  feature.quantized_value = filtered_quantized_color_gradients_ (iter1->x, iter1->y);
645 
646  features.push_back (feature);
647  }
648  return;
649  }
650 
651  size_t distance = list1.size () / nr_features + 1; // ???
652  while (list2.size () != nr_features)
653  {
654  const size_t sqr_distance = distance*distance;
655  for (typename std::list<Candidate>::iterator iter1 = list1.begin (); iter1 != list1.end (); ++iter1)
656  {
657  bool candidate_accepted = true;
658 
659  for (typename std::list<Candidate>::iterator iter2 = list2.begin (); iter2 != list2.end (); ++iter2)
660  {
661  const int dx = iter1->x - iter2->x;
662  const int dy = iter1->y - iter2->y;
663  const unsigned int tmp_distance = dx*dx + dy*dy;
664 
665  //if (tmp_distance < distance)
666  if (tmp_distance < sqr_distance)
667  {
668  candidate_accepted = false;
669  break;
670  }
671  }
672 
673  if (candidate_accepted)
674  list2.push_back (*iter1);
675 
676  if (list2.size () == nr_features)
677  break;
678  }
679  --distance;
680  }
681  }
682 
683  for (typename std::list<Candidate>::iterator iter2 = list2.begin (); iter2 != list2.end (); ++iter2)
684  {
685  QuantizedMultiModFeature feature;
686 
687  feature.x = iter2->x;
688  feature.y = iter2->y;
689  feature.modality_index = modality_index;
690  feature.quantized_value = filtered_quantized_color_gradients_ (iter2->x, iter2->y);
691 
692  features.push_back (feature);
693  }
694 }
695 
696 //////////////////////////////////////////////////////////////////////////////////////////////
697 template <typename PointInT> void
699 extractAllFeatures (const MaskMap & mask, const size_t, const size_t modality_index,
700  std::vector<QuantizedMultiModFeature> & features) const
701 {
702  const size_t width = mask.getWidth ();
703  const size_t height = mask.getHeight ();
704 
705  std::list<Candidate> list1;
706  std::list<Candidate> list2;
707 
708 
709  for (size_t row_index = 0; row_index < height; ++row_index)
710  {
711  for (size_t col_index = 0; col_index < width; ++col_index)
712  {
713  if (mask (col_index, row_index) != 0)
714  {
715  const GradientXY & gradient = color_gradients_ (col_index, row_index);
716  if (gradient.magnitude > gradient_magnitude_threshold_feature_extraction_
717  && filtered_quantized_color_gradients_ (col_index, row_index) != 0)
718  {
719  Candidate candidate;
720  candidate.gradient = gradient;
721  candidate.x = static_cast<int> (col_index);
722  candidate.y = static_cast<int> (row_index);
723 
724  list1.push_back (candidate);
725  }
726  }
727  }
728  }
729 
730  list1.sort();
731 
732  for (typename std::list<Candidate>::iterator iter1 = list1.begin (); iter1 != list1.end (); ++iter1)
733  {
734  QuantizedMultiModFeature feature;
735 
736  feature.x = iter1->x;
737  feature.y = iter1->y;
738  feature.modality_index = modality_index;
739  feature.quantized_value = filtered_quantized_color_gradients_ (iter1->x, iter1->y);
740 
741  features.push_back (feature);
742  }
743 }
744 
745 //////////////////////////////////////////////////////////////////////////////////////////////
746 template <typename PointInT>
747 void
750 {
751  const int width = cloud->width;
752  const int height = cloud->height;
753 
754  color_gradients_.points.resize (width*height);
755  color_gradients_.width = width;
756  color_gradients_.height = height;
757 
758  const float pi = tan (1.0f) * 2;
759  for (int row_index = 0; row_index < height-2; ++row_index)
760  {
761  for (int col_index = 0; col_index < width-2; ++col_index)
762  {
763  const int index0 = row_index*width+col_index;
764  const int index_c = row_index*width+col_index+2;
765  const int index_r = (row_index+2)*width+col_index;
766 
767  //const int index_d = (row_index+1)*width+col_index+1;
768 
769  const unsigned char r0 = cloud->points[index0].r;
770  const unsigned char g0 = cloud->points[index0].g;
771  const unsigned char b0 = cloud->points[index0].b;
772 
773  const unsigned char r_c = cloud->points[index_c].r;
774  const unsigned char g_c = cloud->points[index_c].g;
775  const unsigned char b_c = cloud->points[index_c].b;
776 
777  const unsigned char r_r = cloud->points[index_r].r;
778  const unsigned char g_r = cloud->points[index_r].g;
779  const unsigned char b_r = cloud->points[index_r].b;
780 
781  const float r_dx = static_cast<float> (r_c) - static_cast<float> (r0);
782  const float g_dx = static_cast<float> (g_c) - static_cast<float> (g0);
783  const float b_dx = static_cast<float> (b_c) - static_cast<float> (b0);
784 
785  const float r_dy = static_cast<float> (r_r) - static_cast<float> (r0);
786  const float g_dy = static_cast<float> (g_r) - static_cast<float> (g0);
787  const float b_dy = static_cast<float> (b_r) - static_cast<float> (b0);
788 
789  const float sqr_mag_r = r_dx*r_dx + r_dy*r_dy;
790  const float sqr_mag_g = g_dx*g_dx + g_dy*g_dy;
791  const float sqr_mag_b = b_dx*b_dx + b_dy*b_dy;
792 
793  if (sqr_mag_r > sqr_mag_g && sqr_mag_r > sqr_mag_b)
794  {
795  GradientXY gradient;
796  gradient.magnitude = sqrt (sqr_mag_r);
797  gradient.angle = std::atan2 (r_dy, r_dx) * 180.0f / pi;
798  gradient.x = static_cast<float> (col_index);
799  gradient.y = static_cast<float> (row_index);
800 
801  color_gradients_ (col_index+1, row_index+1) = gradient;
802  }
803  else if (sqr_mag_g > sqr_mag_b)
804  {
805  GradientXY gradient;
806  gradient.magnitude = sqrt (sqr_mag_g);
807  gradient.angle = std::atan2 (g_dy, g_dx) * 180.0f / pi;
808  gradient.x = static_cast<float> (col_index);
809  gradient.y = static_cast<float> (row_index);
810 
811  color_gradients_ (col_index+1, row_index+1) = gradient;
812  }
813  else
814  {
815  GradientXY gradient;
816  gradient.magnitude = sqrt (sqr_mag_b);
817  gradient.angle = std::atan2 (b_dy, b_dx) * 180.0f / pi;
818  gradient.x = static_cast<float> (col_index);
819  gradient.y = static_cast<float> (row_index);
820 
821  color_gradients_ (col_index+1, row_index+1) = gradient;
822  }
823 
824  assert (color_gradients_ (col_index+1, row_index+1).angle >= -180 &&
825  color_gradients_ (col_index+1, row_index+1).angle <= 180);
826  }
827  }
828 
829  return;
830 }
831 
832 //////////////////////////////////////////////////////////////////////////////////////////////
833 template <typename PointInT>
834 void
837 {
838  const int width = cloud->width;
839  const int height = cloud->height;
840 
841  color_gradients_.points.resize (width*height);
842  color_gradients_.width = width;
843  color_gradients_.height = height;
844 
845  const float pi = tanf (1.0f) * 2.0f;
846  for (int row_index = 1; row_index < height-1; ++row_index)
847  {
848  for (int col_index = 1; col_index < width-1; ++col_index)
849  {
850  const int r7 = static_cast<int> (cloud->points[(row_index-1)*width + (col_index-1)].r);
851  const int g7 = static_cast<int> (cloud->points[(row_index-1)*width + (col_index-1)].g);
852  const int b7 = static_cast<int> (cloud->points[(row_index-1)*width + (col_index-1)].b);
853  const int r8 = static_cast<int> (cloud->points[(row_index-1)*width + (col_index)].r);
854  const int g8 = static_cast<int> (cloud->points[(row_index-1)*width + (col_index)].g);
855  const int b8 = static_cast<int> (cloud->points[(row_index-1)*width + (col_index)].b);
856  const int r9 = static_cast<int> (cloud->points[(row_index-1)*width + (col_index+1)].r);
857  const int g9 = static_cast<int> (cloud->points[(row_index-1)*width + (col_index+1)].g);
858  const int b9 = static_cast<int> (cloud->points[(row_index-1)*width + (col_index+1)].b);
859  const int r4 = static_cast<int> (cloud->points[(row_index)*width + (col_index-1)].r);
860  const int g4 = static_cast<int> (cloud->points[(row_index)*width + (col_index-1)].g);
861  const int b4 = static_cast<int> (cloud->points[(row_index)*width + (col_index-1)].b);
862  const int r6 = static_cast<int> (cloud->points[(row_index)*width + (col_index+1)].r);
863  const int g6 = static_cast<int> (cloud->points[(row_index)*width + (col_index+1)].g);
864  const int b6 = static_cast<int> (cloud->points[(row_index)*width + (col_index+1)].b);
865  const int r1 = static_cast<int> (cloud->points[(row_index+1)*width + (col_index-1)].r);
866  const int g1 = static_cast<int> (cloud->points[(row_index+1)*width + (col_index-1)].g);
867  const int b1 = static_cast<int> (cloud->points[(row_index+1)*width + (col_index-1)].b);
868  const int r2 = static_cast<int> (cloud->points[(row_index+1)*width + (col_index)].r);
869  const int g2 = static_cast<int> (cloud->points[(row_index+1)*width + (col_index)].g);
870  const int b2 = static_cast<int> (cloud->points[(row_index+1)*width + (col_index)].b);
871  const int r3 = static_cast<int> (cloud->points[(row_index+1)*width + (col_index+1)].r);
872  const int g3 = static_cast<int> (cloud->points[(row_index+1)*width + (col_index+1)].g);
873  const int b3 = static_cast<int> (cloud->points[(row_index+1)*width + (col_index+1)].b);
874 
875  //const int r_tmp1 = - r7 + r3;
876  //const int r_tmp2 = - r1 + r9;
877  //const int g_tmp1 = - g7 + g3;
878  //const int g_tmp2 = - g1 + g9;
879  //const int b_tmp1 = - b7 + b3;
880  //const int b_tmp2 = - b1 + b9;
881  ////const int gx = - r7 - (r4<<2) - r1 + r3 + (r6<<2) + r9;
882  ////const int gy = - r7 - (r8<<2) - r9 + r1 + (r2<<2) + r3;
883  //const int r_dx = r_tmp1 + r_tmp2 - (r4<<2) + (r6<<2);
884  //const int r_dy = r_tmp1 - r_tmp2 - (r8<<2) + (r2<<2);
885  //const int g_dx = g_tmp1 + g_tmp2 - (g4<<2) + (g6<<2);
886  //const int g_dy = g_tmp1 - g_tmp2 - (g8<<2) + (g2<<2);
887  //const int b_dx = b_tmp1 + b_tmp2 - (b4<<2) + (b6<<2);
888  //const int b_dy = b_tmp1 - b_tmp2 - (b8<<2) + (b2<<2);
889 
890  //const int r_tmp1 = - r7 + r3;
891  //const int r_tmp2 = - r1 + r9;
892  //const int g_tmp1 = - g7 + g3;
893  //const int g_tmp2 = - g1 + g9;
894  //const int b_tmp1 = - b7 + b3;
895  //const int b_tmp2 = - b1 + b9;
896  //const int gx = - r7 - (r4<<2) - r1 + r3 + (r6<<2) + r9;
897  //const int gy = - r7 - (r8<<2) - r9 + r1 + (r2<<2) + r3;
898  const int r_dx = r9 + 2*r6 + r3 - (r7 + 2*r4 + r1);
899  const int r_dy = r1 + 2*r2 + r3 - (r7 + 2*r8 + r9);
900  const int g_dx = g9 + 2*g6 + g3 - (g7 + 2*g4 + g1);
901  const int g_dy = g1 + 2*g2 + g3 - (g7 + 2*g8 + g9);
902  const int b_dx = b9 + 2*b6 + b3 - (b7 + 2*b4 + b1);
903  const int b_dy = b1 + 2*b2 + b3 - (b7 + 2*b8 + b9);
904 
905  const int sqr_mag_r = r_dx*r_dx + r_dy*r_dy;
906  const int sqr_mag_g = g_dx*g_dx + g_dy*g_dy;
907  const int sqr_mag_b = b_dx*b_dx + b_dy*b_dy;
908 
909  if (sqr_mag_r > sqr_mag_g && sqr_mag_r > sqr_mag_b)
910  {
911  GradientXY gradient;
912  gradient.magnitude = std::sqrt (static_cast<float> (sqr_mag_r));
913  gradient.angle = std::atan2 (static_cast<float> (r_dy), static_cast<float> (r_dx)) * 180.0f / pi;
914  if (gradient.angle < -180.0f) gradient.angle += 360.0f;
915  if (gradient.angle >= 180.0f) gradient.angle -= 360.0f;
916  gradient.x = static_cast<float> (col_index);
917  gradient.y = static_cast<float> (row_index);
918 
919  color_gradients_ (col_index, row_index) = gradient;
920  }
921  else if (sqr_mag_g > sqr_mag_b)
922  {
923  GradientXY gradient;
924  gradient.magnitude = std::sqrt (static_cast<float> (sqr_mag_g));
925  gradient.angle = std::atan2 (static_cast<float> (g_dy), static_cast<float> (g_dx)) * 180.0f / pi;
926  if (gradient.angle < -180.0f) gradient.angle += 360.0f;
927  if (gradient.angle >= 180.0f) gradient.angle -= 360.0f;
928  gradient.x = static_cast<float> (col_index);
929  gradient.y = static_cast<float> (row_index);
930 
931  color_gradients_ (col_index, row_index) = gradient;
932  }
933  else
934  {
935  GradientXY gradient;
936  gradient.magnitude = std::sqrt (static_cast<float> (sqr_mag_b));
937  gradient.angle = std::atan2 (static_cast<float> (b_dy), static_cast<float> (b_dx)) * 180.0f / pi;
938  if (gradient.angle < -180.0f) gradient.angle += 360.0f;
939  if (gradient.angle >= 180.0f) gradient.angle -= 360.0f;
940  gradient.x = static_cast<float> (col_index);
941  gradient.y = static_cast<float> (row_index);
942 
943  color_gradients_ (col_index, row_index) = gradient;
944  }
945 
946  assert (color_gradients_ (col_index, row_index).angle >= -180 &&
947  color_gradients_ (col_index, row_index).angle <= 180);
948  }
949  }
950 
951  return;
952 }
953 
954 //////////////////////////////////////////////////////////////////////////////////////////////
955 template <typename PointInT>
956 void
959 {
960  //std::cerr << "quantize this, bastard!!!" << std::endl;
961 
962  //unsigned char quantization_map[16] = {0,1,2,3,4,5,6,7,0,1,2,3,4,5,6,7};
963  //unsigned char quantization_map[16] = {1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8};
964 
965  //for (float angle = 0.0f; angle < 360.0f; angle += 1.0f)
966  //{
967  // const int quantized_value = quantization_map[static_cast<int> (angle * angleScale)];
968  // std::cerr << angle << ": " << quantized_value << std::endl;
969  //}
970 
971 
972  const size_t width = input_->width;
973  const size_t height = input_->height;
974 
975  quantized_color_gradients_.resize (width, height);
976 
977  const float angleScale = 16.0f/360.0f;
978 
979  //float min_angle = std::numeric_limits<float>::max ();
980  //float max_angle = -std::numeric_limits<float>::max ();
981  for (size_t row_index = 0; row_index < height; ++row_index)
982  {
983  for (size_t col_index = 0; col_index < width; ++col_index)
984  {
985  if (color_gradients_ (col_index, row_index).magnitude < gradient_magnitude_threshold_)
986  {
987  quantized_color_gradients_ (col_index, row_index) = 0;
988  continue;
989  }
990 
991  const float angle = 11.25f + color_gradients_ (col_index, row_index).angle + 180.0f;
992  const int quantized_value = (static_cast<int> (angle * angleScale)) & 7;
993  quantized_color_gradients_ (col_index, row_index) = static_cast<unsigned char> (quantized_value + 1);
994 
995  //const float angle = color_gradients_ (col_index, row_index).angle + 180.0f;
996 
997  //min_angle = std::min (min_angle, angle);
998  //max_angle = std::max (max_angle, angle);
999 
1000  //if (angle < 0.0f || angle >= 360.0f)
1001  //{
1002  // std::cerr << "angle shitty: " << angle << std::endl;
1003  //}
1004 
1005  //const int quantized_value = quantization_map[static_cast<int> (angle * angleScale)];
1006  //quantized_color_gradients_ (col_index, row_index) = static_cast<unsigned char> (quantized_value);
1007 
1008  //assert (0 <= quantized_value && quantized_value < 16);
1009  //quantized_color_gradients_ (col_index, row_index) = quantization_map[quantized_value];
1010  //quantized_color_gradients_ (col_index, row_index) = static_cast<unsigned char> ((quantized_value & 7) + 1); // = (quantized_value % 8) + 1
1011  }
1012  }
1013 
1014  //std::cerr << ">>>>> " << min_angle << ", " << max_angle << std::endl;
1015 }
1016 
1017 //////////////////////////////////////////////////////////////////////////////////////////////
1018 template <typename PointInT>
1019 void
1022 {
1023  const size_t width = input_->width;
1024  const size_t height = input_->height;
1025 
1026  filtered_quantized_color_gradients_.resize (width, height);
1027 
1028  // filter data
1029  for (size_t row_index = 1; row_index < height-1; ++row_index)
1030  {
1031  for (size_t col_index = 1; col_index < width-1; ++col_index)
1032  {
1033  unsigned char histogram[9] = {0,0,0,0,0,0,0,0,0};
1034 
1035  {
1036  const unsigned char * data_ptr = quantized_color_gradients_.getData () + (row_index-1)*width+col_index-1;
1037  assert (data_ptr[0] < 9 && data_ptr[1] < 9 && data_ptr[2] < 9);
1038  ++histogram[data_ptr[0]];
1039  ++histogram[data_ptr[1]];
1040  ++histogram[data_ptr[2]];
1041  }
1042  {
1043  const unsigned char * data_ptr = quantized_color_gradients_.getData () + row_index*width+col_index-1;
1044  assert (data_ptr[0] < 9 && data_ptr[1] < 9 && data_ptr[2] < 9);
1045  ++histogram[data_ptr[0]];
1046  ++histogram[data_ptr[1]];
1047  ++histogram[data_ptr[2]];
1048  }
1049  {
1050  const unsigned char * data_ptr = quantized_color_gradients_.getData () + (row_index+1)*width+col_index-1;
1051  assert (data_ptr[0] < 9 && data_ptr[1] < 9 && data_ptr[2] < 9);
1052  ++histogram[data_ptr[0]];
1053  ++histogram[data_ptr[1]];
1054  ++histogram[data_ptr[2]];
1055  }
1056 
1057  unsigned char max_hist_value = 0;
1058  int max_hist_index = -1;
1059 
1060  // for (int i = 0; i < 8; ++i)
1061  // {
1062  // if (max_hist_value < histogram[i+1])
1063  // {
1064  // max_hist_index = i;
1065  // max_hist_value = histogram[i+1]
1066  // }
1067  // }
1068  // Unrolled for performance optimization:
1069  if (max_hist_value < histogram[1]) {max_hist_index = 0; max_hist_value = histogram[1];}
1070  if (max_hist_value < histogram[2]) {max_hist_index = 1; max_hist_value = histogram[2];}
1071  if (max_hist_value < histogram[3]) {max_hist_index = 2; max_hist_value = histogram[3];}
1072  if (max_hist_value < histogram[4]) {max_hist_index = 3; max_hist_value = histogram[4];}
1073  if (max_hist_value < histogram[5]) {max_hist_index = 4; max_hist_value = histogram[5];}
1074  if (max_hist_value < histogram[6]) {max_hist_index = 5; max_hist_value = histogram[6];}
1075  if (max_hist_value < histogram[7]) {max_hist_index = 6; max_hist_value = histogram[7];}
1076  if (max_hist_value < histogram[8]) {max_hist_index = 7; max_hist_value = histogram[8];}
1077 
1078  if (max_hist_index != -1 && max_hist_value >= 5)
1079  filtered_quantized_color_gradients_ (col_index, row_index) = static_cast<unsigned char> (0x1 << max_hist_index);
1080  else
1081  filtered_quantized_color_gradients_ (col_index, row_index) = 0;
1082 
1083  }
1084  }
1085 }
1086 
1087 //////////////////////////////////////////////////////////////////////////////////////////////
1088 template <typename PointInT>
1089 void
1091 erode (const pcl::MaskMap & mask_in,
1092  pcl::MaskMap & mask_out)
1093 {
1094  const size_t width = mask_in.getWidth ();
1095  const size_t height = mask_in.getHeight ();
1096 
1097  mask_out.resize (width, height);
1098 
1099  for (size_t row_index = 1; row_index < height-1; ++row_index)
1100  {
1101  for (size_t col_index = 1; col_index < width-1; ++col_index)
1102  {
1103  if (mask_in (col_index, row_index-1) == 0 ||
1104  mask_in (col_index-1, row_index) == 0 ||
1105  mask_in (col_index+1, row_index) == 0 ||
1106  mask_in (col_index, row_index+1) == 0)
1107  {
1108  mask_out (col_index, row_index) = 0;
1109  }
1110  else
1111  {
1112  mask_out (col_index, row_index) = 255;
1113  }
1114  }
1115  }
1116 }
QuantizedMap & getQuantizedMap() override
Returns a reference to the internally computed quantized map.
std::vector< PointT, Eigen::aligned_allocator< PointT > > points
The point data.
Definition: point_cloud.h:411
static void getDifferenceMask(const MaskMap &mask0, const MaskMap &mask1, MaskMap &diff_mask)
This file defines compatibility wrappers for low level I/O functions.
Definition: convolution.h:45
Feature that defines a position and quantized value in a specific modality.
void computeMaxColorGradientsSobel(const typename pcl::PointCloud< pcl::RGB >::ConstPtr &cloud)
Computes the max-RGB gradients for the specified cloud using sobel.
void convolve(const Eigen::ArrayXf &h_kernel, const Eigen::ArrayXf &v_kernel, PointCloudOut &output)
Convolve point cloud with an horizontal kernel along rows then vertical kernel along columns : convol...
void quantizeColorGradients()
Quantizes the color gradients.
void setFeatureSelectionMethod(const FeatureSelectionMethod method)
Sets the feature selection method.
unsigned char quantized_value
the quantized value attached to the feature.
uint32_t height
The point cloud height (if organized as an image-structure).
Definition: point_cloud.h:416
void resize(size_t width, size_t height)
void setKernel(const Eigen::ArrayXf &kernel)
Set convolving kernel.
Definition: convolution.h:108
A point structure representing Euclidean xyz coordinates, and the intensity value.
Definition: point_types.h:51
A structure representing RGB color information.
uint32_t width
The point cloud width (if organized as an image-structure).
Definition: point_cloud.h:414
Convolution is a mathematical operation on two functions f and g, producing a third function that is ...
Definition: convolution.h:76
QuantizedMap & getSpreadedQuantizedMap() override
Returns a reference to the internally computed spread quantized map.
void resize(size_t width, size_t height)
void computeGaussianKernel(const size_t kernel_size, const float sigma, std::vector< float > &kernel_values)
Computes the Gaussian kernel used for smoothing.
void setInputCloud(const PointCloudInConstPtr &cloud)
Provide a pointer to the input dataset.
Definition: convolution.h:103
PCL base class.
Definition: pcl_base.h:69
void setGradientMagnitudeThresholdForFeatureExtraction(const float threshold)
Sets the threshold for the gradient magnitude which is used for feature extraction.
void computeMaxColorGradients(const typename pcl::PointCloud< pcl::RGB >::ConstPtr &cloud)
Computes the max-RGB gradients for the specified cloud.
size_t getWidth() const
Definition: mask_map.h:57
void setInputCloud(const typename PointCloudIn::ConstPtr &cloud) override
Provide a pointer to the input dataset (overwrites the PCLBase::setInputCloud method) ...
Interface for a quantizable modality.
boost::shared_ptr< PointCloud< PointT > > Ptr
Definition: point_cloud.h:429
size_t getHeight() const
Definition: mask_map.h:60
virtual void processInputDataFromFiltered()
Processes the input data assuming that everything up to filtering is already done/available (so only ...
Modality based on max-RGB gradients.
void filterQuantizedColorGradients()
Filters the quantized gradients.
unsigned char * getData()
Definition: quantized_map.h:62
boost::shared_ptr< const PointCloud< PointRGBT > > ConstPtr
Definition: point_cloud.h:430
FeatureSelectionMethod
Different methods for feature selection/extraction.
bool is_dense
True if no points are invalid (e.g., have NaN or Inf values in any of their floating point fields)...
Definition: point_cloud.h:419
void setSpreadingSize(const size_t spreading_size)
Sets the spreading size for spreading the quantized data.
virtual void processInputData()
Processes the input data (smoothing, computing gradients, quantizing, filtering, spreading).
PointCloudConstPtr input_
The input point cloud dataset.
Definition: pcl_base.h:151
void extractAllFeatures(const MaskMap &mask, size_t nr_features, size_t modalityIndex, std::vector< QuantizedMultiModFeature > &features) const override
Extracts all possible features from the modality within the specified mask.
void resize(size_t n)
Resize the cloud.
Definition: point_cloud.h:456
static void erode(const pcl::MaskMap &mask_in, pcl::MaskMap &mask_out)
Erodes a mask.
void extractFeatures(const MaskMap &mask, size_t nr_features, size_t modalityIndex, std::vector< QuantizedMultiModFeature > &features) const override
Extracts features from this modality within the specified mask.
static void spreadQuantizedMap(const QuantizedMap &input_map, QuantizedMap &output_map, size_t spreading_size)
void setGradientMagnitudeThreshold(const float threshold)
Sets the threshold for the gradient magnitude which is used when quantizing the data.
bool operator<(const Candidate &rhs) const
Operator for comparing to candidates (by magnitude of the gradient).
void setVariableFeatureNr(const bool enabled)
Sets whether variable feature numbers for feature extraction is enabled.
pcl::PointCloud< pcl::GradientXY > & getMaxColorGradients()
Returns a point cloud containing the max-RGB gradients.
size_t modality_index
the index of the corresponding modality.
Candidate for a feature (used in feature extraction methods).