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 
27 
29 {
30  /// Create variables needed for preparations
31  input_cloud_ptr.reset(new pcl::PointCloud<PointT>);
32  input_normals_ptr.reset(new pcl::PointCloud<pcl::Normal>);
33 
34  sv_labeled_cloud.reset(new pcl::PointCloud<pcl::PointXYZL>);
35  lccp_labeled_cloud.reset(new pcl::PointCloud<pcl::PointXYZL>);
36 
37  has_normals = false;
38 
39  /// Default values of parameters before parsing
40  // Supervoxel Stuff
41  voxel_resolution = 0.0095f;
42  seed_resolution = 0.06f;
43  color_importance = 0.0f;
44  spatial_importance = 1.0f;
45  normal_importance = 4.0f;
46  use_single_cam_transform = false;
47  use_supervoxel_refinement = false;
48 
49  // LCCPSegmentation Stuff
50  concavity_tolerance_threshold = 10;
51  smoothness_threshold = 0.1;
52  min_segment_size = 10;
53  use_extended_convexity = false;
54  use_sanity_criterion = false;
55 
56  /// Parse Arguments needed for computation
57  //Supervoxel Stuff
58  normals_scale = seed_resolution / 2.0;
59 
60  // Segmentation Stuff
61  k_factor = 0;
62 
63  if (use_extended_convexity)
64  {
65  k_factor = 1;
66  }
67 
68 }
69 //---------------------------------------------------------------------------------------------------------------------//
70 
72 {
73 
74 }
75 //---------------------------------------------------------------------------------------------------------------------//
76 
77 pcl::PointCloud< pcl::PointXYZL >::Ptr& 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 bool LCCPSegClass::SegmentPointCloud()
89 {
90  /// -----------------------------------| Main Computation |-----------------------------------
91 
92  /// Preparation of Input: Supervoxel Oversegmentation
93  pcl::SupervoxelClustering<PointT> super(voxel_resolution, seed_resolution);
94  super.setUseSingleCameraTransform(use_single_cam_transform);
95  //pcl::SupervoxelClustering<PointT> super (voxel_resolution, seed_resolution,use_single_cam_transform);
96  super.setInputCloud(input_cloud_ptr);
97 
98  if (has_normals)
99  {
100  super.setNormalCloud(input_normals_ptr);
101  }
102 
103  super.setColorImportance(color_importance);
104  super.setSpatialImportance(spatial_importance);
105  super.setNormalImportance(normal_importance);
106  supervoxel_clusters.clear();
107 
108  //PCL_INFO << " LCCP Segmenter is extracting supervoxels";
109  super.extract(supervoxel_clusters);
110 
111  if (supervoxel_clusters.size() == 0)
112  {
113  //PCL_INFO << " LCCP Segmenter has no supervoxel! ";
114  return false;
115  }
116  else
117  {
118  if (use_supervoxel_refinement)
119  {
120  //PCL_INFO << " LCCP Segmenter is refining supervoxels";
121  super.refineSupervoxels(2, supervoxel_clusters);
122  }
123 
124  //PCL_INFO<< " LCCP Segmenter found " << supervoxel_clusters.size () << " supervoxels: " ;
125 
126  //PCL_INFO << " LCCP Segmenter is computing supervoxel adjacency";
127  std::multimap<uint32_t, uint32_t> supervoxel_adjacency;
128  super.getSupervoxelAdjacency(supervoxel_adjacency);
129 
130  /// Get the cloud of supervoxel centroid with normals and the colored cloud with supervoxel coloring (this is used for visulization)
131  pcl::PointCloud<pcl::PointNormal>::Ptr sv_centroid_normal_cloud = pcl::SupervoxelClustering<PointT>::makeSupervoxelNormalCloud(supervoxel_clusters);
132 
133  /// The Main Step: Perform LCCPSegmentation
134 
135  //PCL_INFO << " LCCP Segmenter is starting segmentation";
136  pcl::LCCPSegmentation<PointT> lccp;
137  lccp.setConcavityToleranceThreshold(concavity_tolerance_threshold);
138  lccp.setSanityCheck(use_sanity_criterion);
139  lccp.setSmoothnessCheck(true, voxel_resolution, seed_resolution, smoothness_threshold);
140  lccp.setKFactor(k_factor);
141 
142  //std::cout<< " LCCP Segmenter: supervoxel_clusters size: " << supervoxel_clusters.size () << " supervoxels: " << std::endl;
143 
144  if (min_segment_size > 0)
145  {
146  //PCL_INFO << " LCCP Segmenter is merging small segments";
147  lccp.setMinSegmentSize(min_segment_size);
148  //lccp.mergeSmallSegments(min_segment_size);
149  //lccp.removeSmallSegments(min_segment_size);
150  }
151 
152  lccp.setInputSupervoxels(supervoxel_clusters, supervoxel_adjacency);
153  lccp.segment();
154 
155 
156  //PCL_INFO << " LCCP Segmenter is interpolating voxel cloud for input cloud and relabeling";
157  sv_labeled_cloud = super.getLabeledCloud();
158  lccp_labeled_cloud = sv_labeled_cloud->makeShared();
159  lccp.relabelCloud(*lccp_labeled_cloud);
160  SuperVoxelAdjacencyList sv_adjacency_list;
161  lccp.getSVAdjacencyList(sv_adjacency_list); // Needed for visualization
162 
163  //PCL_INFO<< " LCCP Segmenter: labeled_cloud size: " << lccp_labeled_cloud->size() ;
164 
165  return true;
166  }
167 }
168 //---------------------------------------------------------------------------------------------------------------------//
169 
170 void LCCPSegClass::UpdateParameters(float voxelRes, float seedRes, float colorImp, float spatialImp, float normalImp, float concavityThres, float smoothnessThes, uint32_t minSegSize)
171 {
172  voxel_resolution = voxelRes;
173  seed_resolution = seedRes;
174  color_importance = colorImp;
175  spatial_importance = spatialImp;
176  normal_importance = normalImp;
177  concavity_tolerance_threshold = concavityThres;
178  smoothness_threshold = smoothnessThes;
179  min_segment_size = minSegSize;
180 
181  //PCL_INFO<< " LCCP Segmenter: Parameters updated! >> minSegSize: " << minSegSize << " voxelRes: "<< voxelRes << " seedRes: " << seedRes << " colorImp: " << colorImp << " spatialImp: " << spatialImp << " normalImp: " << normalImp << " concavityThres: " << concavityThres << " smoothnessThes: " << smoothnessThes;
182 }
183 
LCCP_Segmenter.h
LCCPSegClass::LCCPSegClass
LCCPSegClass()
Definition: LCCP_Segmenter.cpp:28
LCCPSegClass::~LCCPSegClass
~LCCPSegClass()
Definition: LCCP_Segmenter.cpp:71
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:170
LCCPSegClass::GetLabeledPointCloud
pcl::PointCloud< pcl::PointXYZL >::Ptr & GetLabeledPointCloud(pcl::PointCloud< pcl::PointXYZRGBA >::Ptr &CloudPtr)
Definition: LCCP_Segmenter.cpp:77