Point Cloud Library (PCL)  1.9.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/pcl_base.h>
39 #include <pcl/search/pcl_search.h>
40 
41 namespace pcl
42 {
43  //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
44  /** \brief Decompose a region of space into clusters based on the Euclidean distance 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 searching
47  * \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 (as a vector of PointIndices)
50  * \param[in] min_pts_per_cluster minimum number of points that a cluster may contain (default: 1)
51  * \param[in] max_pts_per_cluster maximum number of points that a cluster may contain (default: max int)
52  * \param[in] max_label
53  * \ingroup segmentation
54  */
55  template <typename PointT> void
57  const PointCloud<PointT> &cloud, const typename search::Search<PointT>::Ptr &tree,
58  float tolerance, std::vector<std::vector<PointIndices> > &labeled_clusters,
59  unsigned int min_pts_per_cluster = 1, unsigned int max_pts_per_cluster = std::numeric_limits<unsigned int>::max (),
60  unsigned int max_label = std::numeric_limits<unsigned int>::max ());
61 
62  //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
63  //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
64  //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
65  /** \brief @b LabeledEuclideanClusterExtraction represents a segmentation class for cluster extraction in an Euclidean sense, with label info.
66  * \author Koen Buys
67  * \ingroup segmentation
68  */
69  template <typename PointT>
71  {
73 
74  public:
76  typedef typename PointCloud::Ptr PointCloudPtr;
78 
80  typedef typename KdTree::Ptr KdTreePtr;
81 
84 
85  //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
86  /** \brief Empty constructor. */
88  tree_ (),
91  max_pts_per_cluster_ (std::numeric_limits<int>::max ()),
92  max_label_ (std::numeric_limits<int>::max ())
93  {};
94 
95  /** \brief Provide a pointer to the search object.
96  * \param[in] tree a pointer to the spatial search object.
97  */
98  inline void
99  setSearchMethod (const KdTreePtr &tree) { tree_ = tree; }
100 
101  /** \brief Get a pointer to the search method used. */
102  inline KdTreePtr
103  getSearchMethod () const { return (tree_); }
104 
105  /** \brief Set the spatial cluster tolerance as a measure in the L2 Euclidean space
106  * \param[in] tolerance the spatial cluster tolerance as a measure in the L2 Euclidean space
107  */
108  inline void
109  setClusterTolerance (double tolerance) { cluster_tolerance_ = tolerance; }
110 
111  /** \brief Get the spatial cluster tolerance as a measure in the L2 Euclidean space. */
112  inline double
114 
115  /** \brief Set the minimum number of points that a cluster needs to contain in order to be considered valid.
116  * \param[in] min_cluster_size the minimum cluster size
117  */
118  inline void
119  setMinClusterSize (int min_cluster_size) { min_pts_per_cluster_ = min_cluster_size; }
120 
121  /** \brief Get the minimum number of points that a cluster needs to contain in order to be considered valid. */
122  inline int
124 
125  /** \brief Set the maximum number of points that a cluster needs to contain in order to be considered valid.
126  * \param[in] max_cluster_size the maximum cluster size
127  */
128  inline void
129  setMaxClusterSize (int max_cluster_size) { max_pts_per_cluster_ = max_cluster_size; }
130 
131  /** \brief Get the maximum number of points that a cluster needs to contain in order to be considered valid. */
132  inline int
134 
135  /** \brief Set the maximum number of labels in the cloud.
136  * \param[in] max_label the maximum
137  */
138  inline void
139  setMaxLabels (unsigned int max_label) { max_label_ = max_label; }
140 
141  /** \brief Get the maximum number of labels */
142  inline unsigned int
143  getMaxLabels () const { return (max_label_); }
144 
145  /** \brief Cluster extraction in a PointCloud given by <setInputCloud (), setIndices ()>
146  * \param[out] labeled_clusters the resultant point clusters
147  */
148  void
149  extract (std::vector<std::vector<PointIndices> > &labeled_clusters);
150 
151  protected:
152  // Members derived from the base class
153  using BasePCLBase::input_;
154  using BasePCLBase::indices_;
157 
158  /** \brief A pointer to the spatial search object. */
159  KdTreePtr tree_;
160 
161  /** \brief The spatial cluster tolerance as a measure in the L2 Euclidean space. */
163 
164  /** \brief The minimum number of points that a cluster needs to contain in order to be considered valid (default = 1). */
166 
167  /** \brief The maximum number of points that a cluster needs to contain in order to be considered valid (default = MAXINT). */
169 
170  /** \brief The maximum number of labels we can find in this pointcloud (default = MAXINT)*/
171  unsigned int max_label_;
172 
173  /** \brief Class getName method. */
174  virtual std::string getClassName () const { return ("LabeledEuclideanClusterExtraction"); }
175 
176  };
177 
178  /** \brief Sort clusters method (for std::sort).
179  * \ingroup segmentation
180  */
181  inline bool
183  {
184  return (a.indices.size () < b.indices.size ());
185  }
186 }
187 
188 #ifdef PCL_NO_PRECOMPILE
189 #include <pcl/segmentation/impl/extract_labeled_clusters.hpp>
190 #endif
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:44
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...
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
boost::shared_ptr< PointCloud< PointT > > Ptr
Definition: point_cloud.h:427
unsigned int getMaxLabels() const
Get the maximum number of labels.
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:80
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...
PCL base class.
Definition: pcl_base.h:68
virtual std::string getClassName() const
Class getName method.
boost::shared_ptr< const PointCloud< PointT > > ConstPtr
Definition: point_cloud.h:428
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...
PointCloud represents the base class in PCL for storing collections of 3D points. ...
int max_pts_per_cluster_
The maximum number of points that a cluster needs to contain in order to be considered valid (default...
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 <setInputCloud (), setIndices ()>
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
Generic search class.
Definition: search.h:73