Point Cloud Library (PCL)  1.11.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 typename search::Search<PointT>::Ptr &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 ()->size () != cloud.size ())
53  {
54  PCL_ERROR("[pcl::extractLabeledEuclideanClusters] Tree built for a different point "
55  "cloud dataset (%zu) than the input cloud (%zu)!\n",
56  static_cast<std::size_t>(tree->getInputCloud()->size()),
57  static_cast<std::size_t>(cloud.size()));
58  return;
59  }
60  // Create a bool vector of processed point indices, and initialize it to false
61  std::vector<bool> processed (cloud.size (), false);
62 
63  Indices nn_indices;
64  std::vector<float> nn_distances;
65 
66  // Process all points in the indices vector
67  for (index_t i = 0; i < static_cast<index_t> (cloud.size ()); ++i)
68  {
69  if (processed[i])
70  continue;
71 
72  Indices seed_queue;
73  int sq_idx = 0;
74  seed_queue.push_back (i);
75 
76  processed[i] = true;
77 
78  while (sq_idx < static_cast<int> (seed_queue.size ()))
79  {
80  // Search for sq_idx
81  int ret = tree->radiusSearch (seed_queue[sq_idx], tolerance, nn_indices, nn_distances, std::numeric_limits<int>::max());
82  if(ret == -1)
83  PCL_ERROR("radiusSearch on tree came back with error -1");
84  if (!ret)
85  {
86  sq_idx++;
87  continue;
88  }
89 
90  for (std::size_t j = 1; j < nn_indices.size (); ++j) // nn_indices[0] should be sq_idx
91  {
92  if (processed[nn_indices[j]]) // Has this point been processed before ?
93  continue;
94  if (cloud[i].label == cloud[nn_indices[j]].label)
95  {
96  // Perform a simple Euclidean clustering
97  seed_queue.push_back (nn_indices[j]);
98  processed[nn_indices[j]] = true;
99  }
100  }
101 
102  sq_idx++;
103  }
104 
105  // If this queue is satisfactory, add to the clusters
106  if (seed_queue.size () >= min_pts_per_cluster && seed_queue.size () <= max_pts_per_cluster)
107  {
109  r.indices.resize (seed_queue.size ());
110  for (std::size_t j = 0; j < seed_queue.size (); ++j)
111  r.indices[j] = seed_queue[j];
112 
113  std::sort (r.indices.begin (), r.indices.end ());
114  r.indices.erase (std::unique (r.indices.begin (), r.indices.end ()), r.indices.end ());
115 
116  r.header = cloud.header;
117  labeled_clusters[cloud[i].label].push_back (r); // We could avoid a copy by working directly in the vector
118  }
119  }
120 }
121 //////////////////////////////////////////////////////////////////////////////////////////////
122 //////////////////////////////////////////////////////////////////////////////////////////////
123 //////////////////////////////////////////////////////////////////////////////////////////////
124 
125 template <typename PointT> void
126 pcl::LabeledEuclideanClusterExtraction<PointT>::extract (std::vector<std::vector<PointIndices> > &labeled_clusters)
127 {
128  if (!initCompute () ||
129  (input_ && input_->points.empty ()) ||
130  (indices_ && indices_->empty ()))
131  {
132  labeled_clusters.clear ();
133  return;
134  }
135 
136  // Initialize the spatial locator
137  if (!tree_)
138  {
139  if (input_->isOrganized ())
140  tree_.reset (new pcl::search::OrganizedNeighbor<PointT> ());
141  else
142  tree_.reset (new pcl::search::KdTree<PointT> (false));
143  }
144 
145  // Send the input dataset to the spatial locator
146  tree_->setInputCloud (input_);
147  extractLabeledEuclideanClusters (*input_, tree_, static_cast<float> (cluster_tolerance_), labeled_clusters, min_pts_per_cluster_, max_pts_per_cluster_, max_label_);
148 
149  // Sort the clusters based on their size (largest one first)
150  for (auto &labeled_cluster : labeled_clusters)
151  std::sort (labeled_cluster.rbegin (), labeled_cluster.rend (), comparePointClusters);
152 
153  deinitCompute ();
154 }
155 
156 #define PCL_INSTANTIATE_LabeledEuclideanClusterExtraction(T) template class PCL_EXPORTS pcl::LabeledEuclideanClusterExtraction<T>;
157 #define PCL_INSTANTIATE_extractLabeledEuclideanClusters(T) template void PCL_EXPORTS pcl::extractLabeledEuclideanClusters<T>(const pcl::PointCloud<T> &, const typename pcl::search::Search<T>::Ptr &, float , std::vector<std::vector<pcl::PointIndices> > &, unsigned int, unsigned int, unsigned int);
158 
159 #endif // PCL_EXTRACT_CLUSTERS_IMPL_H_
pcl::search::Search::getInputCloud
virtual PointCloudConstPtr getInputCloud() const
Get a pointer to the input point cloud dataset.
Definition: search.h:125
pcl::PointIndices::indices
Indices indices
Definition: PointIndices.h:21
pcl::search::Search::radiusSearch
virtual int radiusSearch(const PointT &point, double radius, Indices &k_indices, std::vector< float > &k_sqr_distances, unsigned int max_nn=0) const =0
Search for all the nearest neighbors of the query point in a given radius.
pcl::LabeledEuclideanClusterExtraction::extract
void extract(std::vector< std::vector< PointIndices > > &labeled_clusters)
Cluster extraction in a PointCloud given by <setInputCloud (), setIndices ()>
Definition: extract_labeled_clusters.hpp:126
pcl::PointIndices::header
::pcl::PCLHeader header
Definition: PointIndices.h:19
pcl::PointCloud
PointCloud represents the base class in PCL for storing collections of 3D points.
Definition: distances.h:55
pcl::index_t
detail::int_type_t< detail::index_type_size, detail::index_type_signed > index_t
Type used for an index in PCL.
Definition: types.h:110
pcl::gpu::comparePointClusters
bool comparePointClusters(const pcl::PointIndices &a, const pcl::PointIndices &b)
Sort clusters method (for std::sort).
Definition: gpu_extract_clusters.h:161
pcl::search::KdTree< PointT >
pcl::search::Search::Ptr
shared_ptr< pcl::search::Search< PointT > > Ptr
Definition: search.h:81
pcl::PointIndices
Definition: PointIndices.h:11
pcl::PointCloud::header
pcl::PCLHeader header
The point cloud header.
Definition: point_cloud.h:385
pcl::Indices
IndicesAllocator<> Indices
Type used for indices in PCL.
Definition: types.h:131
pcl::search::OrganizedNeighbor
OrganizedNeighbor is a class for optimized nearest neigbhor search in organized point clouds.
Definition: organized.h:62
pcl::PointCloud::size
std::size_t size() const
Definition: point_cloud.h:436
pcl::extractLabeledEuclideanClusters
void extractLabeledEuclideanClusters(const PointCloud< PointT > &cloud, const typename search::Search< PointT >::Ptr &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.
Definition: extract_labeled_clusters.hpp:44