Point Cloud Library (PCL)
1.12.1
Toggle main menu visibility
Loading...
Searching...
No Matches
pcl
segmentation
impl
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
Po
int
T>
44
void
45
pcl::extractLabeledEuclideanClusters
(
46
const
PointCloud<PointT>
& cloud,
47
const
typename
search::Search<PointT>::Ptr
& tree,
48
float
tolerance,
49
std::vector<std::vector<PointIndices>>& labeled_clusters,
50
unsigned
int
min_pts_per_cluster,
51
unsigned
int
max_pts_per_cluster,
52
unsigned
int
)
53
{
54
pcl::extractLabeledEuclideanClusters<PointT>
(cloud,
55
tree,
56
tolerance,
57
labeled_clusters,
58
min_pts_per_cluster,
59
max_pts_per_cluster);
60
}
61
62
template
<
typename
Po
int
T>
63
void
64
pcl::extractLabeledEuclideanClusters
(
65
const
PointCloud<PointT>
& cloud,
66
const
typename
search::Search<PointT>::Ptr
& tree,
67
float
tolerance,
68
std::vector<std::vector<PointIndices>>& labeled_clusters,
69
unsigned
int
min_pts_per_cluster,
70
unsigned
int
max_pts_per_cluster)
71
{
72
if
(tree->
getInputCloud
()->size() != cloud.
size
()) {
73
PCL_ERROR(
"[pcl::extractLabeledEuclideanClusters] Tree built for a different point "
74
"cloud dataset (%lu) than the input cloud (%lu)!\n"
,
75
tree->
getInputCloud
()->size(),
76
cloud.
size
());
77
return
;
78
}
79
// Create a bool vector of processed point indices, and initialize it to false
80
std::vector<bool> processed(cloud.
size
(),
false
);
81
82
Indices
nn_indices;
83
std::vector<float> nn_distances;
84
85
// Process all points in the indices vector
86
for
(
index_t
i = 0; i < static_cast<index_t>(cloud.
size
()); ++i) {
87
if
(processed[i])
88
continue
;
89
90
Indices
seed_queue;
91
int
sq_idx = 0;
92
seed_queue.push_back(i);
93
94
processed[i] =
true
;
95
96
while
(sq_idx <
static_cast<
int
>
(seed_queue.size())) {
97
// Search for sq_idx
98
int
ret = tree->
radiusSearch
(seed_queue[sq_idx],
99
tolerance,
100
nn_indices,
101
nn_distances,
102
std::numeric_limits<int>::max());
103
if
(ret == -1)
104
PCL_ERROR(
"radiusSearch on tree came back with error -1"
);
105
if
(!ret) {
106
sq_idx++;
107
continue
;
108
}
109
110
for
(std::size_t j = 1; j < nn_indices.size();
111
++j)
// nn_indices[0] should be sq_idx
112
{
113
if
(processed[nn_indices[j]])
// Has this point been processed before ?
114
continue
;
115
if
(cloud[i].label == cloud[nn_indices[j]].label) {
116
// Perform a simple Euclidean clustering
117
seed_queue.push_back(nn_indices[j]);
118
processed[nn_indices[j]] =
true
;
119
}
120
}
121
122
sq_idx++;
123
}
124
125
// If this queue is satisfactory, add to the clusters
126
if
(seed_queue.size() >= min_pts_per_cluster &&
127
seed_queue.size() <= max_pts_per_cluster) {
128
pcl::PointIndices
r;
129
r.
indices
.resize(seed_queue.size());
130
for
(std::size_t j = 0; j < seed_queue.size(); ++j)
131
r.
indices
[j] = seed_queue[j];
132
133
std::sort(r.
indices
.begin(), r.
indices
.end());
134
r.
indices
.erase(std::unique(r.
indices
.begin(), r.
indices
.end()), r.
indices
.end());
135
136
r.
header
= cloud.
header
;
137
labeled_clusters[cloud[i].label].push_back(
138
r);
// We could avoid a copy by working directly in the vector
139
}
140
}
141
}
142
//////////////////////////////////////////////////////////////////////////////////////////////
143
//////////////////////////////////////////////////////////////////////////////////////////////
144
//////////////////////////////////////////////////////////////////////////////////////////////
145
146
template
<
typename
Po
int
T>
147
void
148
pcl::LabeledEuclideanClusterExtraction<PointT>::extract
(
149
std::vector<std::vector<PointIndices>>& labeled_clusters)
150
{
151
if
(!
initCompute
() || (
input_
&&
input_
->empty()) ||
152
(
indices_
&&
indices_
->empty())) {
153
labeled_clusters.clear();
154
return
;
155
}
156
157
// Initialize the spatial locator
158
if
(!
tree_
) {
159
if
(
input_
->isOrganized())
160
tree_
.reset(
new
pcl::search::OrganizedNeighbor<PointT>
());
161
else
162
tree_
.reset(
new
pcl::search::KdTree<PointT>
(
false
));
163
}
164
165
// Send the input dataset to the spatial locator
166
tree_
->setInputCloud(
input_
);
167
extractLabeledEuclideanClusters
(*
input_
,
168
tree_
,
169
static_cast<
float
>
(
cluster_tolerance_
),
170
labeled_clusters,
171
min_pts_per_cluster_
,
172
max_pts_per_cluster_
);
173
174
// Sort the clusters based on their size (largest one first)
175
for
(
auto
& labeled_cluster : labeled_clusters)
176
std::sort(labeled_cluster.rbegin(), labeled_cluster.rend(),
comparePointClusters
);
177
178
deinitCompute
();
179
}
180
181
#define PCL_INSTANTIATE_LabeledEuclideanClusterExtraction(T) \
182
template class PCL_EXPORTS pcl::LabeledEuclideanClusterExtraction<T>;
183
#define PCL_INSTANTIATE_extractLabeledEuclideanClusters_deprecated(T) \
184
template void PCL_EXPORTS pcl::extractLabeledEuclideanClusters<T>( \
185
const pcl::PointCloud<T>&, \
186
const typename pcl::search::Search<T>::Ptr&, \
187
float, \
188
std::vector<std::vector<pcl::PointIndices>>&, \
189
unsigned int, \
190
unsigned int, \
191
unsigned int);
192
#define PCL_INSTANTIATE_extractLabeledEuclideanClusters(T) \
193
template void PCL_EXPORTS pcl::extractLabeledEuclideanClusters<T>( \
194
const pcl::PointCloud<T>&, \
195
const typename pcl::search::Search<T>::Ptr&, \
196
float, \
197
std::vector<std::vector<pcl::PointIndices>>&, \
198
unsigned int, \
199
unsigned int);
200
201
#endif
// PCL_EXTRACT_CLUSTERS_IMPL_H_
pcl::LabeledEuclideanClusterExtraction::input_
PointCloudConstPtr input_
The input point cloud dataset.
Definition
pcl_base.h:147
pcl::LabeledEuclideanClusterExtraction::tree_
KdTreePtr tree_
A pointer to the spatial search object.
Definition
extract_labeled_clusters.h:225
pcl::LabeledEuclideanClusterExtraction::cluster_tolerance_
double cluster_tolerance_
The spatial cluster tolerance as a measure in the L2 Euclidean space.
Definition
extract_labeled_clusters.h:228
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:148
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:232
pcl::LabeledEuclideanClusterExtraction::indices_
IndicesPtr indices_
A pointer to the vector of point indices to use.
Definition
pcl_base.h:150
pcl::LabeledEuclideanClusterExtraction::initCompute
bool initCompute()
This method should get called before starting the actual computation.
Definition
pcl_base.hpp:138
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:236
pcl::LabeledEuclideanClusterExtraction::deinitCompute
bool deinitCompute()
This method should get called after finishing the actual computation.
Definition
pcl_base.hpp:174
pcl::PointCloud
PointCloud represents the base class in PCL for storing collections of 3D points.
Definition
point_cloud.h:173
pcl::PointCloud::header
pcl::PCLHeader header
The point cloud header.
Definition
point_cloud.h:392
pcl::PointCloud::size
std::size_t size() const
Definition
point_cloud.h:443
pcl::search::KdTree
search::KdTree is a wrapper class which inherits the pcl::KdTree class for performing search function...
Definition
kdtree.h:62
pcl::search::OrganizedNeighbor
OrganizedNeighbor is a class for optimized nearest neigbhor search in organized point clouds.
Definition
organized.h:61
pcl::search::Search::Ptr
shared_ptr< pcl::search::Search< PointT > > Ptr
Definition
search.h:81
pcl::search::Search::getInputCloud
virtual PointCloudConstPtr getInputCloud() const
Get a pointer to the input point cloud dataset.
Definition
search.h:125
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::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::comparePointClusters
bool comparePointClusters(const pcl::PointIndices &a, const pcl::PointIndices &b)
Sort clusters method (for std::sort).
Definition
extract_clusters.h:448
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:112
pcl::Indices
IndicesAllocator<> Indices
Type used for indices in PCL.
Definition
types.h:133
pcl::PointIndices
Definition
PointIndices.h:12
pcl::PointIndices::header
::pcl::PCLHeader header
Definition
PointIndices.h:19
pcl::PointIndices::indices
Indices indices
Definition
PointIndices.h:21