Point Cloud Library (PCL) 1.15.1-dev
Loading...
Searching...
No Matches
seeded_hue_segmentation.hpp
1/*
2 * Software License Agreement (BSD License)
3 *
4 * Point Cloud Library (PCL) - www.pointclouds.org
5 * Copyright (c) 2010-2011, Willow Garage, Inc.
6 *
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * * Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * * Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer in the documentation and/or other materials provided
18 * with the distribution.
19 * * Neither the name of the copyright holder(s) nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
35 *
36 * $id $
37 */
38
39#ifndef PCL_SEGMENTATION_IMPL_SEEDED_HUE_SEGMENTATION_H_
40#define PCL_SEGMENTATION_IMPL_SEEDED_HUE_SEGMENTATION_H_
41
42#include <pcl/segmentation/seeded_hue_segmentation.h>
43#include <pcl/console/print.h> // for PCL_ERROR
44#include <pcl/search/auto.h>
45
46//////////////////////////////////////////////////////////////////////////////////////////////
47void
50 float tolerance,
51 PointIndices &indices_in,
52 PointIndices &indices_out,
53 float delta_hue)
54{
55 if (tree->getInputCloud ()->size () != cloud.size ())
56 {
57 PCL_ERROR("[pcl::seededHueSegmentation] Tree built for a different point cloud "
58 "dataset (%zu) than the input cloud (%zu)!\n",
59 static_cast<std::size_t>(tree->getInputCloud()->size()),
60 static_cast<std::size_t>(cloud.size()));
61 return;
62 }
63 // If tree gives sorted results, we can skip the first one because it is the query point itself
64 const std::size_t nn_start_idx = tree->getSortedResults () ? 1 : 0;
65 // Create a bool vector of processed point indices, and initialize it to false
66 std::vector<bool> processed (cloud.size (), false);
67
68 Indices nn_indices;
69 std::vector<float> nn_distances;
70
71 // Process all points in the indices vector
72 for (const auto &i : indices_in.indices)
73 {
74 if (processed[i])
75 continue;
76
77 processed[i] = true;
78
79 Indices seed_queue;
80 int sq_idx = 0;
81 seed_queue.push_back (i);
82
84 p = cloud[i];
87
88 while (sq_idx < static_cast<int> (seed_queue.size ()))
89 {
90 int ret = tree->radiusSearch (seed_queue[sq_idx], tolerance, nn_indices, nn_distances, std::numeric_limits<int>::max());
91 if(ret == -1)
92 PCL_ERROR("[pcl::seededHueSegmentation] radiusSearch returned error code -1\n");
93 // Search for sq_idx
94 if (!ret)
95 {
96 sq_idx++;
97 continue;
98 }
99
100 for (std::size_t j = nn_start_idx; j < nn_indices.size (); ++j)
101 {
102 if (processed[nn_indices[j]]) // Has this point been processed before ?
103 continue;
104
105 PointXYZRGB p_l;
106 p_l = cloud[nn_indices[j]];
107 PointXYZHSV h_l;
108 PointXYZRGBtoXYZHSV(p_l, h_l);
109
110 if (std::fabs(h_l.h - h.h) < delta_hue)
111 {
112 seed_queue.push_back (nn_indices[j]);
113 processed[nn_indices[j]] = true;
114 }
115 }
116
117 sq_idx++;
118 }
119 // Copy the seed queue into the output indices
120 for (const auto &l : seed_queue)
121 indices_out.indices.push_back(l);
122 }
123 // This is purely esthetical, can be removed for speed purposes
124 std::sort (indices_out.indices.begin (), indices_out.indices.end ());
125}
126//////////////////////////////////////////////////////////////////////////////////////////////
127void
130 float tolerance,
131 PointIndices &indices_in,
132 PointIndices &indices_out,
133 float delta_hue)
134{
135 if (tree->getInputCloud ()->size () != cloud.size ())
136 {
137 PCL_ERROR("[pcl::seededHueSegmentation] Tree built for a different point cloud "
138 "dataset (%zu) than the input cloud (%zu)!\n",
139 static_cast<std::size_t>(tree->getInputCloud()->size()),
140 static_cast<std::size_t>(cloud.size()));
141 return;
142 }
143 // If tree gives sorted results, we can skip the first one because it is the query point itself
144 const std::size_t nn_start_idx = tree->getSortedResults () ? 1 : 0;
145 // Create a bool vector of processed point indices, and initialize it to false
146 std::vector<bool> processed (cloud.size (), false);
147
148 Indices nn_indices;
149 std::vector<float> nn_distances;
150
151 // Process all points in the indices vector
152 for (const auto &i : indices_in.indices)
153 {
154 if (processed[i])
155 continue;
156
157 processed[i] = true;
158
159 Indices seed_queue;
160 int sq_idx = 0;
161 seed_queue.push_back (i);
162
163 PointXYZRGB p;
164 p = cloud[i];
165 PointXYZHSV h;
167
168 while (sq_idx < static_cast<int> (seed_queue.size ()))
169 {
170 int ret = tree->radiusSearch (seed_queue[sq_idx], tolerance, nn_indices, nn_distances, std::numeric_limits<int>::max());
171 if(ret == -1)
172 PCL_ERROR("[pcl::seededHueSegmentation] radiusSearch returned error code -1\n");
173 // Search for sq_idx
174 if (!ret)
175 {
176 sq_idx++;
177 continue;
178 }
179 for (std::size_t j = nn_start_idx; j < nn_indices.size (); ++j)
180 {
181 if (processed[nn_indices[j]]) // Has this point been processed before ?
182 continue;
183
184 PointXYZRGB p_l;
185 p_l = cloud[nn_indices[j]];
186 PointXYZHSV h_l;
187 PointXYZRGBtoXYZHSV(p_l, h_l);
188
189 if (std::fabs(h_l.h - h.h) < delta_hue)
190 {
191 seed_queue.push_back (nn_indices[j]);
192 processed[nn_indices[j]] = true;
193 }
194 }
195
196 sq_idx++;
197 }
198 // Copy the seed queue into the output indices
199 for (const auto &l : seed_queue)
200 indices_out.indices.push_back(l);
201 }
202 // This is purely esthetical, can be removed for speed purposes
203 std::sort (indices_out.indices.begin (), indices_out.indices.end ());
204}
205//////////////////////////////////////////////////////////////////////////////////////////////
206//////////////////////////////////////////////////////////////////////////////////////////////
207
208void
210{
211 if (!initCompute () ||
212 (input_ && input_->points.empty ()) ||
213 (indices_ && indices_->empty ()))
214 {
215 indices_out.indices.clear ();
216 return;
217 }
218
219 // Initialize the spatial locator
220 if (!tree_)
221 {
222 tree_.reset (pcl::search::autoSelectMethod<pcl::PointXYZRGB> (input_, false, pcl::search::Purpose::radius_search));
223 }
224 else
225 // Send the input dataset to the spatial locator
226 tree_->setInputCloud (input_);
227
228 seededHueSegmentation (*input_, tree_, static_cast<float> (cluster_tolerance_), indices_in, indices_out, delta_hue_);
229 deinitCompute ();
230}
231
232#endif // PCL_EXTRACT_CLUSTERS_IMPL_H_
PointCloudConstPtr input_
The input point cloud dataset.
Definition pcl_base.h:147
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
bool deinitCompute()
This method should get called after finishing the actual computation.
Definition pcl_base.hpp:175
PointCloud represents the base class in PCL for storing collections of 3D points.
std::size_t size() const
KdTreePtr tree_
A pointer to the spatial search object.
float delta_hue_
The allowed difference on the hue.
double cluster_tolerance_
The spatial cluster tolerance as a measure in the L2 Euclidean space.
void segment(PointIndices &indices_in, PointIndices &indices_out)
Cluster extraction in a PointCloud given by <setInputCloud (), setIndices ()>
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 seededHueSegmentation(const PointCloud< PointXYZRGB > &cloud, const search::Search< PointXYZRGB >::Ptr &tree, float tolerance, PointIndices &indices_in, PointIndices &indices_out, float delta_hue=0.0)
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.
void PointXYZRGBtoXYZHSV(const PointXYZRGB &in, PointXYZHSV &out)
Convert a XYZRGB point type to a XYZHSV.
IndicesAllocator<> Indices
Type used for indices in PCL.
Definition types.h:133
A point structure representing Euclidean xyz coordinates, and the RGB color.