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