【点云处理技术之PCL】滤波器——提取索引的点云(pcl::ExtractIndices)

这一部分内容为提取索引内的点云。下面的例子借用了分割算法,分割后会产生点云子集的索引,然后使用点云提取技术,提取索引范围内的点云。

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

int
main (int argc, char** argv)
{
  pcl::PCLPointCloud2::Ptr cloud_blob (new pcl::PCLPointCloud2), cloud_filtered_blob (new pcl::PCLPointCloud2);
  pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_filtered (new pcl::PointCloud<pcl::PointXYZ>), cloud_p (new pcl::PointCloud<pcl::PointXYZ>), cloud_f (new pcl::PointCloud<pcl::PointXYZ>);

  // Fill in the cloud data
  pcl::PCDReader reader;
  reader.read ("../../pcd/table_scene_lms400.pcd", *cloud_blob);

  std::cerr << "PointCloud before filtering: " << cloud_blob->width * cloud_blob->height << " data points." << std::endl;

  // Create the filtering object: downsample the dataset using a leaf size of 1cm
  //下采样,每个立方体为1立方厘米
  pcl::VoxelGrid<pcl::PCLPointCloud2> sor;
  sor.setInputCloud (cloud_blob);
  sor.setLeafSize (0.01f, 0.01f, 0.01f);
  sor.filter (*cloud_filtered_blob);

  // Convert to the templated PointCloud
  pcl::fromPCLPointCloud2 (*cloud_filtered_blob, *cloud_filtered);

  std::cerr << "PointCloud after filtering: " << cloud_filtered->width * cloud_filtered->height << " data points." << std::endl;

  // Write the downsampled version to disk
  pcl::PCDWriter writer;
  writer.write<pcl::PointXYZ> ("../../pcd/table_scene_lms400_downsampled.pcd", *cloud_filtered, false);

  pcl::ModelCoefficients::Ptr coefficients (new pcl::ModelCoefficients ());
  pcl::PointIndices::Ptr inliers (new pcl::PointIndices ());
  // Create the segmentation object
  pcl::SACSegmentation<pcl::PointXYZ> seg;//创建分割对象
  // Optional
  seg.setOptimizeCoefficients (true);//设置对估计模型参数进行优化处理
  // Mandatory
  seg.setModelType (pcl::SACMODEL_PLANE);//设置分割模型类别
  seg.setMethodType (pcl::SAC_RANSAC);//设置用哪个随机参数估计方法
  seg.setMaxIterations (1000);//设置最大迭代次数
  seg.setDistanceThreshold (0.01);//判断是否为模型内点的距离阀值

  // Create the filtering object,创建点云提取对象
  pcl::ExtractIndices<pcl::PointXYZ> extract;

  int i = 0, nr_points = (int) cloud_filtered->size ();
  // While 30% of the original cloud is still there
  while (cloud_filtered->size () > 0.3 * nr_points)
  {
    // Segment the largest planar component from the remaining cloud
    seg.setInputCloud (cloud_filtered);
    seg.segment (*inliers, *coefficients);//inliers为分割后的索引
    std::cout<<"这次分割后的点云大小: "<<inliers->indices.size()<<std::endl;
    if (inliers->indices.size () == 0)
    {
      std::cerr << "Could not estimate a planar model for the given dataset." << std::endl;
      break;
    }

    // Extract the inliers
    extract.setInputCloud (cloud_filtered);
    extract.setIndices (inliers);//提取的点云索引inliers中的点
    extract.setNegative (false);//提取范围内的点,所以为false
    extract.filter (*cloud_p);
    std::cerr << "PointCloud representing the planar component: " << cloud_p->width * cloud_p->height << " data points." << std::endl;

    std::stringstream ss;
    ss << "../../pcd/table_scene_lms400_plane_" << i << ".pcd";
    writer.write<pcl::PointXYZ> (ss.str (), *cloud_p, false);

    // Create the filtering object
    extract.setNegative (true);//提取范围外的点
    extract.filter (*cloud_f);
    cloud_filtered.swap (cloud_f);//交换,所以第二次的点云会与第一次不会有交集
    i++;
  }

  return (0);
}

输出结果如下:

PointCloud before filtering: 460400 data points.
PointCloud after filtering: 41049 data points.
PointCloud representing the planar component: 20161 data points.
PointCloud representing the planar component: 12114 data points.

原始点云图像如下:
【点云处理技术之PCL】滤波器——提取索引的点云(pcl::ExtractIndices)_第1张图片
下采样后点云图像如下:
【点云处理技术之PCL】滤波器——提取索引的点云(pcl::ExtractIndices)_第2张图片
第一次分割:
【点云处理技术之PCL】滤波器——提取索引的点云(pcl::ExtractIndices)_第3张图片
第二次分割(使用了点云交换):
【点云处理技术之PCL】滤波器——提取索引的点云(pcl::ExtractIndices)_第4张图片


参考:https://pcl.readthedocs.io/projects/tutorials/en/latest/extract_indices.html#extract-indices

你可能感兴趣的:(PCL,pcl,点云处理,提取索引的点云,ExtractIndices)