LCCP_Segmenter.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package VisionX
19  * @author Eren Aksoy ( eren dot aksoy at kit dot edu )
20  * @date 2015
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 
25 #include "LCCP_Segmenter.h"
26 
28 {
29  /// Create variables needed for preparations
30  input_cloud_ptr.reset(new pcl::PointCloud<PointT>);
31  input_normals_ptr.reset(new pcl::PointCloud<pcl::Normal>);
32 
33  sv_labeled_cloud.reset(new pcl::PointCloud<pcl::PointXYZL>);
34  lccp_labeled_cloud.reset(new pcl::PointCloud<pcl::PointXYZL>);
35 
36  has_normals = false;
37 
38  /// Default values of parameters before parsing
39  // Supervoxel Stuff
40  voxel_resolution = 0.0095f;
41  seed_resolution = 0.06f;
42  color_importance = 0.0f;
43  spatial_importance = 1.0f;
44  normal_importance = 4.0f;
45  use_single_cam_transform = false;
46  use_supervoxel_refinement = false;
47 
48  // LCCPSegmentation Stuff
49  concavity_tolerance_threshold = 10;
50  smoothness_threshold = 0.1;
51  min_segment_size = 10;
52  use_extended_convexity = false;
53  use_sanity_criterion = false;
54 
55  /// Parse Arguments needed for computation
56  //Supervoxel Stuff
57  normals_scale = seed_resolution / 2.0;
58 
59  // Segmentation Stuff
60  k_factor = 0;
61 
62  if (use_extended_convexity)
63  {
64  k_factor = 1;
65  }
66 }
67 
68 //---------------------------------------------------------------------------------------------------------------------//
69 
71 {
72 }
73 
74 //---------------------------------------------------------------------------------------------------------------------//
75 
76 pcl::PointCloud<pcl::PointXYZL>::Ptr&
77 LCCPSegClass::GetLabeledPointCloud(pcl::PointCloud<pcl::PointXYZRGBA>::Ptr& CloudPtr)
78 {
79  // copy original points to the cloud
80  pcl::copyPointCloud(*CloudPtr, *input_cloud_ptr);
81 
82  SegmentPointCloud();
83 
84  return lccp_labeled_cloud;
85 }
86 
87 //---------------------------------------------------------------------------------------------------------------------//
88 
89 bool
90 LCCPSegClass::SegmentPointCloud()
91 {
92  /// -----------------------------------| Main Computation |-----------------------------------
93 
94  /// Preparation of Input: Supervoxel Oversegmentation
95  pcl::SupervoxelClustering<PointT> super(voxel_resolution, seed_resolution);
96  super.setUseSingleCameraTransform(use_single_cam_transform);
97  //pcl::SupervoxelClustering<PointT> super (voxel_resolution, seed_resolution,use_single_cam_transform);
98  super.setInputCloud(input_cloud_ptr);
99 
100  if (has_normals)
101  {
102  super.setNormalCloud(input_normals_ptr);
103  }
104 
105  super.setColorImportance(color_importance);
106  super.setSpatialImportance(spatial_importance);
107  super.setNormalImportance(normal_importance);
108  supervoxel_clusters.clear();
109 
110  //PCL_INFO << " LCCP Segmenter is extracting supervoxels";
111  super.extract(supervoxel_clusters);
112 
113  if (supervoxel_clusters.size() == 0)
114  {
115  //PCL_INFO << " LCCP Segmenter has no supervoxel! ";
116  return false;
117  }
118  else
119  {
120  if (use_supervoxel_refinement)
121  {
122  //PCL_INFO << " LCCP Segmenter is refining supervoxels";
123  super.refineSupervoxels(2, supervoxel_clusters);
124  }
125 
126  //PCL_INFO<< " LCCP Segmenter found " << supervoxel_clusters.size () << " supervoxels: " ;
127 
128  //PCL_INFO << " LCCP Segmenter is computing supervoxel adjacency";
129  std::multimap<uint32_t, uint32_t> supervoxel_adjacency;
130  super.getSupervoxelAdjacency(supervoxel_adjacency);
131 
132  /// Get the cloud of supervoxel centroid with normals and the colored cloud with supervoxel coloring (this is used for visulization)
133  pcl::PointCloud<pcl::PointNormal>::Ptr sv_centroid_normal_cloud =
134  pcl::SupervoxelClustering<PointT>::makeSupervoxelNormalCloud(supervoxel_clusters);
135 
136  /// The Main Step: Perform LCCPSegmentation
137 
138  //PCL_INFO << " LCCP Segmenter is starting segmentation";
139  pcl::LCCPSegmentation<PointT> lccp;
140  lccp.setConcavityToleranceThreshold(concavity_tolerance_threshold);
141  lccp.setSanityCheck(use_sanity_criterion);
142  lccp.setSmoothnessCheck(true, voxel_resolution, seed_resolution, smoothness_threshold);
143  lccp.setKFactor(k_factor);
144 
145  //std::cout<< " LCCP Segmenter: supervoxel_clusters size: " << supervoxel_clusters.size () << " supervoxels: " << std::endl;
146 
147  if (min_segment_size > 0)
148  {
149  //PCL_INFO << " LCCP Segmenter is merging small segments";
150  lccp.setMinSegmentSize(min_segment_size);
151  //lccp.mergeSmallSegments(min_segment_size);
152  //lccp.removeSmallSegments(min_segment_size);
153  }
154 
155  lccp.setInputSupervoxels(supervoxel_clusters, supervoxel_adjacency);
156  lccp.segment();
157 
158 
159  //PCL_INFO << " LCCP Segmenter is interpolating voxel cloud for input cloud and relabeling";
160  sv_labeled_cloud = super.getLabeledCloud();
161  lccp_labeled_cloud = sv_labeled_cloud->makeShared();
162  lccp.relabelCloud(*lccp_labeled_cloud);
163  SuperVoxelAdjacencyList sv_adjacency_list;
164  lccp.getSVAdjacencyList(sv_adjacency_list); // Needed for visualization
165 
166  //PCL_INFO<< " LCCP Segmenter: labeled_cloud size: " << lccp_labeled_cloud->size() ;
167 
168  return true;
169  }
170 }
171 
172 //---------------------------------------------------------------------------------------------------------------------//
173 
174 void
176  float seedRes,
177  float colorImp,
178  float spatialImp,
179  float normalImp,
180  float concavityThres,
181  float smoothnessThes,
182  uint32_t minSegSize)
183 {
184  voxel_resolution = voxelRes;
185  seed_resolution = seedRes;
186  color_importance = colorImp;
187  spatial_importance = spatialImp;
188  normal_importance = normalImp;
189  concavity_tolerance_threshold = concavityThres;
190  smoothness_threshold = smoothnessThes;
191  min_segment_size = minSegSize;
192 
193  //PCL_INFO<< " LCCP Segmenter: Parameters updated! >> minSegSize: " << minSegSize << " voxelRes: "<< voxelRes << " seedRes: " << seedRes << " colorImp: " << colorImp << " spatialImp: " << spatialImp << " normalImp: " << normalImp << " concavityThres: " << concavityThres << " smoothnessThes: " << smoothnessThes;
194 }
LCCP_Segmenter.h
LCCPSegClass::LCCPSegClass
LCCPSegClass()
Definition: LCCP_Segmenter.cpp:27
LCCPSegClass::~LCCPSegClass
~LCCPSegClass()
Definition: LCCP_Segmenter.cpp:70
LCCPSegClass::UpdateParameters
void UpdateParameters(float voxelRes, float seedRes, float colorImp, float spatialImp, float normalImp, float concavityThres, float smoothnessThes, uint32_t minSegSize)
Definition: LCCP_Segmenter.cpp:175
LCCPSegClass::GetLabeledPointCloud
pcl::PointCloud< pcl::PointXYZL >::Ptr & GetLabeledPointCloud(pcl::PointCloud< pcl::PointXYZRGBA >::Ptr &CloudPtr)
Definition: LCCP_Segmenter.cpp:77