Point Cloud Library (PCL)  1.11.1-dev
extract_labeled_clusters.h
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  */
35 
36 #pragma once
37 
38 #include <pcl/search/search.h>
39 #include <pcl/pcl_base.h>
40 
41 namespace pcl {
42 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
43 /** \brief Decompose a region of space into clusters based on the Euclidean distance
44  * between points
45  * \param[in] cloud the point cloud message
46  * \param[in] tree the spatial locator (e.g., kd-tree) used for nearest neighbors
47  * searching
48  * \note the tree has to be created as a spatial locator on \a cloud
49  * \param[in] tolerance the spatial cluster tolerance as a measure in L2 Euclidean space
50  * \param[out] labeled_clusters the resultant clusters containing point indices (as a
51  * vector of PointIndices)
52  * \param[in] min_pts_per_cluster minimum number of points that a cluster may contain
53  * (default: 1)
54  * \param[in] max_pts_per_cluster maximum number of points that a cluster may contain
55  * (default: max int)
56  * \param[in] max_label
57  * \ingroup segmentation
58  */
59 template <typename PointT>
60 PCL_DEPRECATED(1, 14, "Use of max_label is deprecated")
62  const PointCloud<PointT>& cloud,
63  const typename search::Search<PointT>::Ptr& tree,
64  float tolerance,
65  std::vector<std::vector<PointIndices>>& labeled_clusters,
66  unsigned int min_pts_per_cluster,
67  unsigned int max_pts_per_cluster,
68  unsigned int max_label);
69 
70 /** \brief Decompose a region of space into clusters based on the Euclidean distance
71  * between points \param[in] cloud the point cloud message \param[in] tree the spatial
72  * locator (e.g., kd-tree) used for nearest neighbors searching \note the tree has to be
73  * created as a spatial locator on \a cloud \param[in] tolerance the spatial cluster
74  * tolerance as a measure in L2 Euclidean space \param[out] labeled_clusters the
75  * resultant clusters containing point indices (as a vector of PointIndices) \param[in]
76  * min_pts_per_cluster minimum number of points that a cluster may contain (default: 1)
77  * \param[in] max_pts_per_cluster maximum number of points that a cluster may contain
78  * (default: max int) \ingroup segmentation
79  */
80 template <typename PointT>
81 void
83  const PointCloud<PointT>& cloud,
84  const typename search::Search<PointT>::Ptr& tree,
85  float tolerance,
86  std::vector<std::vector<PointIndices>>& labeled_clusters,
87  unsigned int min_pts_per_cluster = 1,
88  unsigned int max_pts_per_cluster = std::numeric_limits<unsigned int>::max());
89 
90 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
91 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
92 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
93 /** \brief @b LabeledEuclideanClusterExtraction represents a segmentation class for
94  * cluster extraction in an Euclidean sense, with label info. \author Koen Buys \ingroup
95  * segmentation
96  */
97 template <typename PointT>
100 
101 public:
103  using PointCloudPtr = typename PointCloud::Ptr;
105 
107  using KdTreePtr = typename KdTree::Ptr;
108 
111 
112  //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
113  /** \brief Empty constructor. */
115  : tree_()
116  , cluster_tolerance_(0)
117  , min_pts_per_cluster_(1)
118  , max_pts_per_cluster_(std::numeric_limits<int>::max())
119  , max_label_(std::numeric_limits<int>::max()){};
120 
121  /** \brief Provide a pointer to the search object.
122  * \param[in] tree a pointer to the spatial search object.
123  */
124  inline void
126  {
127  tree_ = tree;
128  }
129 
130  /** \brief Get a pointer to the search method used. */
131  inline KdTreePtr
133  {
134  return (tree_);
135  }
136 
137  /** \brief Set the spatial cluster tolerance as a measure in the L2 Euclidean space
138  * \param[in] tolerance the spatial cluster tolerance as a measure in the L2 Euclidean
139  * space
140  */
141  inline void
142  setClusterTolerance(double tolerance)
143  {
144  cluster_tolerance_ = tolerance;
145  }
146 
147  /** \brief Get the spatial cluster tolerance as a measure in the L2 Euclidean space.
148  */
149  inline double
151  {
152  return (cluster_tolerance_);
153  }
154 
155  /** \brief Set the minimum number of points that a cluster needs to contain in order
156  * to be considered valid. \param[in] min_cluster_size the minimum cluster size
157  */
158  inline void
159  setMinClusterSize(int min_cluster_size)
160  {
161  min_pts_per_cluster_ = min_cluster_size;
162  }
163 
164  /** \brief Get the minimum number of points that a cluster needs to contain in order
165  * to be considered valid. */
166  inline int
168  {
169  return (min_pts_per_cluster_);
170  }
171 
172  /** \brief Set the maximum number of points that a cluster needs to contain in order
173  * to be considered valid. \param[in] max_cluster_size the maximum cluster size
174  */
175  inline void
176  setMaxClusterSize(int max_cluster_size)
177  {
178  max_pts_per_cluster_ = max_cluster_size;
179  }
180 
181  /** \brief Get the maximum number of points that a cluster needs to contain in order
182  * to be considered valid. */
183  inline int
185  {
186  return (max_pts_per_cluster_);
187  }
188 
189  /** \brief Set the maximum number of labels in the cloud.
190  * \param[in] max_label the maximum
191  */
192  PCL_DEPRECATED(1, 14, "Max label is being deprecated")
193  inline void
194  setMaxLabels(unsigned int max_label)
195  {
196  max_label_ = max_label;
197  }
198 
199  /** \brief Get the maximum number of labels */
200  PCL_DEPRECATED(1, 14, "Max label is being deprecated")
201  inline unsigned int
202  getMaxLabels() const
203  {
204  return (max_label_);
205  }
206 
207  /** \brief Cluster extraction in a PointCloud given by <setInputCloud (), setIndices
208  * ()> \param[out] labeled_clusters the resultant point clusters
209  */
210  void
211  extract(std::vector<std::vector<PointIndices>>& labeled_clusters);
212 
213 protected:
214  // Members derived from the base class
215  using BasePCLBase::deinitCompute;
216  using BasePCLBase::indices_;
217  using BasePCLBase::initCompute;
218  using BasePCLBase::input_;
219 
220  /** \brief A pointer to the spatial search object. */
222 
223  /** \brief The spatial cluster tolerance as a measure in the L2 Euclidean space. */
225 
226  /** \brief The minimum number of points that a cluster needs to contain in order to be
227  * considered valid (default = 1). */
229 
230  /** \brief The maximum number of points that a cluster needs to contain in order to be
231  * considered valid (default = MAXINT). */
233 
234  /** \brief The maximum number of labels we can find in this pointcloud (default =
235  * MAXINT)*/
236  unsigned int max_label_;
237 
238  /** \brief Class getName method. */
239  virtual std::string
240  getClassName() const
241  {
242  return ("LabeledEuclideanClusterExtraction");
243  }
244 };
245 
246 /** \brief Sort clusters method (for std::sort).
247  * \ingroup segmentation
248  */
249 inline bool
251 {
252  return (a.indices.size() < b.indices.size());
253 }
254 } // namespace pcl
255 
256 #ifdef PCL_NO_PRECOMPILE
257 #include <pcl/segmentation/impl/extract_labeled_clusters.hpp>
258 #endif
pcl::search::Search
Generic search class.
Definition: search.h:74
pcl::LabeledEuclideanClusterExtraction::tree_
KdTreePtr tree_
A pointer to the spatial search object.
Definition: extract_labeled_clusters.h:221
pcl
Definition: convolution.h:46
pcl::PCLBase::PointCloudConstPtr
typename PointCloud::ConstPtr PointCloudConstPtr
Definition: pcl_base.h:74
pcl::LabeledEuclideanClusterExtraction::getSearchMethod
KdTreePtr getSearchMethod() const
Get a pointer to the search method used.
Definition: extract_labeled_clusters.h:132
pcl::PCLBase::PointCloudPtr
typename PointCloud::Ptr PointCloudPtr
Definition: pcl_base.h:73
pcl::LabeledEuclideanClusterExtraction::max_label_
unsigned int max_label_
The maximum number of labels we can find in this pointcloud (default = MAXINT)
Definition: extract_labeled_clusters.h:236
pcl::LabeledEuclideanClusterExtraction::max_pts_per_cluster_
int max_pts_per_cluster_
The maximum number of points that a cluster needs to contain in order to be considered valid (default...
Definition: extract_labeled_clusters.h:232
pcl::PointIndices::indices
Indices indices
Definition: PointIndices.h:21
pcl::LabeledEuclideanClusterExtraction::LabeledEuclideanClusterExtraction
LabeledEuclideanClusterExtraction()
Empty constructor.
Definition: extract_labeled_clusters.h:114
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, unsigned int max_pts_per_cluster, unsigned int max_label)
Decompose a region of space into clusters based on the Euclidean distance between points.
Definition: extract_labeled_clusters.hpp:45
pcl::PCLBase
PCL base class.
Definition: pcl_base.h:69
pcl::PCLBase::PointIndicesConstPtr
PointIndices::ConstPtr PointIndicesConstPtr
Definition: pcl_base.h:77
pcl::PointCloud
PointCloud represents the base class in PCL for storing collections of 3D points.
Definition: distances.h:55
pcl::LabeledEuclideanClusterExtraction::setClusterTolerance
void setClusterTolerance(double tolerance)
Set the spatial cluster tolerance as a measure in the L2 Euclidean space.
Definition: extract_labeled_clusters.h:142
pcl::PointXYZRGB
A point structure representing Euclidean xyz coordinates, and the RGB color.
Definition: point_types.hpp:628
pcl::LabeledEuclideanClusterExtraction::cluster_tolerance_
double cluster_tolerance_
The spatial cluster tolerance as a measure in the L2 Euclidean space.
Definition: extract_labeled_clusters.h:224
PCL_DEPRECATED
#define PCL_DEPRECATED(Major, Minor, Message)
macro for compatibility across compilers and help remove old deprecated items for the Major....
Definition: pcl_macros.h:156
pcl::LabeledEuclideanClusterExtraction::getClassName
virtual std::string getClassName() const
Class getName method.
Definition: extract_labeled_clusters.h:240
pcl::PointIndices::ConstPtr
shared_ptr< const ::pcl::PointIndices > ConstPtr
Definition: PointIndices.h:14
pcl::LabeledEuclideanClusterExtraction::getClusterTolerance
double getClusterTolerance() const
Get the spatial cluster tolerance as a measure in the L2 Euclidean space.
Definition: extract_labeled_clusters.h:150
pcl::LabeledEuclideanClusterExtraction::min_pts_per_cluster_
int min_pts_per_cluster_
The minimum number of points that a cluster needs to contain in order to be considered valid (default...
Definition: extract_labeled_clusters.h:228
pcl::LabeledEuclideanClusterExtraction::getMinClusterSize
int getMinClusterSize() const
Get the minimum number of points that a cluster needs to contain in order to be considered valid.
Definition: extract_labeled_clusters.h:167
pcl::LabeledEuclideanClusterExtraction::setSearchMethod
void setSearchMethod(const KdTreePtr &tree)
Provide a pointer to the search object.
Definition: extract_labeled_clusters.h:125
pcl::LabeledEuclideanClusterExtraction::setMaxClusterSize
void setMaxClusterSize(int max_cluster_size)
Set the maximum number of points that a cluster needs to contain in order to be considered valid.
Definition: extract_labeled_clusters.h:176
pcl::LabeledEuclideanClusterExtraction::KdTreePtr
typename KdTree::Ptr KdTreePtr
Definition: extract_labeled_clusters.h:107
pcl::PointIndices
Definition: PointIndices.h:11
pcl::PointIndices::Ptr
shared_ptr< ::pcl::PointIndices > Ptr
Definition: PointIndices.h:13
pcl::compareLabeledPointClusters
bool compareLabeledPointClusters(const pcl::PointIndices &a, const pcl::PointIndices &b)
Sort clusters method (for std::sort).
Definition: extract_labeled_clusters.h:250
pcl::PointCloud::Ptr
shared_ptr< PointCloud< PointT > > Ptr
Definition: point_cloud.h:406
pcl::KdTree::Ptr
shared_ptr< KdTree< PointT > > Ptr
Definition: kdtree.h:68
pcl::LabeledEuclideanClusterExtraction::setMinClusterSize
void setMinClusterSize(int min_cluster_size)
Set the minimum number of points that a cluster needs to contain in order to be considered valid.
Definition: extract_labeled_clusters.h:159
pcl::PointCloud::ConstPtr
shared_ptr< const PointCloud< PointT > > ConstPtr
Definition: point_cloud.h:407
pcl::PCLBase::PointIndicesPtr
PointIndices::Ptr PointIndicesPtr
Definition: pcl_base.h:76
pcl::LabeledEuclideanClusterExtraction
LabeledEuclideanClusterExtraction represents a segmentation class for cluster extraction in an Euclid...
Definition: extract_labeled_clusters.h:98
pcl::LabeledEuclideanClusterExtraction::getMaxClusterSize
int getMaxClusterSize() const
Get the maximum number of points that a cluster needs to contain in order to be considered valid.
Definition: extract_labeled_clusters.h:184