test_pcl_segmentation.cpp
Go to the documentation of this file.
00001 
00063 #ifdef PCL_MINOR_VERSION >= 6
00064 
00065 #include <pcl/point_types.h>
00066 #include <pcl/io/pcd_io.h>
00067 #include <pcl/common/file_io.h>
00068 #include <pcl/search/organized.h>
00069 #include <pcl/features/normal_3d.h>
00070 #include <pcl/features/integral_image_normal.h>
00071 #include <pcl/segmentation/organized_multi_plane_segmentation.h>
00072 #include <pcl/segmentation/edge_aware_plane_comparator.h>
00073 #include <pcl/visualization/pcl_visualizer.h>
00074 #include <pcl/visualization/point_cloud_handlers.h>
00075 
00076 #include <cob_3d_mapping_common/stop_watch.h>
00077 #include "cob_3d_mapping_common/point_types.h"
00078 
00079 using namespace pcl;
00080 
00081 int main (int argc, char** argv)
00082 {
00083   if (argc != 2)
00084   {
00085     std::cerr << "Please provide an input pcd file" << std::endl;
00086     return 0;
00087   }
00088 
00089   PCDReader r;
00090   PointCloud<PointXYZRGB>::Ptr p(new PointCloud<PointXYZRGB>);
00091   PointCloud<Normal>::Ptr n(new PointCloud<Normal>);
00092   float* distance_map;
00093   PointCloud<PointLabel>::Ptr l(new PointCloud<PointLabel>);
00094   PointCloud<Label>::Ptr l2(new PointCloud<Label>);
00095 
00096   r.read( argv[1], *p);
00097 
00098   PrecisionStopWatch t;
00099   std::cout << "Computing Normals..." << std::endl;
00100   t.precisionStart();
00101   /*search::OrganizedNeighbor<PointXYZRGB>::Ptr tree(new search::OrganizedNeighbor<PointXYZRGB>);
00102   NormalEstimation<PointXYZRGB, Normal> ne;
00103   ne.setSearchMethod(tree);
00104   ne.setKSearch(200);
00105   ne.setInputCloud(p);
00106   ne.compute(*n);*/
00107   IntegralImageNormalEstimation<PointXYZRGB, Normal> ne;
00108   ne.setNormalEstimationMethod(ne.COVARIANCE_MATRIX);
00109   ne.setMaxDepthChangeFactor(0.02f);
00110   //ne.setDepthDependentSmoothing(true);
00111   ne.setNormalSmoothingSize(40.0f);
00112   ne.setInputCloud(p);
00113   ne.compute(*n);
00114   distance_map = ne.getDistanceMap();
00115   std::cout << "Took: " << t.precisionStop() << std::endl;
00116 
00117   std::cout << "Computing segments..." << std::endl;
00118   t.precisionStart();
00119   std::vector<PlanarRegion<PointXYZRGB>, Eigen::aligned_allocator<PlanarRegion<PointXYZRGB> > > regions;
00120   std::vector<ModelCoefficients> coef;
00121   std::vector<PointIndices> inlier_indices, label_indices, boundary_indices;
00122   OrganizedMultiPlaneSegmentation<PointXYZRGB, Normal, Label> omps;
00123   EdgeAwarePlaneComparator<PointXYZRGB, Normal>::Ptr comparator(new EdgeAwarePlaneComparator<PointXYZRGB, Normal>(distance_map));
00124   omps.setComparator(comparator);
00125   omps.setInputCloud(p);
00126   omps.setInputNormals(n);
00127   omps.setMinInliers(100);
00128   omps.setAngularThreshold(5.0f/180.0f*M_PI);
00129   omps.setMaximumCurvature(0.1); // default 0.001
00130   omps.setDistanceThreshold(0.02f);
00131   omps.segmentAndRefine(regions, coef, inlier_indices, l2, label_indices, boundary_indices);
00132   std::cout << "Took: " << t.precisionStop() << std::endl;
00133   std::cout << "Regions: " << regions.size() << std::endl;
00134   std::cout << "Coefs: " << coef.size() << std::endl;
00135   std::cout << "Inlier: " << inlier_indices.size() << std::endl;
00136   std::cout << "Label: " << label_indices.size() << std::endl;
00137   std::cout << "Boundary: " << boundary_indices.size() << std::endl;
00138   std::cout << "Cloud: " << l2->size() << std::endl;
00139 
00140   std::map<int, int> colors;
00141   for (size_t i = 0; i < regions.size(); i++)
00142   {
00143     uint8_t r = rand() % 255;
00144     uint8_t g = rand() % 255;
00145     uint8_t b = rand() % 255;
00146     colors[i] = (r << 16 | b << 8 | b);
00147     std::cout << "[" << i << "] => " << colors[i] << std::endl;
00148   }
00149 
00150   for (size_t i=0; i<inlier_indices.size(); ++i)
00151   {
00152     for (size_t j=0; j<inlier_indices[i].indices.size(); ++j)
00153     {
00154       (*p)[ inlier_indices[i].indices[j] ].rgba = colors[ i ];
00155     }
00156   }
00157 
00158   visualization::PCLVisualizer v;
00159   visualization::PointCloudColorHandlerRGBField<PointXYZRGB> col_hdl (p);
00160 
00161 
00162   int v1(0);
00163   v.createViewPort(0.0, 0.0, 0.5, 1.0, v1);
00164   v.addPointCloud<PointXYZRGB>(p, col_hdl, "segmented1");
00165 
00166 
00167   int v2(0);
00168   v.createViewPort(0.5, 0.0, 1.0, 1.0, v2);
00169   v.addPointCloudNormals<PointXYZRGB, Normal>(p, n, 10, 0.02, "boundary_normals", v2);
00170   while(!v.wasStopped())
00171   {
00172     v.spinOnce(100);
00173     usleep(100000);
00174   }
00175 
00176   return 0;
00177 }
00178 
00179 
00180 #else
00181 // Better: check in CMakeLists for pcl version!
00182 #include <iostream>
00183 int main (int argc, char** argv)
00184 {
00185   std::cout << "not supported by PCL older than 1.7" << std::endl;
00186 }
00187 
00188 #endif


cob_3d_segmentation
Author(s): Georg Arbeiter
autogenerated on Wed Aug 26 2015 11:03:03