Advertisement
Guest User

Plane model segmentation + Normals estimation for each plane

a guest
Aug 21st, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.37 KB | None | 0 0
  1. #include <iostream>
  2. #include <pcl/ModelCoefficients.h>
  3. #include <pcl/io/pcd_io.h>
  4. #include <pcl/point_types.h>
  5. #include <pcl/sample_consensus/method_types.h>
  6. #include <pcl/sample_consensus/model_types.h>
  7. #include <pcl/segmentation/sac_segmentation.h>
  8. #include <pcl/filters/voxel_grid.h>
  9. #include <pcl/filters/extract_indices.h>
  10. #include <pcl/features/normal_3d.h>
  11. #include <pcl/visualization/cloud_viewer.h>
  12.  
  13. #include <pcl/io/io.h>
  14. #include <pcl/features/integral_image_normal.h>
  15.  
  16. int main (int argc, char** argv)
  17. {
  18.   pcl::PCLPointCloud2::Ptr cloud_blob (new pcl::PCLPointCloud2), cloud_filtered_blob (new pcl::PCLPointCloud2);
  19.   pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_filtered (new pcl::PointCloud<pcl::PointXYZ>), cloud_p (new
  20.   pcl::PointCloud<pcl::PointXYZ>), cloud_f (new pcl::PointCloud<pcl::PointXYZ>);
  21.  
  22.   // Load the cloud data
  23.   pcl::PCDReader reader;
  24.   reader.read ("table_scene_lms400.pcd", *cloud_blob);
  25.   std::cerr << "PointCloud before filtering: " << cloud_blob->width * cloud_blob->height << " data points." << std::endl;
  26.  
  27.   // Create the filtering object: downsample the dataset using a leaf size of 1cm
  28.   pcl::VoxelGrid<pcl::PCLPointCloud2> sor;
  29.   sor.setInputCloud (cloud_blob);
  30.   sor.setLeafSize (0.01f, 0.01f, 0.01f);
  31.   sor.filter (*cloud_filtered_blob);
  32.  
  33.   // Convert to the templated PointCloud
  34.   pcl::fromPCLPointCloud2 (*cloud_filtered_blob, *cloud_filtered);
  35.   std::cerr << "PointCloud after filtering: " << cloud_filtered->width * cloud_filtered->height << " data points." << std::endl;
  36.  
  37.   pcl::PCDWriter writer;
  38.  
  39.   // Write the downsampled version to disk
  40.   //writer.write<pcl::PointXYZ> ("downsampled.pcd", *cloud_filtered, false);
  41.  
  42.   pcl::ModelCoefficients::Ptr coefficients (new pcl::ModelCoefficients ());
  43.   pcl::PointIndices::Ptr inliers (new pcl::PointIndices ());
  44.  
  45.   // Create the segmentation object
  46.   pcl::SACSegmentation<pcl::PointXYZ> seg;
  47.  
  48.   // Optional
  49.   seg.setOptimizeCoefficients (true);
  50.  
  51.   // Mandatory
  52.   seg.setModelType (pcl::SACMODEL_PLANE);
  53.   seg.setMethodType (pcl::SAC_RANSAC);
  54.   seg.setMaxIterations (1000);
  55.   seg.setDistanceThreshold (0.05);
  56.  
  57.   // Create the filtering object
  58.   pcl::ExtractIndices<pcl::PointXYZ> extract;
  59.  
  60.   int i = 0, nr_points = (int) cloud_filtered->points.size ();
  61.  
  62.   // While 30% of the original cloud is still there
  63.   while (cloud_filtered->points.size () > 0.1 * nr_points)
  64.   {
  65.     // Segment the largest planar component from the remaining cloud
  66.     seg.setInputCloud (cloud_filtered);
  67.     seg.segment (*inliers, *coefficients);
  68.     if (inliers->indices.size () == 0)
  69.     {
  70.       std::cerr << "Could not estimate a planar model for the given dataset." << std::endl;
  71.       break;
  72.     }
  73.  
  74.     // Extract the inliers
  75.     extract.setInputCloud (cloud_filtered);
  76.     extract.setIndices (inliers);
  77.     extract.setNegative (false);
  78.     extract.filter (*cloud_p);
  79.     std::cerr << "PointCloud representing the planar component: " << cloud_p->width * cloud_p->height << " data points." << std::endl;
  80.  
  81.     std::stringstream ss;
  82.     ss << "plane_" << i << ".pcd";
  83.     writer.write<pcl::PointXYZ> (ss.str (), *cloud_p, false);
  84.  
  85.   // Create the normal estimation class, and pass the input dataset to it
  86.   pcl::NormalEstimation<pcl::PointXYZ, pcl::Normal> ne;
  87.   ne.setInputCloud (cloud_p);
  88.  
  89.   // Create an empty kdtree representation, and pass it to the normal estimation object.
  90.   // Its content will be filled inside the object, based on the given input dataset (as no other search surface is given).
  91.   pcl::search::KdTree<pcl::PointXYZ>::Ptr tree (new pcl::search::KdTree<pcl::PointXYZ> ());
  92.   ne.setSearchMethod (tree);
  93.  
  94.   // Output datasets
  95.   pcl::PointCloud<pcl::Normal>::Ptr cloud_normals (new pcl::PointCloud<pcl::Normal>);
  96.  
  97.   // Use all neighbors in a sphere of radius 3cm
  98.   ne.setRadiusSearch (0.03);
  99.  
  100.   // Compute the features
  101.   ne.compute (*cloud_normals);
  102.  
  103. //  std::cout << "Normals: " << cloud_normals->points[0] << std::endl;
  104.  
  105.  
  106.   // visualize normals
  107.   pcl::visualization::PCLVisualizer viewer("PCL Viewer");
  108.   viewer.setBackgroundColor (0.0, 0.0, 0.5);
  109.   viewer.addPointCloudNormals<pcl::PointXYZ,pcl::Normal>(cloud_p, cloud_normals);
  110.  
  111.   while (!viewer.wasStopped ())
  112.   {
  113.      viewer.spinOnce ();
  114.   }
  115.  
  116.     // Create the filtering object
  117.     extract.setNegative (true);
  118.     extract.filter (*cloud_f);
  119.     cloud_filtered.swap (cloud_f);
  120.     i++;
  121.   }
  122.  
  123.   return (0);
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement