(十)使用StatisticalOutlierRemove滤波器删除异常点
以下代码实现使用StatisticalOutlierRemove去去除点云的异常点。
statistical_removal.cpp
#include <iostream>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/filters/statistical_outlier_removal.h>int main ()
{pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>);pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_filtered (new pcl::PointCloud<pcl::PointXYZ>);// 读取点云数据pcl::PCDReader reader;reader.read<pcl::PointXYZ> ("table_scene_lms400.pcd", *cloud);std::cerr << "Cloud before filtering: " << std::endl;std::cerr << *cloud << std::endl;// 创建一个pcl::StatisticalOutlierRemoval滤波器。//所有距离查询点的平均距离的标准差大于1的点都将被标记为异常值并被删除。pcl::StatisticalOutlierRemoval<pcl::PointXYZ> sor;sor.setInputCloud (cloud);sor.setMeanK (50); // 每个查询点要分析的邻居数量设置为50sor.setStddevMulThresh (1.0); // 标准差阈值设置为1sor.filter (*cloud_filtered); // 计算输出并将其存储在cloud_filtered中。std::cerr << "Cloud after filtering: " << std::endl;std::cerr << *cloud_filtered << std::endl; // 保存滤波后的点云:pcl::PCDWriter writer;writer.write<pcl::PointXYZ> ("table_scene_lms400_inliers.pcd", *cloud_filtered, false);// 保存异常点:sor.setNegative (true);sor.filter (*cloud_filtered);writer.write<pcl::PointXYZ> ("table_scene_lms400_outliers.pcd", *cloud_filtered, false);return (0);
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)project(statistical_removal)find_package(PCL 1.2 REQUIRED)include_directories(${PCL_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})add_executable (statistical_removal statistical_removal.cpp)
target_link_libraries (statistical_removal ${PCL_LIBRARIES})
编译并运行:
./statistical_removal
Cloud before filtering:
points[]: 460400
width: 460400
height: 1
is_dense: 1
sensor origin (xyz): [0, 0, 0] / orientation (xyzw): [0, 0, 0, 1]Cloud after filtering:
points[]: 451410
width: 451410
height: 1
is_dense: 1
sensor origin (xyz): [0, 0, 0] / orientation (xyzw): [0, 0, 0, 1]
红点为异常点: