Point Cloud Library (PCL)  1.9.1-dev
cpc_segmentation.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 the copyright holder(s) 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_SEGMENTATION_IMPL_CPC_SEGMENTATION_HPP_
39 #define PCL_SEGMENTATION_IMPL_CPC_SEGMENTATION_HPP_
40 
41 #include <pcl/segmentation/cpc_segmentation.h>
42 
43 template <typename PointT>
45  max_cuts_ (20),
46  min_segment_size_for_cutting_ (400),
47  min_cut_score_ (0.16),
48  use_local_constrains_ (true),
49  use_directed_weights_ (true),
50  ransac_itrs_ (10000)
51 {
52 }
53 
54 template <typename PointT>
56 {
57 }
58 
59 template <typename PointT> void
61 {
62  if (supervoxels_set_)
63  {
64  // Calculate for every Edge if the connection is convex or invalid
65  // This effectively performs the segmentation.
67 
68  // Correct edge relations using extended convexity definition if k>0
70 
71  // Determine whether to use cutting planes
72  doGrouping ();
73 
74  grouping_data_valid_ = true;
75 
76  applyCuttingPlane (max_cuts_);
77 
78  // merge small segments
80  }
81  else
82  PCL_WARN ("[pcl::CPCSegmentation::segment] WARNING: Call function setInputSupervoxels first. Nothing has been done. \n");
83 }
84 
85 template <typename PointT> void
86 pcl::CPCSegmentation<PointT>::applyCuttingPlane (uint32_t depth_levels_left)
87 {
88  using SegLabel2ClusterMap = std::map<uint32_t, pcl::PointCloud<WeightSACPointType>::Ptr>;
89 
90  pcl::console::print_info ("Cutting at level %d (maximum %d)\n", max_cuts_ - depth_levels_left + 1, max_cuts_);
91  // stop if we reached the 0 level
92  if (depth_levels_left <= 0)
93  return;
94 
95  pcl::IndicesPtr support_indices (new pcl::Indices);
96  SegLabel2ClusterMap seg_to_edge_points_map;
97  std::map<uint32_t, std::vector<EdgeID> > seg_to_edgeIDs_map;
98  EdgeIterator edge_itr, edge_itr_end, next_edge;
99  boost::tie (edge_itr, edge_itr_end) = boost::edges (sv_adjacency_list_);
100  for (next_edge = edge_itr; edge_itr != edge_itr_end; edge_itr = next_edge)
101  {
102  next_edge++; // next_edge iterator is necessary, because removing an edge invalidates the iterator to the current edge
103  uint32_t source_sv_label = sv_adjacency_list_[boost::source (*edge_itr, sv_adjacency_list_)];
104  uint32_t target_sv_label = sv_adjacency_list_[boost::target (*edge_itr, sv_adjacency_list_)];
105 
106  uint32_t source_segment_label = sv_label_to_seg_label_map_[source_sv_label];
107  uint32_t target_segment_label = sv_label_to_seg_label_map_[target_sv_label];
108 
109  // do not process edges which already split two segments
110  if (source_segment_label != target_segment_label)
111  continue;
112 
113  // if edge has been used for cutting already do not use it again
114  if (sv_adjacency_list_[*edge_itr].used_for_cutting)
115  continue;
116  // get centroids of vertices
117  const pcl::PointXYZRGBA source_centroid = sv_label_to_supervoxel_map_[source_sv_label]->centroid_;
118  const pcl::PointXYZRGBA target_centroid = sv_label_to_supervoxel_map_[target_sv_label]->centroid_;
119 
120  // stores the information about the edge cloud (used for the weighted ransac)
121  // we use the normal to express the direction of the connection
122  // we use the intensity to express the normal differences between supervoxel patches. <=0: Convex, >0: Concave
123  WeightSACPointType edge_centroid;
124  edge_centroid.getVector3fMap () = (source_centroid.getVector3fMap () + target_centroid.getVector3fMap ()) / 2;
125 
126  // we use the normal to express the direction of the connection!
127  edge_centroid.getNormalVector3fMap () = (target_centroid.getVector3fMap () - source_centroid.getVector3fMap ()).normalized ();
128 
129  // we use the intensity to express the normal differences between supervoxel patches. <=0: Convex, >0: Concave
130  edge_centroid.intensity = sv_adjacency_list_[*edge_itr].is_convex ? -sv_adjacency_list_[*edge_itr].normal_difference : sv_adjacency_list_[*edge_itr].normal_difference;
131  if (seg_to_edge_points_map.find (source_segment_label) == seg_to_edge_points_map.end ())
132  {
133  seg_to_edge_points_map[source_segment_label] = pcl::PointCloud<WeightSACPointType>::Ptr (new pcl::PointCloud<WeightSACPointType> ());
134  }
135  seg_to_edge_points_map[source_segment_label]->push_back (edge_centroid);
136  seg_to_edgeIDs_map[source_segment_label].push_back (*edge_itr);
137  }
138  bool cut_found = false;
139  // do the following processing for each segment separately
140  for (const auto &seg_to_edge_points : seg_to_edge_points_map)
141  {
142  // if too small do not process
143  if (seg_to_edge_points.second->size () < min_segment_size_for_cutting_)
144  {
145  continue;
146  }
147 
148  std::vector<double> weights;
149  weights.resize (seg_to_edge_points.second->size ());
150  for (std::size_t cp = 0; cp < seg_to_edge_points.second->size (); ++cp)
151  {
152  float& cur_weight = seg_to_edge_points.second->points[cp].intensity;
153  cur_weight = cur_weight < concavity_tolerance_threshold_ ? 0 : 1;
154  weights[cp] = cur_weight;
155  }
156 
157  pcl::PointCloud<WeightSACPointType>::Ptr edge_cloud_cluster = seg_to_edge_points.second;
159 
160  WeightedRandomSampleConsensus weight_sac (model_p, seed_resolution_, true);
161 
162  weight_sac.setWeights (weights, use_directed_weights_);
163  weight_sac.setMaxIterations (ransac_itrs_);
164 
165  // if not enough inliers are found
166  if (!weight_sac.computeModel ())
167  {
168  continue;
169  }
170 
171  Eigen::VectorXf model_coefficients;
172  weight_sac.getModelCoefficients (model_coefficients);
173 
174  model_coefficients[3] += std::numeric_limits<float>::epsilon ();
175 
176  weight_sac.getInliers (*support_indices);
177 
178  // the support_indices which are actually cut (if not locally constrain: cut_support_indices = support_indices
179  pcl::Indices cut_support_indices;
180 
181  if (use_local_constrains_)
182  {
183  Eigen::Vector3f plane_normal (model_coefficients[0], model_coefficients[1], model_coefficients[2]);
184  // Cut the connections.
185  // We only iterate through the points which are within the support (when we are local, otherwise all points in the segment).
186  // We also just actually cut when the edge goes through the plane. This is why we check the planedistance
187  std::vector<pcl::PointIndices> cluster_indices;
190  tree->setInputCloud (edge_cloud_cluster);
191  euclidean_clusterer.setClusterTolerance (seed_resolution_);
192  euclidean_clusterer.setMinClusterSize (1);
193  euclidean_clusterer.setMaxClusterSize (25000);
194  euclidean_clusterer.setSearchMethod (tree);
195  euclidean_clusterer.setInputCloud (edge_cloud_cluster);
196  euclidean_clusterer.setIndices (support_indices);
197  euclidean_clusterer.extract (cluster_indices);
198 // sv_adjacency_list_[seg_to_edgeID_map[seg_to_edge_points.first][point_index]].used_for_cutting = true;
199 
200  for (const auto &cluster_index : cluster_indices)
201  {
202  // get centroids of vertices
203  int cluster_concave_pts = 0;
204  float cluster_score = 0;
205 // std::cout << "Cluster has " << cluster_indices[cc].indices.size () << " points" << std::endl;
206  for (const int &current_index : cluster_index.indices)
207  {
208  double index_score = weights[current_index];
209  if (use_directed_weights_)
210  index_score *= 1.414 * (fabsf (plane_normal.dot (edge_cloud_cluster->at (current_index).getNormalVector3fMap ())));
211  cluster_score += index_score;
212  if (weights[current_index] > 0)
213  ++cluster_concave_pts;
214  }
215  // check if the score is below the threshold. If that is the case this segment should not be split
216  cluster_score /= cluster_index.indices.size ();
217 // std::cout << "Cluster score: " << cluster_score << std::endl;
218  if (cluster_score >= min_cut_score_)
219  {
220  cut_support_indices.insert (cut_support_indices.end (), cluster_index.indices.begin (), cluster_index.indices.end ());
221  }
222  }
223  if (cut_support_indices.empty ())
224  {
225 // std::cout << "Could not find planes which exceed required minimum score (threshold " << min_cut_score_ << "), not cutting" << std::endl;
226  continue;
227  }
228  }
229  else
230  {
231  double current_score = weight_sac.getBestScore ();
232  cut_support_indices = *support_indices;
233  // check if the score is below the threshold. If that is the case this segment should not be split
234  if (current_score < min_cut_score_)
235  {
236 // std::cout << "Score too low, no cutting" << std::endl;
237  continue;
238  }
239  }
240 
241  int number_connections_cut = 0;
242  for (const int &point_index : cut_support_indices)
243  {
244  if (use_clean_cutting_)
245  {
246  // skip edges where both centroids are on one side of the cutting plane
247  uint32_t source_sv_label = sv_adjacency_list_[boost::source (seg_to_edgeIDs_map[seg_to_edge_points.first][point_index], sv_adjacency_list_)];
248  uint32_t target_sv_label = sv_adjacency_list_[boost::target (seg_to_edgeIDs_map[seg_to_edge_points.first][point_index], sv_adjacency_list_)];
249  // get centroids of vertices
250  const pcl::PointXYZRGBA source_centroid = sv_label_to_supervoxel_map_[source_sv_label]->centroid_;
251  const pcl::PointXYZRGBA target_centroid = sv_label_to_supervoxel_map_[target_sv_label]->centroid_;
252  // this makes a clean cut
253  if (pcl::pointToPlaneDistanceSigned (source_centroid, model_coefficients) * pcl::pointToPlaneDistanceSigned (target_centroid, model_coefficients) > 0)
254  {
255  continue;
256  }
257  }
258  sv_adjacency_list_[seg_to_edgeIDs_map[seg_to_edge_points.first][point_index]].used_for_cutting = true;
259  if (sv_adjacency_list_[seg_to_edgeIDs_map[seg_to_edge_points.first][point_index]].is_valid)
260  {
261  ++number_connections_cut;
262  sv_adjacency_list_[seg_to_edgeIDs_map[seg_to_edge_points.first][point_index]].is_valid = false;
263  }
264  }
265 // std::cout << "We cut " << number_connections_cut << " connections" << std::endl;
266  if (number_connections_cut > 0)
267  cut_found = true;
268  }
269 
270  // if not cut has been performed we can stop the recursion
271  if (cut_found)
272  {
273  doGrouping ();
274  --depth_levels_left;
275  applyCuttingPlane (depth_levels_left);
276  }
277  else
278  pcl::console::print_info ("Could not find any more cuts, stopping recursion\n");
279 }
280 
281 /******************************************* Directional weighted RANSAC definitions ******************************************************************/
282 
283 
284 template <typename PointT> bool
286 {
287  // Warn and exit if no threshold was set
288  if (threshold_ == std::numeric_limits<double>::max ())
289  {
290  PCL_ERROR ("[pcl::CPCSegmentation<PointT>::WeightedRandomSampleConsensus::computeModel] No threshold set!\n");
291  return (false);
292  }
293 
294  iterations_ = 0;
295  best_score_ = -std::numeric_limits<double>::max ();
296 
297  std::vector<int> selection;
298  Eigen::VectorXf model_coefficients;
299 
300  unsigned skipped_count = 0;
301  // suppress infinite loops by just allowing 10 x maximum allowed iterations for invalid model parameters!
302  const unsigned max_skip = max_iterations_ * 10;
303 
304  // Iterate
305  while (iterations_ < max_iterations_ && skipped_count < max_skip)
306  {
307  // Get X samples which satisfy the model criteria and which have a weight > 0
308  sac_model_->setIndices (model_pt_indices_);
309  sac_model_->getSamples (iterations_, selection);
310 
311  if (selection.empty ())
312  {
313  PCL_ERROR ("[pcl::CPCSegmentation<PointT>::WeightedRandomSampleConsensus::computeModel] No samples could be selected!\n");
314  break;
315  }
316 
317  // Search for inliers in the point cloud for the current plane model M
318  if (!sac_model_->computeModelCoefficients (selection, model_coefficients))
319  {
320  //++iterations_;
321  ++skipped_count;
322  continue;
323  }
324  // weight distances to get the score (only using connected inliers)
325  sac_model_->setIndices (full_cloud_pt_indices_);
326 
327  pcl::IndicesPtr current_inliers (new pcl::Indices);
328  sac_model_->selectWithinDistance (model_coefficients, threshold_, *current_inliers);
329  double current_score = 0;
330  Eigen::Vector3f plane_normal (model_coefficients[0], model_coefficients[1], model_coefficients[2]);
331  for (const int &current_index : *current_inliers)
332  {
333  double index_score = weights_[current_index];
334  if (use_directed_weights_)
335  // the sqrt(2) factor was used in the paper and was meant for making the scores better comparable between directed and undirected weights
336  index_score *= 1.414 * (fabsf (plane_normal.dot (point_cloud_ptr_->at (current_index).getNormalVector3fMap ())));
337 
338  current_score += index_score;
339  }
340  // normalize by the total number of inliers
341  current_score /= current_inliers->size ();
342 
343  // Better match ?
344  if (current_score > best_score_)
345  {
346  best_score_ = current_score;
347  // Save the current model/inlier/coefficients selection as being the best so far
348  model_ = selection;
349  model_coefficients_ = model_coefficients;
350  }
351 
352  ++iterations_;
353  PCL_DEBUG ("[pcl::CPCSegmentation<PointT>::WeightedRandomSampleConsensus::computeModel] Trial %d (max %d): score is %f (best is: %f so far).\n", iterations_, max_iterations_, current_score, best_score_);
354  if (iterations_ > max_iterations_)
355  {
356  PCL_DEBUG ("[pcl::CPCSegmentation<PointT>::WeightedRandomSampleConsensus::computeModel] RANSAC reached the maximum number of trials.\n");
357  break;
358  }
359  }
360 // std::cout << "Took us " << iterations_ - 1 << " iterations" << std::endl;
361  PCL_DEBUG ("[pcl::CPCSegmentation<PointT>::WeightedRandomSampleConsensus::computeModel] Model: %lu size, %f score.\n", model_.size (), best_score_);
362 
363  if (model_.empty ())
364  {
365  inliers_.clear ();
366  return (false);
367  }
368 
369  // Get the set of inliers that correspond to the best model found so far
370  sac_model_->selectWithinDistance (model_coefficients_, threshold_, inliers_);
371  return (true);
372 }
373 
374 #endif // PCL_SEGMENTATION_IMPL_CPC_SEGMENTATION_HPP_
double pointToPlaneDistanceSigned(const Point &p, double a, double b, double c, double d)
Get the distance from a point to a plane (signed) defined by ax+by+cz+d=0.
search::KdTree is a wrapper class which inherits the pcl::KdTree class for performing search function...
Definition: kdtree.h:61
void doGrouping()
Perform depth search on the graph and recursively group all supervoxels with convex connections...
void segment()
Merge supervoxels using cuts through local convexities.
PCL_EXPORTS void print_info(const char *format,...)
Print an info message on stream with colors.
std::vector< int > Indices
Definition: pcl_base.h:60
boost::shared_ptr< SampleConsensusModelPlane< PointT > > Ptr
A segmentation algorithm partitioning a supervoxel graph.
void setClusterTolerance(double tolerance)
Set the spatial cluster tolerance as a measure in the L2 Euclidean space.
A point structure representing Euclidean xyz coordinates, and the RGBA color.
uint32_t k_factor_
Factor used for k-convexity.
std::map< uint32_t, typename pcl::Supervoxel< PointT >::Ptr > sv_label_to_supervoxel_map_
map from the supervoxel labels to the supervoxel objects
void setMaxClusterSize(int max_cluster_size)
Set the maximum number of points that a cluster needs to contain in order to be considered valid...
float seed_resolution_
Seed resolution of the supervoxels (used only for smoothness check)
boost::shared_ptr< Indices > IndicesPtr
Definition: pcl_base.h:61
void setInputCloud(const PointCloudConstPtr &cloud, const IndicesConstPtr &indices=IndicesConstPtr()) override
Provide a pointer to the input dataset.
Definition: kdtree.hpp:77
A point structure representing Euclidean xyz coordinates, intensity, together with normal coordinates...
boost::shared_ptr< KdTree< PointT, Tree > > Ptr
Definition: kdtree.h:78
void extract(std::vector< PointIndices > &clusters)
Cluster extraction in a PointCloud given by <setInputCloud (), setIndices ()>
boost::shared_ptr< PointCloud< PointT > > Ptr
Definition: point_cloud.h:429
bool supervoxels_set_
Marks if supervoxels have been set by calling setInputSupervoxels.
void mergeSmallSegments()
Segments smaller than min_segment_size_ are merged to the label of largest neighbor.
PointCloud represents the base class in PCL for storing collections of 3D points. ...
virtual void setIndices(const IndicesPtr &indices)
Provide a pointer to the vector of indices that represents the input data.
Definition: pcl_base.hpp:72
void setMinClusterSize(int min_cluster_size)
Set the minimum number of points that a cluster needs to contain in order to be considered valid...
virtual void setInputCloud(const PointCloudConstPtr &cloud)
Provide a pointer to the input dataset.
Definition: pcl_base.hpp:65
std::map< uint32_t, uint32_t > sv_label_to_seg_label_map_
Storing relation between original SuperVoxel Labels and new segmantion labels.
EuclideanClusterExtraction represents a segmentation class for cluster extraction in an Euclidean sen...
float concavity_tolerance_threshold_
*** Parameters *** ///
void calculateConvexConnections(SupervoxelAdjacencyList &adjacency_list_arg)
Calculates convexity of edges and saves this to the adjacency graph.
void applyKconvexity(const unsigned int k_arg)
Connections are only convex if this is true for at least k_arg common neighbors of the two patches...
const PointT & at(int column, int row) const
Obtain the point given by the (column, row) coordinates.
Definition: point_cloud.h:284
SampleConsensusModelPlane defines a model for 3D plane segmentation.
bool grouping_data_valid_
Marks if valid grouping data (sv_adjacency_list_, sv_label_to_seg_label_map_, processed_) is availabl...
SupervoxelAdjacencyList sv_adjacency_list_
Adjacency graph with the supervoxel labels as nodes and edges between adjacent supervoxels.
void setSearchMethod(const KdTreePtr &tree)
Provide a pointer to the search object.