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

Posted 非晚非晚

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了点云处理技术之PCL滤波器——提取索引的点云(pcl::ExtractIndices)相关的知识,希望对你有一定的参考价值。

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

#include <iostream>
#include <pcl/ModelCoefficients.h>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/sample_consensus/method_types.h>
#include <pcl/sample_consensus/model_types.h>
#include <pcl/segmentation/sac_segmentation.h>
#include <pcl/filters/voxel_grid.h>
#include <pcl/filters/extract_indices.h>

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.

原始点云图像如下:

下采样后点云图像如下:

第一次分割:

第二次分割(使用了点云交换):


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

以上是关于点云处理技术之PCL滤波器——提取索引的点云(pcl::ExtractIndices)的主要内容,如果未能解决你的问题,请参考以下文章

点云处理技术之PCL滤波器——直通滤波器(pcl::PassThrough)

点云滤波相关

点云处理技术之PCL滤波器——体素滤波器(pcl::VoxelGrid)

点云处理技术之PCL滤波器——离群点滤波(statisticalOutlierRemovalConditionalRemoval 和RadiusOutlinerRemoval)

点云处理技术之PCL点云分割算法1——平面模型分割圆柱模型分割和欧式聚类提取(含欧式聚类原理)

【PCL】VoxelGrid体素化网格滤波器