Point Cloud Library (PCL)  1.8.1-dev
extract_labeled_clusters.hpp
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2011, Willow Garage, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of the copyright holder(s) nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *
34  * $id $
35  */
36 
37 #ifndef PCL_SEGMENTATION_IMPL_EXTRACT_LABELED_CLUSTERS_H_
38 #define PCL_SEGMENTATION_IMPL_EXTRACT_LABELED_CLUSTERS_H_
39 
40 #include <pcl/segmentation/extract_labeled_clusters.h>
41 
42 //////////////////////////////////////////////////////////////////////////////////////////////
43 template <typename PointT> void
45  const boost::shared_ptr<search::Search<PointT> > &tree,
46  float tolerance,
47  std::vector<std::vector<PointIndices> > &labeled_clusters,
48  unsigned int min_pts_per_cluster,
49  unsigned int max_pts_per_cluster,
50  unsigned int)
51 {
52  if (tree->getInputCloud ()->points.size () != cloud.points.size ())
53  {
54  PCL_ERROR ("[pcl::extractLabeledEuclideanClusters] Tree built for a different point cloud dataset (%lu) than the input cloud (%lu)!\n", tree->getInputCloud ()->points.size (), cloud.points.size ());
55  return;
56  }
57  // Create a bool vector of processed point indices, and initialize it to false
58  std::vector<bool> processed (cloud.points.size (), false);
59 
60  std::vector<int> nn_indices;
61  std::vector<float> nn_distances;
62 
63  // Process all points in the indices vector
64  for (int i = 0; i < static_cast<int> (cloud.points.size ()); ++i)
65  {
66  if (processed[i])
67  continue;
68 
69  std::vector<int> seed_queue;
70  int sq_idx = 0;
71  seed_queue.push_back (i);
72 
73  processed[i] = true;
74 
75  while (sq_idx < static_cast<int> (seed_queue.size ()))
76  {
77  // Search for sq_idx
78  int ret = tree->radiusSearch (seed_queue[sq_idx], tolerance, nn_indices, nn_distances, std::numeric_limits<int>::max());
79  if(ret == -1)
80  PCL_ERROR("radiusSearch on tree came back with error -1");
81  if (!ret)
82  {
83  sq_idx++;
84  continue;
85  }
86 
87  for (size_t j = 1; j < nn_indices.size (); ++j) // nn_indices[0] should be sq_idx
88  {
89  if (processed[nn_indices[j]]) // Has this point been processed before ?
90  continue;
91  if (cloud.points[i].label == cloud.points[nn_indices[j]].label)
92  {
93  // Perform a simple Euclidean clustering
94  seed_queue.push_back (nn_indices[j]);
95  processed[nn_indices[j]] = true;
96  }
97  }
98 
99  sq_idx++;
100  }
101 
102  // If this queue is satisfactory, add to the clusters
103  if (seed_queue.size () >= min_pts_per_cluster && seed_queue.size () <= max_pts_per_cluster)
104  {
106  r.indices.resize (seed_queue.size ());
107  for (size_t j = 0; j < seed_queue.size (); ++j)
108  r.indices[j] = seed_queue[j];
109 
110  std::sort (r.indices.begin (), r.indices.end ());
111  r.indices.erase (std::unique (r.indices.begin (), r.indices.end ()), r.indices.end ());
112 
113  r.header = cloud.header;
114  labeled_clusters[cloud.points[i].label].push_back (r); // We could avoid a copy by working directly in the vector
115  }
116  }
117 }
118 //////////////////////////////////////////////////////////////////////////////////////////////
119 //////////////////////////////////////////////////////////////////////////////////////////////
120 //////////////////////////////////////////////////////////////////////////////////////////////
121 
122 template <typename PointT> void
123 pcl::LabeledEuclideanClusterExtraction<PointT>::extract (std::vector<std::vector<PointIndices> > &labeled_clusters)
124 {
125  if (!initCompute () ||
126  (input_ != 0 && input_->points.empty ()) ||
127  (indices_ != 0 && indices_->empty ()))
128  {
129  labeled_clusters.clear ();
130  return;
131  }
132 
133  // Initialize the spatial locator
134  if (!tree_)
135  {
136  if (input_->isOrganized ())
137  tree_.reset (new pcl::search::OrganizedNeighbor<PointT> ());
138  else
139  tree_.reset (new pcl::search::KdTree<PointT> (false));
140  }
141 
142  // Send the input dataset to the spatial locator
143  tree_->setInputCloud (input_);
144  extractLabeledEuclideanClusters (*input_, tree_, static_cast<float> (cluster_tolerance_), labeled_clusters, min_pts_per_cluster_, max_pts_per_cluster_, max_label_);
145 
146  // Sort the clusters based on their size (largest one first)
147  for (int i = 0; i < static_cast<int> (labeled_clusters.size ()); i++)
148  std::sort (labeled_clusters[i].rbegin (), labeled_clusters[i].rend (), comparePointClusters);
149 
150  deinitCompute ();
151 }
152 
153 #define PCL_INSTANTIATE_LabeledEuclideanClusterExtraction(T) template class PCL_EXPORTS pcl::LabeledEuclideanClusterExtraction<T>;
154 #define PCL_INSTANTIATE_extractLabeledEuclideanClusters(T) template void PCL_EXPORTS pcl::extractLabeledEuclideanClusters<T>(const pcl::PointCloud<T> &, const boost::shared_ptr<pcl::search::Search<T> > &, float , std::vector<std::vector<pcl::PointIndices> > &, unsigned int, unsigned int, unsigned int);
155 
156 #endif // PCL_EXTRACT_CLUSTERS_IMPL_H_
std::vector< int > indices
Definition: PointIndices.h:19
pcl::PCLHeader header
The point cloud header.
Definition: point_cloud.h:407
std::vector< PointT, Eigen::aligned_allocator< PointT > > points
The point data.
Definition: point_cloud.h:410
bool comparePointClusters(const pcl::PointIndices &a, const pcl::PointIndices &b)
Sort clusters method (for std::sort).
::pcl::PCLHeader header
Definition: PointIndices.h:17
OrganizedNeighbor is a class for optimized nearest neigbhor search in organized point clouds...
Definition: organized.h:62
void extract(std::vector< std::vector< PointIndices > > &labeled_clusters)
Cluster extraction in a PointCloud given by <setInputCloud (), setIndices ()>
void extractLabeledEuclideanClusters(const PointCloud< PointT > &cloud, const boost::shared_ptr< search::Search< PointT > > &tree, float tolerance, std::vector< std::vector< PointIndices > > &labeled_clusters, unsigned int min_pts_per_cluster=1, unsigned int max_pts_per_cluster=std::numeric_limits< unsigned int >::max(), unsigned int max_label=std::numeric_limits< unsigned int >::max())
Decompose a region of space into clusters based on the Euclidean distance between points...
Generic search class.
Definition: search.h:74