Point Cloud Library (PCL)  1.11.1-dev
statistical_multiscale_interest_region_extraction.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_STATISTICAL_MULTISCALE_INTEREST_REGION_EXTRACTION_H_
41 #define PCL_FEATURES_IMPL_STATISTICAL_MULTISCALE_INTEREST_REGION_EXTRACTION_H_
42 
43 #include <pcl/features/statistical_multiscale_interest_region_extraction.h>
44 #include <pcl/kdtree/kdtree_flann.h>
45 #include <pcl/common/distances.h>
46 #include <pcl/console/print.h> // for PCL_INFO, PCL_ERROR
47 #include <pcl/features/boost.h>
48 #include <boost/graph/adjacency_list.hpp>
49 #include <boost/graph/johnson_all_pairs_shortest.hpp>
50 
51 
52 //////////////////////////////////////////////////////////////////////////////////////////////
53 template <typename PointT> void
55 {
56  // generate a K-NNG (K-nearest neighbors graph)
58  kdtree.setInputCloud (input_);
59 
60  using namespace boost;
61  using Weight = property<edge_weight_t, float>;
62  using Graph = adjacency_list<vecS, vecS, undirectedS, no_property, Weight>;
63  Graph cloud_graph;
64 
65  for (std::size_t point_i = 0; point_i < input_->size (); ++point_i)
66  {
67  pcl::Indices k_indices (16);
68  std::vector<float> k_distances (16);
69  kdtree.nearestKSearch (static_cast<int> (point_i), 16, k_indices, k_distances);
70 
71  for (std::size_t k_i = 0; k_i < k_indices.size (); ++k_i)
72  add_edge (point_i, k_indices[k_i], Weight (std::sqrt (k_distances[k_i])), cloud_graph);
73  }
74 
75  const std::size_t E = num_edges (cloud_graph),
76  V = num_vertices (cloud_graph);
77  PCL_INFO ("The graph has %lu vertices and %lu edges.\n", V, E);
78  geodesic_distances_.clear ();
79  for (std::size_t i = 0; i < V; ++i)
80  {
81  std::vector<float> aux (V);
82  geodesic_distances_.push_back (aux);
83  }
84  johnson_all_pairs_shortest_paths (cloud_graph, geodesic_distances_);
85 
86  PCL_INFO ("Done generating the graph\n");
87 }
88 
89 
90 //////////////////////////////////////////////////////////////////////////////////////////////
91 template <typename PointT> bool
93 {
95  {
96  PCL_ERROR ("[pcl::StatisticalMultiscaleInterestRegionExtraction::initCompute] PCLBase::initCompute () failed - no input cloud was given.\n");
97  return (false);
98  }
99  if (scale_values_.empty ())
100  {
101  PCL_ERROR ("[pcl::StatisticalMultiscaleInterestRegionExtraction::initCompute] No scale values were given\n");
102  return (false);
103  }
104 
105  return (true);
106 }
107 
108 
109 //////////////////////////////////////////////////////////////////////////////////////////////
110 template <typename PointT> void
112  float &radius,
113  std::vector<int> &result_indices)
114 {
115  for (std::size_t i = 0; i < geodesic_distances_[query_index].size (); ++i)
116  if (i != query_index && geodesic_distances_[query_index][i] < radius)
117  result_indices.push_back (static_cast<int> (i));
118 }
119 
120 
121 //////////////////////////////////////////////////////////////////////////////////////////////
122 template <typename PointT> void
124 {
125  if (!initCompute ())
126  {
127  PCL_ERROR ("StatisticalMultiscaleInterestRegionExtraction: not completely initialized\n");
128  return;
129  }
130 
131  generateCloudGraph ();
132 
133  computeF ();
134 
135  extractExtrema (rois);
136 }
137 
138 
139 //////////////////////////////////////////////////////////////////////////////////////////////
140 template <typename PointT> void
142 {
143  PCL_INFO ("Calculating statistical information\n");
144 
145  // declare and initialize data structure
146  F_scales_.resize (scale_values_.size ());
147  std::vector<float> point_density (input_->size ()),
148  F (input_->size ());
149  std::vector<std::vector<float> > phi (input_->size ());
150  std::vector<float> phi_row (input_->size ());
151 
152  for (std::size_t scale_i = 0; scale_i < scale_values_.size (); ++scale_i)
153  {
154  float scale_squared = scale_values_[scale_i] * scale_values_[scale_i];
155 
156  // calculate point density for each point x_i
157  for (std::size_t point_i = 0; point_i < input_->size (); ++point_i)
158  {
159  float point_density_i = 0.0;
160  for (std::size_t point_j = 0; point_j < input_->size (); ++point_j)
161  {
162  float d_g = geodesic_distances_[point_i][point_j];
163  float phi_i_j = 1.0f / std::sqrt (2.0f * static_cast<float> (M_PI) * scale_squared) * std::exp ( (-1) * d_g*d_g / (2.0f * scale_squared));
164 
165  point_density_i += phi_i_j;
166  phi_row[point_j] = phi_i_j;
167  }
168  point_density[point_i] = point_density_i;
169  phi[point_i] = phi_row;
170  }
171 
172  // compute weights for each pair (x_i, x_j), evaluate the operator A_hat
173  for (std::size_t point_i = 0; point_i < input_->size (); ++point_i)
174  {
175  float A_hat_normalization = 0.0;
176  PointT A_hat; A_hat.x = A_hat.y = A_hat.z = 0.0;
177  for (std::size_t point_j = 0; point_j < input_->size (); ++point_j)
178  {
179  float phi_hat_i_j = phi[point_i][point_j] / (point_density[point_i] * point_density[point_j]);
180  A_hat_normalization += phi_hat_i_j;
181 
182  PointT aux = (*input_)[point_j];
183  aux.x *= phi_hat_i_j; aux.y *= phi_hat_i_j; aux.z *= phi_hat_i_j;
184 
185  A_hat.x += aux.x; A_hat.y += aux.y; A_hat.z += aux.z;
186  }
187  A_hat.x /= A_hat_normalization; A_hat.y /= A_hat_normalization; A_hat.z /= A_hat_normalization;
188 
189  // compute the invariant F
190  float aux = 2.0f / scale_values_[scale_i] * euclideanDistance<PointT, PointT> (A_hat, (*input_)[point_i]);
191  F[point_i] = aux * std::exp (-aux);
192  }
193 
194  F_scales_[scale_i] = F;
195  }
196 }
197 
198 
199 //////////////////////////////////////////////////////////////////////////////////////////////
200 template <typename PointT> void
202 {
203  std::vector<std::vector<bool> > is_min (scale_values_.size ()),
204  is_max (scale_values_.size ());
205 
206  // for each point, check if it is a local extrema on each scale
207  for (std::size_t scale_i = 0; scale_i < scale_values_.size (); ++scale_i)
208  {
209  std::vector<bool> is_min_scale (input_->size ()),
210  is_max_scale (input_->size ());
211  for (std::size_t point_i = 0; point_i < input_->size (); ++point_i)
212  {
213  std::vector<int> nn_indices;
214  geodesicFixedRadiusSearch (point_i, scale_values_[scale_i], nn_indices);
215  bool is_max_point = true, is_min_point = true;
216  for (const auto &nn_index : nn_indices)
217  if (F_scales_[scale_i][point_i] < F_scales_[scale_i][nn_index])
218  is_max_point = false;
219  else
220  is_min_point = false;
221 
222  is_min_scale[point_i] = is_min_point;
223  is_max_scale[point_i] = is_max_point;
224  }
225 
226  is_min[scale_i] = is_min_scale;
227  is_max[scale_i] = is_max_scale;
228  }
229 
230  // look for points that are min/max over three consecutive scales
231  for (std::size_t scale_i = 1; scale_i < scale_values_.size () - 1; ++scale_i)
232  {
233  for (std::size_t point_i = 0; point_i < input_->size (); ++point_i)
234  if ((is_min[scale_i - 1][point_i] && is_min[scale_i][point_i] && is_min[scale_i + 1][point_i]) ||
235  (is_max[scale_i - 1][point_i] && is_max[scale_i][point_i] && is_max[scale_i + 1][point_i]))
236  {
237  // add the point to the result vector
238  IndicesPtr region (new std::vector<int>);
239  region->push_back (static_cast<int> (point_i));
240 
241  // and also add its scale-sized geodesic neighborhood
242  std::vector<int> nn_indices;
243  geodesicFixedRadiusSearch (point_i, scale_values_[scale_i], nn_indices);
244  region->insert (region->end (), nn_indices.begin (), nn_indices.end ());
245  rois.push_back (region);
246  }
247  }
248 }
249 
250 
251 #define PCL_INSTANTIATE_StatisticalMultiscaleInterestRegionExtraction(T) template class PCL_EXPORTS pcl::StatisticalMultiscaleInterestRegionExtraction<T>;
252 
253 #endif /* PCL_FEATURES_IMPL_STATISTICAL_MULTISCALE_INTEREST_REGION_EXTRACTION_H_ */
254 
pcl::IndicesPtr
shared_ptr< Indices > IndicesPtr
Definition: pcl_base.h:58
pcl::StatisticalMultiscaleInterestRegionExtraction
Class for extracting interest regions from unstructured point clouds, based on a multi scale statisti...
Definition: statistical_multiscale_interest_region_extraction.h:64
pcl::StatisticalMultiscaleInterestRegionExtraction::generateCloudGraph
void generateCloudGraph()
Method that generates the underlying nearest neighbor graph based on the input point cloud.
Definition: statistical_multiscale_interest_region_extraction.hpp:54
pcl::PCLBase
PCL base class.
Definition: pcl_base.h:69
boost
Definition: boost_graph.h:48
pcl::PointXYZRGB
A point structure representing Euclidean xyz coordinates, and the RGB color.
Definition: point_types.hpp:628
pcl::KdTreeFLANN::nearestKSearch
int nearestKSearch(const PointT &point, unsigned int k, std::vector< int > &k_indices, std::vector< float > &k_sqr_distances) const override
Search for k-nearest neighbors for the given query point.
Definition: kdtree_flann.hpp:132
pcl::KdTreeFLANN< PointT >
M_PI
#define M_PI
Definition: pcl_macros.h:201
pcl::StatisticalMultiscaleInterestRegionExtraction::computeRegionsOfInterest
void computeRegionsOfInterest(std::list< IndicesPtr > &rois)
The method to be called in order to run the algorithm and produce the resulting set of regions of int...
Definition: statistical_multiscale_interest_region_extraction.hpp:123
pcl::Indices
IndicesAllocator<> Indices
Type used for indices in PCL.
Definition: types.h:131
distances.h
pcl::KdTreeFLANN::setInputCloud
void setInputCloud(const PointCloudConstPtr &cloud, const IndicesConstPtr &indices=IndicesConstPtr()) override
Provide a pointer to the input dataset.
Definition: kdtree_flann.hpp:92