LCCPSegmentation.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
19  * @author
20  * @date
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
25 
26 #ifdef OLP_USE_LCCP
27 
28 #include "LCCPSegmentation.h"
29 
30 
32 
33 // PCL
34 #include <pcl/segmentation/lccp_segmentation.h>
35 #include <pcl/segmentation/supervoxel_clustering.h>
36 #include <pcl/io/pcd_io.h>
37 
38 
39 //#include <cmath>
40 
41 
42 
43 // lccpSegmentation = new LCCPSegmentationWrapper(25, 80.0, 0.0, 1.0, 4.0, false, 10.0, 0.1, 10, false, true);
44 // lccpSegmentation = new LCCPSegmentationWrapper(7.5, 30.0, 0.0, 1.0, 4.0, false, 10.0, 0.1, 0, false, true);
45 LCCPSegmentationWrapper::LCCPSegmentationWrapper(const float voxel_resolution, const float seed_resolution, const float color_importance, const float spatial_importance, const float normal_importance,
46  const bool use_single_cam_transform, const float concavity_tolerance_threshold, const float smoothnessThreshold, const unsigned int min_segment_size, const bool use_extended_convexity,
47  const bool use_sanity_criterion, const float maxZ)
48 {
49  this->voxelResolution = voxel_resolution;
50  this->seedResolution = seed_resolution;
51  this->colorImportance = color_importance;
52  this->spatialImportance = spatial_importance;
53  this->normalImportance = normal_importance;
54  this->useSingleCamTransform = use_single_cam_transform;
55  this->concavityToleranceThreshold = concavity_tolerance_threshold;
56  this->minSegmentSize = min_segment_size;
57  this->useExtendedConvexity = use_extended_convexity;
58  this->useSanityCriterion = use_sanity_criterion;
59  this->smoothnessThreshold = smoothnessThreshold;
60  this->maxZ = maxZ;
61 }
62 
63 
64 
65 LCCPSegmentationWrapper::~LCCPSegmentationWrapper()
66 {
67 }
68 
69 
70 void LCCPSegmentationWrapper::CreateHypothesesFromLCCPSegments(const std::vector<CHypothesisPoint*>& inputPointCloud, const int maxNumHypotheses,
71  CVec3dArray*& hypothesesFromLCCP, int& numFoundSegments)
72 {
73  // convert points to PCL format
74  pcl::PointCloud<pcl::PointXYZRGBA>::Ptr inputPointCloudPCL(new pcl::PointCloud<pcl::PointXYZRGBA>);
75  inputPointCloudPCL->resize(inputPointCloud.size());
76 
77  for (size_t i = 0; i < inputPointCloud.size(); i++)
78  {
79  if (inputPointCloud.at(i)->vPosition.z <= maxZ)
80  {
81  inputPointCloudPCL->at(i) = ConvertHypothesisPointToPCL(inputPointCloud.at(i));
82  }
83  }
84 
85 #ifdef OLP_MAKE_LCCP_SEG_IMAGES
86  std::string filename = OLP_SCREENSHOT_PATH;
87  filename.append("scene.pcd");
88  pcl::io::savePCDFileASCII(filename, *inputPointCloudPCL);
89 #endif
90 
91  pcl::SupervoxelClustering<pcl::PointXYZRGBA> super(voxelResolution, seedResolution);
92  super.setUseSingleCameraTransform(useSingleCamTransform);
93  super.setInputCloud(inputPointCloudPCL);
94  super.setColorImportance(colorImportance);
95  super.setSpatialImportance(spatialImportance);
96  super.setNormalImportance(normalImportance);
97  std::map<uint32_t, pcl::Supervoxel<pcl::PointXYZRGBA>::Ptr> supervoxelClusters;
98 
99  ARMARX_INFO_S << "Extracting supervoxels";
100  super.extract(supervoxelClusters);
101  ARMARX_VERBOSE_S << "Number of Supervoxels: " << supervoxelClusters.size();
102 
103  ARMARX_VERBOSE_S << "Getting supervoxel adjacency";
104 
105  std::multimap<uint32_t, uint32_t> supervoxelAdjacency;
106  super.getSupervoxelAdjacency(supervoxelAdjacency);
107 
108  /// LCCP for merging Supervoxels
109  ARMARX_INFO_S << "Starting LCCP segmentation";
110  pcl::LCCPSegmentation<pcl::PointXYZRGBA> lccp;
111  lccp.setConcavityToleranceThreshold(concavityToleranceThreshold);
112  lccp.setSanityCheck(useSanityCriterion);
113  lccp.setSmoothnessCheck(true, voxelResolution, seedResolution, smoothnessThreshold);
114  uint kFactor = useExtendedConvexity ? 1 : 0;
115  lccp.setKFactor(kFactor);
116  lccp.setMinSegmentSize(minSegmentSize);
117  lccp.setInputSupervoxels(supervoxelClusters, supervoxelAdjacency);
118  lccp.segment();
119 
120 
121  ARMARX_VERBOSE_S << "Interpolation voxel cloud -> input cloud and relabeling";
122  pcl::PointCloud<pcl::PointXYZL>::Ptr cloudWithSegmentationLabels = super.getLabeledCloud(); // this gets the points with their supervoxel index label
123  lccp.relabelCloud(*cloudWithSegmentationLabels); // this overwrites the labels with those from the segmentation
124 
125  ARMARX_INFO_S << "Finished LCCP segmentation";
126 
127  // return segments
128  for (int i = 0; i < maxNumHypotheses; i++)
129  {
130  hypothesesFromLCCP[i].Clear();
131  }
132 
133  numFoundSegments = -1;
134 
135  for (size_t i = 0; i < cloudWithSegmentationLabels->size(); i++)
136  {
137  int label = (int)cloudWithSegmentationLabels->at(i).label;
138 
139  if (label < maxNumHypotheses)
140  {
141  pcl::PointXYZL pointPCL = cloudWithSegmentationLabels->at(i);
142  Vec3d pointIVT = {pointPCL.getVector3fMap()(0), pointPCL.getVector3fMap()(1), pointPCL.getVector3fMap()(2)};
143  hypothesesFromLCCP[label].AddElement(pointIVT);
144 
145  if (label > numFoundSegments)
146  {
147  numFoundSegments = label;
148  }
149  }
150  }
151 
152  numFoundSegments += 1;
153 }
154 
155 
156 
157 pcl::PointXYZRGBA LCCPSegmentationWrapper::ConvertHypothesisPointToPCL(const CHypothesisPoint* point)
158 {
159  pcl::PointXYZRGBA result;
160  result.getVector3fMap() = Eigen::Vector3f(point->vPosition.x, point->vPosition.y, point->vPosition.z);
161  result.r = point->fColorR;
162  result.g = point->fColorG;
163  result.b = point->fColorB;
164  return result;
165 }
166 
167 
168 #endif // OLP_USE_LCCP
169 
170 
CHypothesisPoint::fColorG
float fColorG
Definition: ObjectHypothesis.h:241
CHypothesisPoint::vPosition
Vec3d vPosition
Definition: ObjectHypothesis.h:238
ObjectLearningByPushingDefinitions.h
CHypothesisPoint
Definition: ObjectHypothesis.h:171
GfxTL::Vec3d
VectorXD< 3, double > Vec3d
Definition: VectorXD.h:695
LCCPSegmentation.h
filename
std::string filename
Definition: VisualizationRobot.cpp:84
CHypothesisPoint::fColorR
float fColorR
Definition: ObjectHypothesis.h:241
ARMARX_VERBOSE_S
#define ARMARX_VERBOSE_S
Definition: Logging.h:200
ARMARX_INFO_S
#define ARMARX_INFO_S
Definition: Logging.h:195
OLP_SCREENSHOT_PATH
#define OLP_SCREENSHOT_PATH
Definition: ObjectLearningByPushingDefinitions.h:206
Logging.h
CHypothesisPoint::fColorB
float fColorB
Definition: ObjectHypothesis.h:241