Point Cloud Library (PCL)  1.9.1-dev
multiscale_feature_persistence.hpp
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2011, Alexandru-Eugen Ichim
6  * Copyright (c) 2012-, Open Perception, Inc.
7  *
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * * Redistributions of source code must retain the above copyright
15  * notice, this list of conditions and the following disclaimer.
16  * * Redistributions in binary form must reproduce the above
17  * copyright notice, this list of conditions and the following
18  * disclaimer in the documentation and/or other materials provided
19  * with the distribution.
20  * * Neither the name of the copyright holder(s) nor the names of its
21  * contributors may be used to endorse or promote products derived
22  * from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  *
37  * $Id$
38  */
39 
40 #ifndef PCL_FEATURES_IMPL_MULTISCALE_FEATURE_PERSISTENCE_H_
41 #define PCL_FEATURES_IMPL_MULTISCALE_FEATURE_PERSISTENCE_H_
42 
43 #include <pcl/features/multiscale_feature_persistence.h>
44 
45 //////////////////////////////////////////////////////////////////////////////////////////////
46 template <typename PointSource, typename PointFeature>
48  alpha_ (0),
49  distance_metric_ (L1),
50  feature_estimator_ (),
51  features_at_scale_ (),
52  feature_representation_ ()
53 {
54  feature_representation_.reset (new DefaultPointRepresentation<PointFeature>);
55  // No input is needed, hack around the initCompute () check from PCLBase
56  input_.reset (new pcl::PointCloud<PointSource> ());
57 }
58 
59 
60 //////////////////////////////////////////////////////////////////////////////////////////////
61 template <typename PointSource, typename PointFeature> bool
63 {
65  {
66  PCL_ERROR ("[pcl::MultiscaleFeaturePersistence::initCompute] PCLBase::initCompute () failed - no input cloud was given.\n");
67  return false;
68  }
69  if (!feature_estimator_)
70  {
71  PCL_ERROR ("[pcl::MultiscaleFeaturePersistence::initCompute] No feature estimator was set\n");
72  return false;
73  }
74  if (scale_values_.empty ())
75  {
76  PCL_ERROR ("[pcl::MultiscaleFeaturePersistence::initCompute] No scale values were given\n");
77  return false;
78  }
79 
80  mean_feature_.resize (feature_representation_->getNumberOfDimensions ());
81 
82  return true;
83 }
84 
85 
86 //////////////////////////////////////////////////////////////////////////////////////////////
87 template <typename PointSource, typename PointFeature> void
89 {
90  features_at_scale_.resize (scale_values_.size ());
91  features_at_scale_vectorized_.resize (scale_values_.size ());
92  for (size_t scale_i = 0; scale_i < scale_values_.size (); ++scale_i)
93  {
94  FeatureCloudPtr feature_cloud (new FeatureCloud ());
95  computeFeatureAtScale (scale_values_[scale_i], feature_cloud);
96  features_at_scale_[scale_i] = feature_cloud;
97 
98  // Vectorize each feature and insert it into the vectorized feature storage
99  std::vector<std::vector<float> > feature_cloud_vectorized (feature_cloud->points.size ());
100  for (size_t feature_i = 0; feature_i < feature_cloud->points.size (); ++feature_i)
101  {
102  std::vector<float> feature_vectorized (feature_representation_->getNumberOfDimensions ());
103  feature_representation_->vectorize (feature_cloud->points[feature_i], feature_vectorized);
104  feature_cloud_vectorized[feature_i] = feature_vectorized;
105  }
106  features_at_scale_vectorized_[scale_i] = feature_cloud_vectorized;
107  }
108 }
109 
110 
111 //////////////////////////////////////////////////////////////////////////////////////////////
112 template <typename PointSource, typename PointFeature> void
114  FeatureCloudPtr &features)
115 {
116  feature_estimator_->setRadiusSearch (scale);
117  feature_estimator_->compute (*features);
118 }
119 
120 
121 //////////////////////////////////////////////////////////////////////////////////////////////
122 template <typename PointSource, typename PointFeature> float
124  const std::vector<float> &b)
125 {
126  return (pcl::selectNorm<std::vector<float> > (a, b, static_cast<int> (a.size ()), distance_metric_));
127 }
128 
129 
130 //////////////////////////////////////////////////////////////////////////////////////////////
131 template <typename PointSource, typename PointFeature> void
133 {
134  // Reset mean feature
135  for (int i = 0; i < feature_representation_->getNumberOfDimensions (); ++i)
136  mean_feature_[i] = 0.0f;
137 
138  float normalization_factor = 0.0f;
139  for (std::vector<std::vector<std::vector<float> > >::iterator scale_it = features_at_scale_vectorized_.begin (); scale_it != features_at_scale_vectorized_.end(); ++scale_it) {
140  normalization_factor += static_cast<float> (scale_it->size ());
141  for (const auto &feature : *scale_it)
142  for (int dim_i = 0; dim_i < feature_representation_->getNumberOfDimensions (); ++dim_i)
143  mean_feature_[dim_i] += feature[dim_i];
144  }
145 
146  for (int dim_i = 0; dim_i < feature_representation_->getNumberOfDimensions (); ++dim_i)
147  mean_feature_[dim_i] /= normalization_factor;
148 }
149 
150 
151 //////////////////////////////////////////////////////////////////////////////////////////////
152 template <typename PointSource, typename PointFeature> void
154 {
155  unique_features_indices_.resize (scale_values_.size ());
156  unique_features_table_.resize (scale_values_.size ());
157  for (size_t scale_i = 0; scale_i < features_at_scale_vectorized_.size (); ++scale_i)
158  {
159  // Calculate standard deviation within the scale
160  float standard_dev = 0.0;
161  std::vector<float> diff_vector (features_at_scale_vectorized_[scale_i].size ());
162  for (size_t point_i = 0; point_i < features_at_scale_vectorized_[scale_i].size (); ++point_i)
163  {
164  float diff = distanceBetweenFeatures (features_at_scale_vectorized_[scale_i][point_i], mean_feature_);
165  standard_dev += diff * diff;
166  diff_vector[point_i] = diff;
167  }
168  standard_dev = std::sqrt (standard_dev / static_cast<float> (features_at_scale_vectorized_[scale_i].size ()));
169  PCL_DEBUG ("[pcl::MultiscaleFeaturePersistence::extractUniqueFeatures] Standard deviation for scale %f is %f\n", scale_values_[scale_i], standard_dev);
170 
171  // Select only points outside (mean +/- alpha * standard_dev)
172  std::list<size_t> indices_per_scale;
173  std::vector<bool> indices_table_per_scale (features_at_scale_[scale_i]->points.size (), false);
174  for (size_t point_i = 0; point_i < features_at_scale_[scale_i]->points.size (); ++point_i)
175  {
176  if (diff_vector[point_i] > alpha_ * standard_dev)
177  {
178  indices_per_scale.push_back (point_i);
179  indices_table_per_scale[point_i] = true;
180  }
181  }
182  unique_features_indices_[scale_i] = indices_per_scale;
183  unique_features_table_[scale_i] = indices_table_per_scale;
184  }
185 }
186 
187 
188 //////////////////////////////////////////////////////////////////////////////////////////////
189 template <typename PointSource, typename PointFeature> void
191  boost::shared_ptr<std::vector<int> > &output_indices)
192 {
193  if (!initCompute ())
194  return;
195 
196  // Compute the features for all scales with the given feature estimator
197  PCL_DEBUG ("[pcl::MultiscaleFeaturePersistence::determinePersistentFeatures] Computing features ...\n");
199 
200  // Compute mean feature
201  PCL_DEBUG ("[pcl::MultiscaleFeaturePersistence::determinePersistentFeatures] Calculating mean feature ...\n");
202  calculateMeanFeature ();
203 
204  // Get the 'unique' features at each scale
205  PCL_DEBUG ("[pcl::MultiscaleFeaturePersistence::determinePersistentFeatures] Extracting unique features ...\n");
206  extractUniqueFeatures ();
207 
208  PCL_DEBUG ("[pcl::MultiscaleFeaturePersistence::determinePersistentFeatures] Determining persistent features between scales ...\n");
209  // Determine persistent features between scales
210 
211 /*
212  // Method 1: a feature is considered persistent if it is 'unique' in at least 2 different scales
213  for (size_t scale_i = 0; scale_i < features_at_scale_vectorized_.size () - 1; ++scale_i)
214  for (std::list<size_t>::iterator feature_it = unique_features_indices_[scale_i].begin (); feature_it != unique_features_indices_[scale_i].end (); ++feature_it)
215  {
216  if (unique_features_table_[scale_i][*feature_it] == true)
217  {
218  output_features.points.push_back (features_at_scale[scale_i]->points[*feature_it]);
219  output_indices->push_back (feature_estimator_->getIndices ()->at (*feature_it));
220  }
221  }
222 */
223  // Method 2: a feature is considered persistent if it is 'unique' in all the scales
224  for (std::list<size_t>::iterator feature_it = unique_features_indices_.front ().begin (); feature_it != unique_features_indices_.front ().end (); ++feature_it)
225  {
226  bool present_in_all = true;
227  for (size_t scale_i = 0; scale_i < features_at_scale_.size (); ++scale_i)
228  present_in_all = present_in_all && unique_features_table_[scale_i][*feature_it];
229 
230  if (present_in_all)
231  {
232  output_features.points.push_back (features_at_scale_.front ()->points[*feature_it]);
233  output_indices->push_back (feature_estimator_->getIndices ()->at (*feature_it));
234  }
235  }
236 
237  // Consider that output cloud is unorganized
238  output_features.header = feature_estimator_->getInputCloud ()->header;
239  output_features.is_dense = feature_estimator_->getInputCloud ()->is_dense;
240  output_features.width = static_cast<uint32_t> (output_features.points.size ());
241  output_features.height = 1;
242 }
243 
244 
245 #define PCL_INSTANTIATE_MultiscaleFeaturePersistence(InT, Feature) template class PCL_EXPORTS pcl::MultiscaleFeaturePersistence<InT, Feature>;
246 
247 #endif /* PCL_FEATURES_IMPL_MULTISCALE_FEATURE_PERSISTENCE_H_ */
std::vector< PointT, Eigen::aligned_allocator< PointT > > points
The point data.
Definition: point_cloud.h:423
uint32_t height
The point cloud height (if organized as an image-structure).
Definition: point_cloud.h:428
void computeFeaturesAtAllScales()
Method that calls computeFeatureAtScale () for each scale parameter.
uint32_t width
The point cloud width (if organized as an image-structure).
Definition: point_cloud.h:426
Definition: norms.h:54
PCL base class.
Definition: pcl_base.h:69
pcl::PCLHeader header
The point cloud header.
Definition: point_cloud.h:420
Generic class for extracting the persistent features from an input point cloud It can be given any Fe...
float selectNorm(FloatVectorT a, FloatVectorT b, int dim, NormType norm_type)
Method that calculates any norm type available, based on the norm_type variable.
Definition: norms.hpp:49
DefaultPointRepresentation extends PointRepresentation to define default behavior for common point ty...
typename pcl::PointCloud< PointFeature >::Ptr FeatureCloudPtr
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:431
PointCloudConstPtr input_
The input point cloud dataset.
Definition: pcl_base.h:151
void determinePersistentFeatures(FeatureCloud &output_features, boost::shared_ptr< std::vector< int > > &output_indices)
Central function that computes the persistent features.