一种平面提取的点云简化算法

1.本算法使用了PCL点云库,因此运行此代码需要安装PCL (http://pointclouds.org/)

其中平面区域的简化效率时70%,其它区域的简化效率时30%.

//downSample
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 


int 
main (int argc, char** argv)
{
  srand(time(0));
  // Read in the cloud data
  pcl::PCDReader reader;
  pcl::PointCloud::Ptr cloud_filtered (new pcl::PointCloud), cloud_f (new pcl::PointCloud);
  reader.read ("table_scene_lms400.pcd", *cloud_filtered);
  std::cout << "PointCloud has: " << cloud_filtered->points.size () << " data points." << std::endl; //*

  //输出
  ofstream fout("plane.txt");

  // Create the segmentation object for the planar model and set all the parameters
  pcl::SACSegmentation seg;
  pcl::PointIndices::Ptr inliers (new pcl::PointIndices);
  pcl::ModelCoefficients::Ptr coefficients (new pcl::ModelCoefficients);
  pcl::PointCloud::Ptr cloud_plane (new pcl::PointCloud ());
  pcl::PCDWriter writer;
  seg.setOptimizeCoefficients (true);
  seg.setModelType (pcl::SACMODEL_PLANE);
  seg.setMethodType (pcl::SAC_RANSAC);
  seg.setMaxIterations (100);
  seg.setDistanceThreshold (0.02);    //此处可以自己修改,一般保持默认即可

  int i=0, nr_points = (int) cloud_filtered->points.size ();
  while (cloud_filtered->points.size () > 0.3 * nr_points)    //此处的0.3可以修改,一般保持默认即可
  {
    // Segment the largest planar component from the remaining cloud
    seg.setInputCloud (cloud_filtered);
    seg.segment (*inliers, *coefficients);
    if (inliers->indices.size () == 0)
    {
      std::cout << "Could not estimate a planar model for the given dataset." << std::endl;
      break;
    }

    // Extract the planar inliers from the input cloud
    pcl::ExtractIndices extract;
    extract.setInputCloud (cloud_filtered);
    extract.setIndices (inliers);
    extract.setNegative (false);

    // Get the points associated with the planar surface
    extract.filter (*cloud_plane);
    std::cout << "PointCloud representing the planar component: " << cloud_plane->points.size () << " data points." << std::endl;
    for (int i = 0; i points.size (); i++)
    {
        if (rand() %100 < 30)            //平面简化率为70%
        {
            fout<points[i].x<<" "<points[i].y<<" "<points[i].z<<endl;
        }
    }
    
    // Remove the planar inliers, extract the rest
    extract.setNegative (true);
    extract.filter (*cloud_f);
    *cloud_filtered = *cloud_f;
  }
  for (int i = 0; i points.size (); i++)
  {
      if (rand() %100 < 70)            //简化率为30%
      {
      fout<points[i].x<<" "<points[i].y<<" "<points[i].z<<endl;
       }
  }
  
  return (0);
}
View Code

2.简化前后对比

一种平面提取的点云简化算法_第1张图片一种平面提取的点云简化算法_第2张图片

一种平面提取的点云简化算法_第3张图片一种平面提取的点云简化算法_第4张图片

一种平面提取的点云简化算法_第5张图片一种平面提取的点云简化算法_第6张图片

3.总结说明

分别对比三组实验数据:

第一组:桌面和地面简化率较高,其他的对象简化率地

第二组:人简化率地而平面板和地面简化率较高

第三组:墙面上的镶嵌物体得到了很好保留的同时,墙面和地面得到了和好的简化

4.实验第一组数据(其它两组数据为实验室数据不能提供)

http://pan.baidu.com/s/1i3kVW37 或

https://raw.github.com/PointCloudLibrary/data/master/tutorials/table_scene_mug_stereo_textured.pcd

转载于:https://www.cnblogs.com/lwngreat/p/4312749.html

你可能感兴趣的:(一种平面提取的点云简化算法)