Point Cloud Library (PCL)  1.9.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
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 #ifndef PCL_EXTRACT_LABELED_CLUSTERS_H_
37 #define PCL_EXTRACT_LABELED_CLUSTERS_H_
38 
39 #include <pcl/pcl_base.h>
40 #include <pcl/search/pcl_search.h>
41 
42 namespace pcl
43 {
44  //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
45  /** \brief Decompose a region of space into clusters based on the Euclidean distance between points
46  * \param[in] cloud the point cloud message
47  * \param[in] tree the spatial locator (e.g., kd-tree) used for nearest neighbors 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 vector of PointIndices)
51  * \param[in] min_pts_per_cluster minimum number of points that a cluster may contain (default: 1)
52  * \param[in] max_pts_per_cluster maximum number of points that a cluster may contain (default: max int)
53  * \param[in] max_label
54  * \ingroup segmentation
55  */
56  template <typename PointT> void
58  const PointCloud<PointT> &cloud, const boost::shared_ptr<search::Search<PointT> > &tree,
59  float tolerance, std::vector<std::vector<PointIndices> > &labeled_clusters,
60  unsigned int min_pts_per_cluster = 1, unsigned int max_pts_per_cluster = std::numeric_limits<unsigned int>::max (),
61  unsigned int max_label = std::numeric_limits<unsigned int>::max ());
62 
63  //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
64  //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
65  //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
66  /** \brief @b LabeledEuclideanClusterExtraction represents a segmentation class for cluster extraction in an Euclidean sense, with label info.
67  * \author Koen Buys
68  * \ingroup segmentation
69  */
70  template <typename PointT>
72  {
74 
75  public:
77  typedef typename PointCloud::Ptr PointCloudPtr;
79 
82 
85 
86  //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
87  /** \brief Empty constructor. */
89  tree_ (),
92  max_pts_per_cluster_ (std::numeric_limits<int>::max ()),
93  max_label_ (std::numeric_limits<int>::max ())
94  {};
95 
96  /** \brief Provide a pointer to the search object.
97  * \param[in] tree a pointer to the spatial search object.
98  */
99  inline void
100  setSearchMethod (const KdTreePtr &tree) { tree_ = tree; }
101 
102  /** \brief Get a pointer to the search method used. */
103  inline KdTreePtr
104  getSearchMethod () const { return (tree_); }
105 
106  /** \brief Set the spatial cluster tolerance as a measure in the L2 Euclidean space
107  * \param[in] tolerance the spatial cluster tolerance as a measure in the L2 Euclidean space
108  */
109  inline void
110  setClusterTolerance (double tolerance) { cluster_tolerance_ = tolerance; }
111 
112  /** \brief Get the spatial cluster tolerance as a measure in the L2 Euclidean space. */
113  inline double
115 
116  /** \brief Set the minimum number of points that a cluster needs to contain in order to be considered valid.
117  * \param[in] min_cluster_size the minimum cluster size
118  */
119  inline void
120  setMinClusterSize (int min_cluster_size) { min_pts_per_cluster_ = min_cluster_size; }
121 
122  /** \brief Get the minimum number of points that a cluster needs to contain in order to be considered valid. */
123  inline int
125 
126  /** \brief Set the maximum number of points that a cluster needs to contain in order to be considered valid.
127  * \param[in] max_cluster_size the maximum cluster size
128  */
129  inline void
130  setMaxClusterSize (int max_cluster_size) { max_pts_per_cluster_ = max_cluster_size; }
131 
132  /** \brief Get the maximum number of points that a cluster needs to contain in order to be considered valid. */
133  inline int
135 
136  /** \brief Set the maximum number of labels in the cloud.
137  * \param[in] max_label the maximum
138  */
139  inline void
140  setMaxLabels (unsigned int max_label) { max_label_ = max_label; }
141 
142  /** \brief Get the maximum number of labels */
143  inline unsigned int
144  getMaxLabels () const { return (max_label_); }
145 
146  /** \brief Cluster extraction in a PointCloud given by <setInputCloud (), setIndices ()>
147  * \param[out] labeled_clusters the resultant point clusters
148  */
149  void
150  extract (std::vector<std::vector<PointIndices> > &labeled_clusters);
151 
152  protected:
153  // Members derived from the base class
154  using BasePCLBase::input_;
155  using BasePCLBase::indices_;
158 
159  /** \brief A pointer to the spatial search object. */
160  KdTreePtr tree_;
161 
162  /** \brief The spatial cluster tolerance as a measure in the L2 Euclidean space. */
164 
165  /** \brief The minimum number of points that a cluster needs to contain in order to be considered valid (default = 1). */
167 
168  /** \brief The maximum number of points that a cluster needs to contain in order to be considered valid (default = MAXINT). */
170 
171  /** \brief The maximum number of labels we can find in this pointcloud (default = MAXINT)*/
172  unsigned int max_label_;
173 
174  /** \brief Class getName method. */
175  virtual std::string getClassName () const { return ("LabeledEuclideanClusterExtraction"); }
176 
177  };
178 
179  /** \brief Sort clusters method (for std::sort).
180  * \ingroup segmentation
181  */
182  inline bool
184  {
185  return (a.indices.size () < b.indices.size ());
186  }
187 }
188 
189 #ifdef PCL_NO_PRECOMPILE
190 #include <pcl/segmentation/impl/extract_labeled_clusters.hpp>
191 #endif
192 
193 #endif //#ifndef PCL_EXTRACT_LABELED_CLUSTERS_H_
boost::shared_ptr< PointCloud< PointT > > Ptr
Definition: point_cloud.h:428
KdTreePtr getSearchMethod() const
Get a pointer to the search method used.
double cluster_tolerance_
The spatial cluster tolerance as a measure in the L2 Euclidean space.
bool compareLabeledPointClusters(const pcl::PointIndices &a, const pcl::PointIndices &b)
Sort clusters method (for std::sort).
double getClusterTolerance() const
Get the spatial cluster tolerance as a measure in the L2 Euclidean space.
This file defines compatibility wrappers for low level I/O functions.
Definition: convolution.h:45
IndicesPtr indices_
A pointer to the vector of point indices to use.
Definition: pcl_base.h:153
std::vector< int > indices
Definition: PointIndices.h:19
int min_pts_per_cluster_
The minimum number of points that a cluster needs to contain in order to be considered valid (default...
void setMinClusterSize(int min_cluster_size)
Set the minimum number of points that a cluster needs to contain in order to be considered valid...
pcl::search::Search< PointT >::Ptr KdTreePtr
LabeledEuclideanClusterExtraction represents a segmentation class for cluster extraction in an Euclid...
bool initCompute()
This method should get called before starting the actual computation.
Definition: pcl_base.hpp:139
unsigned int getMaxLabels() const
Get the maximum number of labels.
boost::shared_ptr< const PointCloud< PointT > > ConstPtr
Definition: point_cloud.h:429
KdTreePtr tree_
A pointer to the spatial search object.
void setSearchMethod(const KdTreePtr &tree)
Provide a pointer to the search object.
boost::shared_ptr< pcl::search::Search< PointT > > Ptr
Definition: search.h:81
PCL base class.
Definition: pcl_base.h:68
virtual std::string getClassName() const
Class getName method.
void setClusterTolerance(double tolerance)
Set the spatial cluster tolerance as a measure in the L2 Euclidean space.
bool deinitCompute()
This method should get called after finishing the actual computation.
Definition: pcl_base.hpp:174
int getMaxClusterSize() const
Get the maximum 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...
PointCloud represents the base class in PCL for storing collections of 3D points. ...
int getMinClusterSize() const
Get the minimum number of points that a cluster needs to contain in order to be considered valid...
boost::shared_ptr< ::pcl::PointIndices > Ptr
Definition: PointIndices.h:22
void setMaxClusterSize(int max_cluster_size)
Set the maximum number of points that a cluster needs to contain in order to be considered valid...
void extract(std::vector< std::vector< PointIndices > > &labeled_clusters)
Cluster extraction in a PointCloud given by
PointCloudConstPtr input_
The input point cloud dataset.
Definition: pcl_base.h:150
void setMaxLabels(unsigned int max_label)
Set the maximum number of labels in the cloud.
unsigned int max_label_
The maximum number of labels we can find in this pointcloud (default = MAXINT)
boost::shared_ptr< ::pcl::PointIndices const > ConstPtr
Definition: PointIndices.h:23
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