Point Cloud Library (PCL)  1.11.1-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  {
63  int i = indices_in.indices[k];
64  // if we already processed this point continue with the next one
65  if (processed[i])
66  continue;
67  // now we will process this point
68  processed[i] = true;
69 
70  PointXYZRGB p;
71  p = (*host_cloud_)[i];
72  PointXYZHSV h;
73  PointXYZRGBtoXYZHSV(p, h);
74 
75  // Create the query queue on the device, point based not indices
76  pcl::gpu::Octree::Queries queries_device;
77  // Create the query queue on the host
79  // Push the starting point in the vector
80  queries_host.push_back ((*host_cloud_)[i]);
81 
82  unsigned int found_points = queries_host.size ();
83  unsigned int previous_found_points = 0;
84 
85  pcl::gpu::NeighborIndices result_device;
86 
87  // Host buffer for results
88  std::vector<int> sizes, data;
89 
90  // once the area stop growing, stop also iterating.
91  while (previous_found_points < found_points)
92  {
93  // Move queries to GPU
94  queries_device.upload(queries_host);
95  // Execute search
96  tree->radiusSearch(queries_device, tolerance, max_answers, result_device);
97 
98  // Store the previously found number of points
99  previous_found_points = found_points;
100 
101  // Clear the Host vectors
102  sizes.clear (); data.clear ();
103 
104  // Copy results from GPU to Host
105  result_device.sizes.download (sizes);
106  result_device.data.download (data);
107 
108  for(std::size_t qp = 0; qp < sizes.size (); qp++)
109  {
110  for(int qp_r = 0; qp_r < sizes[qp]; qp_r++)
111  {
112  if(processed[data[qp_r + qp * max_answers]])
113  continue;
114 
115  PointXYZRGB p_l;
116  p_l = (*host_cloud_)[data[qp_r + qp * max_answers]];
117  PointXYZHSV h_l;
118  PointXYZRGBtoXYZHSV(p_l, h_l);
119 
120  if (std::abs(h_l.h - h.h) < delta_hue)
121  {
122  processed[data[qp_r + qp * max_answers]] = true;
123  queries_host.push_back ((*host_cloud_)[data[qp_r + qp * max_answers]]);
124  found_points++;
125  }
126  }
127  }
128  }
129  for(std::size_t qp = 0; qp < sizes.size (); qp++)
130  {
131  for(int qp_r = 0; qp_r < sizes[qp]; qp_r++)
132  {
133  indices_out.indices.push_back(data[qp_r + qp * max_answers]);
134  }
135  }
136  }
137  // @todo: do we need to sort here and remove double points?
138 }
139 
140 void
142 {
143  // Initialize the GPU search tree
144  if (!tree_)
145  {
146  tree_.reset (new pcl::gpu::Octree());
147  ///@todo what do we do if input isn't a PointXYZ cloud?
148  tree_->setCloud(input_);
149  }
150  if (!tree_->isBuild())
151  {
152  tree_->build();
153  }
154 /*
155  if(tree_->cloud_.size() != host_cloud.size ())
156  {
157  PCL_ERROR("[pcl::gpu::SeededHueSegmentation] size of host cloud and device cloud don't match!\n");
158  return;
159  }
160 */
161  // Extract the actual clusters
163 }
164 
165 #endif //PCL_GPU_SEGMENTATION_IMPL_SEEDED_HUE_SEGMENTATION_H_
pcl::gpu::SeededHueSegmentation::host_cloud_
PointCloudHostPtr host_cloud_
the original cloud the Host
Definition: gpu_seeded_hue_segmentation.h:123
pcl::gpu::SeededHueSegmentation::input_
CloudDevice input_
the input cloud on the GPU
Definition: gpu_seeded_hue_segmentation.h:120
pcl::gpu::NeighborIndices::sizes
DeviceArray< int > sizes
Definition: device_format.hpp:49
pcl::gpu::SeededHueSegmentation::tree_
GPUTreePtr tree_
A pointer to the spatial search object.
Definition: gpu_seeded_hue_segmentation.h:126
pcl::gpu::Octree
Octree implementation on GPU.
Definition: octree.hpp:57
pcl::gpu::NeighborIndices::data
DeviceArray< int > data
Definition: device_format.hpp:48
pcl::gpu::SeededHueSegmentation::delta_hue_
float delta_hue_
The allowed difference on the hue.
Definition: gpu_seeded_hue_segmentation.h:132
pcl::PointCloud::VectorType
std::vector< PointT, Eigen::aligned_allocator< PointT > > VectorType
Definition: point_cloud.h:404
pcl::gpu::seededHueSegmentation
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)
pcl::PointXYZRGBtoXYZHSV
void PointXYZRGBtoXYZHSV(const PointXYZRGB &in, PointXYZHSV &out)
Convert a XYZRGB point type to a XYZHSV.
Definition: point_types_conversion.h:105
pcl::gpu::NeighborIndices
Definition: device_format.hpp:46
pcl::gpu::SeededHueSegmentation::cluster_tolerance_
double cluster_tolerance_
The spatial cluster tolerance as a measure in the L2 Euclidean space.
Definition: gpu_seeded_hue_segmentation.h:129
pcl::gpu::SeededHueSegmentation::segment
void segment(PointIndices &indices_in, PointIndices &indices_out)
Cluster extraction in a PointCloud given by <setInputCloud (), setIndices ()>
Definition: gpu_seeded_hue_segmentation.hpp:141
pcl::gpu::DeviceArray< PointType >
pcl::PointIndices
Definition: PointIndices.h:11
pcl::device::PointXYZRGB
float4 PointXYZRGB
Definition: internal.hpp:60
pcl::PointCloud::size
std::size_t size() const
Definition: point_cloud.h:436
pcl::seededHueSegmentation
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.
Definition: seeded_hue_segmentation.hpp:49
pcl::PointCloud::Ptr
shared_ptr< PointCloud< PointT > > Ptr
Definition: point_cloud.h:406
pcl::gpu::DeviceArray::upload
void upload(const T *host_ptr, std::size_t size)
Uploads data to internal buffer in GPU memory.
Definition: device_array.hpp:64
pcl::gpu::Octree::Ptr
shared_ptr< Octree > Ptr
Types.
Definition: octree.hpp:68
pcl::PointCloud::push_back
void push_back(const PointT &pt)
Insert a new point in the cloud, at the end of the container.
Definition: point_cloud.h:543
pcl::gpu::DeviceArray::download
void download(T *host_ptr) const
Downloads data from internal buffer to CPU memory.
Definition: device_array.hpp:66