官方教程 http://pointclouds.org/documentation/tutorials/statistical_outlier.php#statistical-outlier-re
离群点是按照K个近邻点的标准方差*Threshold 来定义的,假如K=50,*Threshold =1. 那么某一个点是否是离群点。是这么确定的。首先求出这个点附近的50个点之间距离的标准方差dev,然后计算这个点到这些点的距离d,如果d>dev*threshold 那么这个点就是离群点。
附上源代码。在官方教程上加了一点点注释,并且把点云显示了一下,红色的是离群点。 点云文件青岛上面的链接下载
#include <iostream>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/filters/statistical_outlier_removal.h>
#include <pcl/visualization/cloud_viewer.h>
#include <pcl/visualization/pcl_visualizer.h>
int main(int argc, char** argv)
{
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_inliner(new pcl::PointCloud<pcl::PointXYZ>);
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_outliner(new pcl::PointCloud<pcl::PointXYZ>);
// Fill in the cloud data
pcl::PCDReader reader;
// Replace the path below with the path where you saved your file
reader.read<pcl::PointXYZ>("table_scene_lms400.pcd", *cloud);
std::cerr << "Cloud before filtering: " << std::endl;
std::cerr << *cloud << std::endl;
// Create the filtering object
pcl::StatisticalOutlierRemoval<pcl::PointXYZ> sor;
sor.setInputCloud(cloud);
sor.setMeanK(50);//50个临近点
sor.setStddevMulThresh(1.0);//距离大于1倍标准方差
sor.filter(*cloud_inliner);
std::cerr << "Cloud after filtering: " << std::endl;
std::cerr << *cloud_inliner << std::endl;
pcl::PCDWriter writer;
writer.write<pcl::PointXYZ>("table_scene_lms400_inliers.pcd", *cloud_inliner, false);
sor.setNegative(true);
sor.filter(*cloud_outliner);
writer.write<pcl::PointXYZ>("table_scene_lms400_outliers.pcd", *cloud_outliner, false);
pcl::visualization::PCLVisualizer viewer("demo");
int v1(0);
int v2(1);
viewer.createViewPort(0.0, 0.0, 1.0, 1.0, v1);
// The color we will be using
float bckgr_gray_level = 0.0; // Black
float txt_gray_lvl = 1.0 - bckgr_gray_level;
// Original point cloud is white
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> cloud_in_color_h(cloud, (int)255 * txt_gray_lvl, (int)255 * txt_gray_lvl, (int)255 * txt_gray_lvl);
viewer.addPointCloud(cloud, cloud_in_color_h, "cloud_in_v1", v1); //viewer.addPointCloud(cloud_in, cloud_in_color_h, "cloud_in_v2", v2);
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> cloud_out_green(cloud_inliner, 20, 180, 20);
viewer.addPointCloud(cloud_inliner, cloud_out_green, "cloud_out", v1);
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> Final_red(cloud_outliner, 180, 10, 20);
viewer.addPointCloud(cloud_outliner, Final_red, "cloud_Final", v1);
viewer.setBackgroundColor(bckgr_gray_level, bckgr_gray_level, bckgr_gray_level, v1);
// viewer.setBackgroundColor(bckgr_gray_level, bckgr_gray_level, bckgr_gray_level, v2);
viewer.setSize(1280, 1024); // Visualiser window size
//viewer.showCloud(cloud_out);
while (!viewer.wasStopped())
{
viewer.spinOnce();
}
return (0);
}