Point Cloud Library (PCL)  1.14.0-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 \note the tree has to be created as a spatial locator on \a cloud
48  * \param[in] tolerance the spatial cluster tolerance as a measure in L2 Euclidean space
49  * \param[out] labeled_clusters the resultant clusters containing point indices
50  * (as a vector of PointIndices)
51  * \param[in] min_pts_per_cluster minimum number of points that a cluster may contain
52  * (default: 1)
53  * \param[in] max_pts_per_cluster maximum number of points that a cluster may contain
54  * (default: max int)
55  * \ingroup segmentation
56  */
57 template <typename PointT>
58 void
60  const PointCloud<PointT>& cloud,
61  const typename search::Search<PointT>::Ptr& tree,
62  float tolerance,
63  std::vector<std::vector<PointIndices>>& labeled_clusters,
64  unsigned int min_pts_per_cluster = 1,
65  unsigned int max_pts_per_cluster = std::numeric_limits<unsigned int>::max());
66 
67 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
68 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
69 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
70 /** \brief @b LabeledEuclideanClusterExtraction represents a segmentation class for
71  * cluster extraction in an Euclidean sense, with label info. \author Koen Buys
72  * \ingroup segmentation
73  */
74 template <typename PointT>
77 
78 public:
80  using PointCloudPtr = typename PointCloud::Ptr;
82 
84  using KdTreePtr = typename KdTree::Ptr;
85 
88 
89  //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
90  /** \brief Empty constructor. */
92 
93  /** \brief Provide a pointer to the search object.
94  * \param[in] tree a pointer to the spatial search object.
95  */
96  inline void
98  {
99  tree_ = tree;
100  }
101 
102  /** \brief Get a pointer to the search method used. */
103  inline KdTreePtr
105  {
106  return (tree_);
107  }
108 
109  /** \brief Set the spatial cluster tolerance as a measure in the L2 Euclidean space
110  * \param[in] tolerance the spatial cluster tolerance as a measure in the L2 Euclidean
111  * space
112  */
113  inline void
114  setClusterTolerance(double tolerance)
115  {
116  cluster_tolerance_ = tolerance;
117  }
118 
119  /** \brief Get the spatial cluster tolerance as a measure in the L2 Euclidean space.
120  */
121  inline double
123  {
124  return (cluster_tolerance_);
125  }
126 
127  /** \brief Set the minimum number of points that a cluster needs to contain in order
128  * to be considered valid. \param[in] min_cluster_size the minimum cluster size
129  */
130  inline void
131  setMinClusterSize(int min_cluster_size)
132  {
133  min_pts_per_cluster_ = min_cluster_size;
134  }
135 
136  /** \brief Get the minimum number of points that a cluster needs to contain in order
137  * to be considered valid. */
138  inline int
140  {
141  return (min_pts_per_cluster_);
142  }
143 
144  /** \brief Set the maximum number of points that a cluster needs to contain in order
145  * to be considered valid. \param[in] max_cluster_size the maximum cluster size
146  */
147  inline void
148  setMaxClusterSize(int max_cluster_size)
149  {
150  max_pts_per_cluster_ = max_cluster_size;
151  }
152 
153  /** \brief Get the maximum number of points that a cluster needs to contain in order
154  * to be considered valid. */
155  inline int
157  {
158  return (max_pts_per_cluster_);
159  }
160 
161  /** \brief Cluster extraction in a PointCloud given by <setInputCloud (), setIndices
162  * ()> \param[out] labeled_clusters the resultant point clusters
163  */
164  void
165  extract(std::vector<std::vector<PointIndices>>& labeled_clusters);
166 
167 protected:
168  // Members derived from the base class
170  using BasePCLBase::indices_;
172  using BasePCLBase::input_;
173 
174  /** \brief A pointer to the spatial search object. */
175  KdTreePtr tree_{nullptr};
176 
177  /** \brief The spatial cluster tolerance as a measure in the L2 Euclidean space. */
178  double cluster_tolerance_{0.0};
179 
180  /** \brief The minimum number of points that a cluster needs to contain in order to be
181  * considered valid (default = 1). */
183 
184  /** \brief The maximum number of points that a cluster needs to contain in order to be
185  * considered valid (default = MAXINT). */
186  int max_pts_per_cluster_{std::numeric_limits<int>::max()};
187 
188  /** \brief The maximum number of labels we can find in this pointcloud (default =
189  * MAXINT)*/
190  unsigned int max_label_{std::numeric_limits<int>::max()};
191 
192  /** \brief Class getName method. */
193  virtual std::string
194  getClassName() const
195  {
196  return ("LabeledEuclideanClusterExtraction");
197  }
198 };
199 
200 /** \brief Sort clusters method (for std::sort).
201  * \ingroup segmentation
202  */
203 inline bool
205 {
206  return (a.indices.size() < b.indices.size());
207 }
208 } // namespace pcl
209 
210 #ifdef PCL_NO_PRECOMPILE
211 #include <pcl/segmentation/impl/extract_labeled_clusters.hpp>
212 #endif
LabeledEuclideanClusterExtraction represents a segmentation class for cluster extraction in an Euclid...
unsigned int max_label_
The maximum number of labels we can find in this pointcloud (default = MAXINT)
void setClusterTolerance(double tolerance)
Set the spatial cluster tolerance as a measure in the L2 Euclidean space.
KdTreePtr tree_
A pointer to the spatial search object.
double cluster_tolerance_
The spatial cluster tolerance as a measure in the L2 Euclidean space.
void setMinClusterSize(int min_cluster_size)
Set the minimum number of points that a cluster needs to contain in order to be considered valid.
int getMaxClusterSize() const
Get the maximum number of points that a cluster needs to contain in order to be considered valid.
int min_pts_per_cluster_
The minimum number of points that a cluster needs to contain in order to be considered valid (default...
double getClusterTolerance() const
Get the spatial cluster tolerance as a measure in the L2 Euclidean space.
virtual std::string getClassName() const
Class getName method.
void extract(std::vector< std::vector< PointIndices >> &labeled_clusters)
Cluster extraction in a PointCloud given by <setInputCloud (), setIndices ()>
KdTreePtr getSearchMethod() const
Get a pointer to the search method used.
int getMinClusterSize() const
Get the minimum number of points that a cluster needs to contain in order to be considered valid.
int max_pts_per_cluster_
The maximum number of points that a cluster needs to contain in order to be considered valid (default...
void setMaxClusterSize(int max_cluster_size)
Set the maximum number of points that a cluster needs to contain in order to be considered valid.
LabeledEuclideanClusterExtraction()=default
Empty constructor.
void setSearchMethod(const KdTreePtr &tree)
Provide a pointer to the search object.
PCL base class.
Definition: pcl_base.h:70
PointCloudConstPtr input_
The input point cloud dataset.
Definition: pcl_base.h:147
typename PointCloud::Ptr PointCloudPtr
Definition: pcl_base.h:73
typename PointCloud::ConstPtr PointCloudConstPtr
Definition: pcl_base.h:74
IndicesPtr indices_
A pointer to the vector of point indices to use.
Definition: pcl_base.h:150
bool initCompute()
This method should get called before starting the actual computation.
Definition: pcl_base.hpp:138
PointIndices::ConstPtr PointIndicesConstPtr
Definition: pcl_base.h:77
bool deinitCompute()
This method should get called after finishing the actual computation.
Definition: pcl_base.hpp:175
PointIndices::Ptr PointIndicesPtr
Definition: pcl_base.h:76
PointCloud represents the base class in PCL for storing collections of 3D points.
Definition: point_cloud.h:173
shared_ptr< PointCloud< PointT > > Ptr
Definition: point_cloud.h:413
shared_ptr< const PointCloud< PointT > > ConstPtr
Definition: point_cloud.h:414
Generic search class.
Definition: search.h:75
shared_ptr< pcl::search::Search< PointT > > Ptr
Definition: search.h:81
bool compareLabeledPointClusters(const pcl::PointIndices &a, const pcl::PointIndices &b)
Sort clusters method (for std::sort).
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())
Decompose a region of space into clusters based on the Euclidean distance between points.
shared_ptr< ::pcl::PointIndices > Ptr
Definition: PointIndices.h:13
shared_ptr< const ::pcl::PointIndices > ConstPtr
Definition: PointIndices.h:14