Point Cloud Library (PCL) 1.15.1-dev
Loading...
Searching...
No Matches
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#include <pcl/search/auto.h>
42
43//////////////////////////////////////////////////////////////////////////////////////////////
44template <typename PointT>
45void
47 const PointCloud<PointT>& cloud,
48 const typename search::Search<PointT>::Ptr& tree,
49 float tolerance,
50 std::vector<std::vector<PointIndices>>& labeled_clusters,
51 unsigned int min_pts_per_cluster,
52 unsigned int max_pts_per_cluster)
53{
54 if (tree->getInputCloud()->size() != cloud.size()) {
55 PCL_ERROR("[pcl::extractLabeledEuclideanClusters] Tree built for a different point "
56 "cloud dataset (%lu) than the input cloud (%lu)!\n",
57 tree->getInputCloud()->size(),
58 cloud.size());
59 return;
60 }
61 // If tree gives sorted results, we can skip the first one because it is the query point itself
62 const std::size_t nn_start_idx = tree->getSortedResults () ? 1 : 0;
63 // Create a bool vector of processed point indices, and initialize it to false
64 std::vector<bool> processed(cloud.size(), false);
65
66 Indices nn_indices;
67 std::vector<float> nn_distances;
68
69 // Process all points in the indices vector
70 for (index_t i = 0; i < static_cast<index_t>(cloud.size()); ++i) {
71 if (processed[i])
72 continue;
73
74 Indices seed_queue;
75 int sq_idx = 0;
76 seed_queue.push_back(i);
77
78 processed[i] = true;
79
80 while (sq_idx < static_cast<int>(seed_queue.size())) {
81 // Search for sq_idx
82 int ret = tree->radiusSearch(seed_queue[sq_idx],
83 tolerance,
84 nn_indices,
85 nn_distances,
86 std::numeric_limits<int>::max());
87 if (ret == -1)
88 PCL_ERROR("radiusSearch on tree came back with error -1");
89 if (!ret) {
90 sq_idx++;
91 continue;
92 }
93
94 for (std::size_t j = nn_start_idx; j < nn_indices.size(); ++j)
95 {
96 if (processed[nn_indices[j]]) // Has this point been processed before ?
97 continue;
98 if (cloud[i].label == cloud[nn_indices[j]].label) {
99 // Perform a simple Euclidean clustering
100 seed_queue.push_back(nn_indices[j]);
101 processed[nn_indices[j]] = true;
102 }
103 }
104
105 sq_idx++;
106 }
107
108 // If this queue is satisfactory, add to the clusters
109 if (seed_queue.size() >= min_pts_per_cluster &&
110 seed_queue.size() <= max_pts_per_cluster) {
112 r.indices.resize(seed_queue.size());
113 for (std::size_t j = 0; j < seed_queue.size(); ++j)
114 r.indices[j] = seed_queue[j];
115 // After clustering, indices are out of order, so sort them
116 std::sort(r.indices.begin(), r.indices.end());
117
118 r.header = cloud.header;
119 labeled_clusters[cloud[i].label].push_back(
120 r); // We could avoid a copy by working directly in the vector
121 }
122 }
123}
124//////////////////////////////////////////////////////////////////////////////////////////////
125//////////////////////////////////////////////////////////////////////////////////////////////
126//////////////////////////////////////////////////////////////////////////////////////////////
127
128template <typename PointT>
129void
131 std::vector<std::vector<PointIndices>>& labeled_clusters)
132{
133 if (!initCompute() || (input_ && input_->empty()) ||
134 (indices_ && indices_->empty())) {
135 labeled_clusters.clear();
136 return;
137 }
138
139 // Initialize the spatial locator
140 if (!tree_)
141 tree_.reset(pcl::search::autoSelectMethod<PointT>(input_, false, pcl::search::Purpose::radius_search));
142 else
143 // Send the input dataset to the spatial locator
144 tree_->setInputCloud(input_);
145 extractLabeledEuclideanClusters(*input_,
146 tree_,
147 static_cast<float>(cluster_tolerance_),
148 labeled_clusters,
149 min_pts_per_cluster_,
150 max_pts_per_cluster_);
151
152 // Sort the clusters based on their size (largest one first)
153 for (auto& labeled_cluster : labeled_clusters)
154 std::sort(labeled_cluster.rbegin(), labeled_cluster.rend(), comparePointClusters);
155
156 deinitCompute();
157}
158
159#define PCL_INSTANTIATE_LabeledEuclideanClusterExtraction(T) \
160 template class PCL_EXPORTS pcl::LabeledEuclideanClusterExtraction<T>;
161#define PCL_INSTANTIATE_extractLabeledEuclideanClusters(T) \
162 template void PCL_EXPORTS pcl::extractLabeledEuclideanClusters<T>( \
163 const pcl::PointCloud<T>&, \
164 const typename pcl::search::Search<T>::Ptr&, \
165 float, \
166 std::vector<std::vector<pcl::PointIndices>>&, \
167 unsigned int, \
168 unsigned int);
169
170#endif // PCL_EXTRACT_CLUSTERS_IMPL_H_
void extract(std::vector< std::vector< PointIndices > > &labeled_clusters)
Cluster extraction in a PointCloud given by <setInputCloud (), setIndices ()>
PointCloud represents the base class in PCL for storing collections of 3D points.
pcl::PCLHeader header
The point cloud header.
std::size_t size() const
virtual bool getSortedResults()
Gets whether the results should be sorted (ascending in the distance) or not Otherwise the results ma...
Definition search.hpp:68
shared_ptr< pcl::search::Search< PointT > > Ptr
Definition search.h:81
virtual PointCloudConstPtr getInputCloud() const
Get a pointer to the input point cloud dataset.
Definition search.h:124
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.
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.
@ radius_search
The search method will mainly be used for radiusSearch.
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
IndicesAllocator<> Indices
Type used for indices in PCL.
Definition types.h:133
::pcl::PCLHeader header