PCL学习笔记(20)——remove_outliers

源码

#include <iostream>
#include <pcl/point_types.h>
#include <pcl/filters/radius_outlier_removal.h>
#include <pcl/filters/conditional_removal.h>
#include <pcl/visualization/pcl_visualizer.h>

int main (int argc, char** argv)
{
  if (argc != 2)
  {
    std::cerr << "please specify command line arg '-r' or '-c'" << std::endl;
    exit(0);
  }
  pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>);
  pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_filtered (new pcl::PointCloud<pcl::PointXYZ>);
  // 填入点云数据
  cloud->width  = 100000;
  cloud->height = 1;
  cloud->points.resize (cloud->width * cloud->height);
  for (size_t i = 0; i < cloud->points.size (); ++i)
  {
    cloud->points[i].x = 1024 * rand () / (RAND_MAX + 1.0f);
    cloud->points[i].y = 1024 * rand () / (RAND_MAX + 1.0f);
    cloud->points[i].z = 1024 * rand () / (RAND_MAX + 1.0f);
  }
  if (strcmp(argv[1], "-r") == 0){
    pcl::RadiusOutlierRemoval<pcl::PointXYZ> outrem;
    // 创建滤波器
    outrem.setInputCloud(cloud);
    //搜索半径设为8,在此半径内点必须要有至少1个邻居时,此点才会被保留
    outrem.setRadiusSearch(8);
    outrem.setMinNeighborsInRadius (2);
    // 应用滤波器
    outrem.filter (*cloud_filtered);
  }
  else if (strcmp(argv[1], "-c") == 0){
    // 创建环境
    pcl::ConditionAnd<pcl::PointXYZ>::Ptr range_cond (new
      pcl::ConditionAnd<pcl::PointXYZ> ());
      //为条件定义对象添加比较算子: 使用大于0.0和小于0.8这两个条件用于建立滤波器。
    range_cond->addComparison (pcl::FieldComparison<pcl::PointXYZ>::ConstPtr (new
      pcl::FieldComparison<pcl::PointXYZ> ("z", pcl::ComparisonOps::GT, 0.0)));
    range_cond->addComparison (pcl::FieldComparison<pcl::PointXYZ>::ConstPtr (new
      pcl::FieldComparison<pcl::PointXYZ> ("z", pcl::ComparisonOps::LT, 0.8)));
       //添加在z字段上小于0.8的比较算子
    // 创建滤波器
    pcl::ConditionalRemoval<pcl::PointXYZ> condrem (range_cond);
    condrem.setInputCloud (cloud);
    condrem.setKeepOrganized(true);
    // 应用滤波器
    condrem.filter (*cloud_filtered);
  }
  else{
    std::cerr << "please specify command line arg '-r' or '-c'" << std::endl;
    exit(0);
  }
  //visualizer
  pcl::visualization::PCLVisualizer::Ptr viewer(new pcl::visualization::PCLVisualizer);
  viewer->initCameraParameters();

  int v1(0);
  viewer->createViewPort(0, 0, 0.5, 1, v1);
  viewer->setBackgroundColor(0, 0, 0, v1);
  pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> single_color1(cloud, 255, 255, 255);
  viewer->addPointCloud(cloud, single_color1, "cloud_in", v1);

  int v2(0);
  viewer->createViewPort(0.5, 0, 1, 1, v2);
  viewer->setBackgroundColor(0, 0, 0, v2);
  pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> single_color2(cloud_filtered, 255, 255, 55);
  viewer->addPointCloud(cloud_filtered, single_color2, "cloud_out", v2);

  viewer->addCoordinateSystem();

  viewer->spin();
  //std::cerr << "Cloud before filtering: " << std::endl;
  //for (size_t i = 0; i < cloud->points.size (); ++i)
  //  std::cerr << "    " << cloud->points[i].x << " "
  //                      << cloud->points[i].y << " "
  //                      << cloud->points[i].z << std::endl;
   显示滤波后的点云
  //std::cerr << "Cloud after filtering: " << std::endl;
  //for (size_t i = 0; i < cloud_filtered->points.size (); ++i)
  //  std::cerr << "    " << cloud_filtered->points[i].x << " "
  //                      << cloud_filtered->points[i].y << " "
  //                      << cloud_filtered->points[i].z << std::endl;
  return (0);
}

PCL学习笔记(20)——remove_outliers

  • 解释
    GT 就是 GREATER THAN大于 
    GE 就是 GREATER THAN OR EQUAL 大于等于
    LT 就是 LESS THAN小于
    LE 就是 LESS THAN OR EQUAL 小于等于
    EQ 就是 EQUAL等于
    NE就是 NOT EQUAL不等于
  namespace ComparisonOps
  {
    /** \brief The kind of comparison operations that are possible within a 
      * comparison object
      */
    typedef enum
    {
      GT, GE, LT, LE, EQ
    } CompareOp;
  }
上一篇:pclpy 统计滤波


下一篇:PCL:PassThrough ❤️ 直通滤波