Point Cloud Library (PCL)  1.14.0-dev
gpu_seeded_hue_segmentation.hpp
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2009, Willow Garage, Inc.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above
15  * copyright notice, this list of conditions and the following
16  * disclaimer in the documentation and/or other materials provided
17  * with the distribution.
18  * * Neither the name of Willow Garage, Inc. nor the names of its
19  * contributors may be used to endorse or promote products derived
20  * from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  *
35  * $Id:$
36  *
37  */
38 
39 #ifndef PCL_GPU_SEGMENTATION_IMPL_SEEDED_HUE_SEGMENTATION_H_
40 #define PCL_GPU_SEGMENTATION_IMPL_SEEDED_HUE_SEGMENTATION_H_
41 
42 #include <pcl/gpu/segmentation/gpu_seeded_hue_segmentation.h>
43 
44 //////////////////////////////////////////////////////////////////////////////////////////////
45 void
47  const pcl::gpu::Octree::Ptr& tree,
48  float tolerance,
49  PointIndices& indices_in,
50  PointIndices& indices_out,
51  float delta_hue)
52 {
53 
54  // Create a bool vector of processed point indices, and initialize it to false
55  // cloud is a DeviceArray<PointType>
56  std::vector<bool> processed(host_cloud_->size(), false);
57 
58  const auto max_answers = host_cloud_->size();
59 
60  // Process all points in the indices vector
61  for (std::size_t k = 0; k < indices_in.indices.size(); ++k) {
62  int i = indices_in.indices[k];
63  // if we already processed this point continue with the next one
64  if (processed[i])
65  continue;
66  // now we will process this point
67  processed[i] = true;
68 
69  PointXYZRGB p;
70  p = (*host_cloud_)[i];
71  PointXYZHSV h;
72  PointXYZRGBtoXYZHSV(p, h);
73 
74  // Create the query queue on the device, point based not indices
75  pcl::gpu::Octree::Queries queries_device;
76  // Create the query queue on the host
78  // Push the starting point in the vector
79  queries_host.push_back((*host_cloud_)[i]);
80 
81  unsigned int found_points = queries_host.size();
82  unsigned int previous_found_points = 0;
83 
84  pcl::gpu::NeighborIndices result_device;
85 
86  // Host buffer for results
87  std::vector<int> sizes, data;
88 
89  // once the area stop growing, stop also iterating.
90  while (previous_found_points < found_points) {
91  // Move queries to GPU
92  queries_device.upload(queries_host);
93  // Execute search
94  tree->radiusSearch(queries_device, tolerance, max_answers, result_device);
95 
96  // Store the previously found number of points
97  previous_found_points = found_points;
98 
99  // Clear the Host vectors
100  sizes.clear();
101  data.clear();
102 
103  // Copy results from GPU to Host
104  result_device.sizes.download(sizes);
105  result_device.data.download(data);
106 
107  for (std::size_t qp = 0; qp < sizes.size(); qp++) {
108  for (int qp_r = 0; qp_r < sizes[qp]; qp_r++) {
109  if (processed[data[qp_r + qp * max_answers]])
110  continue;
111 
112  PointXYZRGB p_l;
113  p_l = (*host_cloud_)[data[qp_r + qp * max_answers]];
114  PointXYZHSV h_l;
115  PointXYZRGBtoXYZHSV(p_l, h_l);
116 
117  if (std::abs(h_l.h - h.h) < delta_hue) {
118  processed[data[qp_r + qp * max_answers]] = true;
119  queries_host.push_back((*host_cloud_)[data[qp_r + qp * max_answers]]);
120  found_points++;
121  }
122  }
123  }
124  }
125  for (std::size_t qp = 0; qp < sizes.size(); qp++) {
126  for (int qp_r = 0; qp_r < sizes[qp]; qp_r++) {
127  indices_out.indices.push_back(data[qp_r + qp * max_answers]);
128  }
129  }
130  }
131  // @todo: do we need to sort here and remove double points?
132 }
133 
134 void
136  PointIndices& indices_out)
137 {
138  // Initialize the GPU search tree
139  if (!tree_) {
140  tree_.reset(new pcl::gpu::Octree());
141  ///@todo what do we do if input isn't a PointXYZ cloud?
142  tree_->setCloud(input_);
143  }
144  if (!tree_->isBuild()) {
145  tree_->build();
146  }
147  /*
148  if(tree_->cloud_.size() != host_cloud.size ())
149  {
150  PCL_ERROR("[pcl::gpu::SeededHueSegmentation] size of host cloud and device cloud
151  don't match!\n"); return;
152  }
153  */
154  // Extract the actual clusters
156  host_cloud_, tree_, cluster_tolerance_, indices_in, indices_out, delta_hue_);
157 }
158 
159 #endif // PCL_GPU_SEGMENTATION_IMPL_SEEDED_HUE_SEGMENTATION_H_
void push_back(const PointT &pt)
Insert a new point in the cloud, at the end of the container.
Definition: point_cloud.h:663
std::size_t size() const
Definition: point_cloud.h:443
std::vector< PointT, Eigen::aligned_allocator< PointT > > VectorType
Definition: point_cloud.h:411
shared_ptr< PointCloud< PointT > > Ptr
Definition: point_cloud.h:413
void upload(const T *host_ptr, std::size_t size)
Uploads data to internal buffer in GPU memory.
void download(T *host_ptr) const
Downloads data from internal buffer to CPU memory.
Octree implementation on GPU.
Definition: octree.hpp:59
shared_ptr< Octree > Ptr
Types.
Definition: octree.hpp:69
PointCloudHostPtr host_cloud_
the original cloud the Host
CloudDevice input_
the input cloud on the GPU
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)
extract clusters of a PointCloud given by <setInputCloud(), setIndices()>
GPUTreePtr tree_
A pointer to the spatial search object.
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.
float4 PointXYZRGB
Definition: internal.hpp:60
void seededHueSegmentation(const pcl::PointCloud< pcl::PointXYZRGB >::Ptr &host_cloud_, const pcl::gpu::Octree::Ptr &tree, float tolerance, PointIndices &clusters_in, PointIndices &clusters_out, float delta_hue=0.0)
void PointXYZRGBtoXYZHSV(const PointXYZRGB &in, PointXYZHSV &out)
Convert a XYZRGB point type to a XYZHSV.
DeviceArray< int > sizes
DeviceArray< int > data