PCL之统计异常值去除滤波器--StatisticalOutlierRemoval

作用:主要用于剔除离群点,或则测量误差导致的粗差点.

原理:对每一个点的邻域进行一个统计分析,计算它到所有临近点的平均距离。假设得到的结果是一个高斯分布,其形状是由均值和标准差决定,那么平均距离在标准范围(由全局距离平均值和方差定义)之外的点,可以被定义为离群点并从数据中去除。

代码展示

#include <iostream>
#include <pcl/point_types.h>
#include <pcl/point_cloud.h>
#include <pcl/io/pcd_io.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/filters/statistical_outlier_removal.h>

using namespace std;

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>);
	//-------------------------------读取点云文件---------------------------------------------------
// 点云文件地址:https://github.com/PointCloudLibrary/data/blob/master/tutorials/table_scene_lms400.pcd
	if (pcl::io::loadPCDFile("table_scene_lms400.pcd", *cloud) == -1)
	{
		cout << "COULD NOT READ FILE table_scene_lms400.pcd \n";
		system("pause");
		return (-1);
	}
	cout << "points size is:" << cloud->size() << endl;

	// 定义滤波器
	pcl::StatisticalOutlierRemoval<pcl::PointXYZ> sor;
	sor.setInputCloud(cloud);
	sor.setMeanK(50);   //设置在进行统计时考虑查询点邻近点数
	sor.setStddevMulThresh(1.0);   //设置判断是否为离群点的阈值,如果一个点的距离超出平均距离一个标准差以上,则该点被标记为离群点,并将被移除。
	sor.filter(*cloud_filtered);
	//---------------------------------显示---------------------------------------------------------------------
	pcl::visualization::PCLVisualizer viewer("3D viewer");

	int v1(0);
	viewer.createViewPort(0.0, 0.0, 0.5, 1.0, v1);//xmin, ymin, xmax, ymax,取值范围0-1
	viewer.setBackgroundColor(0, 0, 0, v1);
	pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> green0(cloud, 0, 225, 0);
	viewer.addPointCloud(cloud, green0, "cloud", v1);

	int v2(0);
	viewer.createViewPort(0.5, 0.0, 1.0, 1.0, v2);
	viewer.setBackgroundColor(0.3, 0.3, 0.3, v2);
	pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> green1(cloud_filtered, 0, 225, 0);
	viewer.addPointCloud(cloud_filtered, green1, "cloud_filtered", v2);

	while (!viewer.wasStopped()) {
		viewer.spinOnce(100);
	}
	system("pause");
	return 0;
}

效果展示

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Sure! Here's an example code snippet for using the `StatisticalOutlierRemoval` filter in PCL: ```cpp #include <iostream> #include <pcl/io/pcd_io.h> #include <pcl/filters/statistical_outlier_removal.h> int main(int argc, char** argv) { pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>); if (pcl::io::loadPCDFile<pcl::PointXYZ>(argv[1], *cloud) == -1) { PCL_ERROR("Couldn't read file\n"); return (-1); } pcl::StatisticalOutlierRemoval<pcl::PointXYZ> sor; sor.setInputCloud(cloud); sor.setMeanK(50); // number of points to use for mean distance calculation sor.setStddevMulThresh(1.0); // standard deviation multiplier threshold sor.filter(*cloud); pcl::io::savePCDFileASCII("output.pcd", *cloud); std::cout << "Filtered cloud saved as output.pcd\n"; return 0; } ``` In this example, we first load a point cloud from a PCD file using `pcl::io::loadPCDFile`. Then we create a `StatisticalOutlierRemoval` object and set the input cloud using `setInputCloud`. We also set two parameters: `setMeanK` sets the number of nearest neighbors to use for mean distance calculation, and `setStddevMulThresh` sets the threshold for removing outliers based on standard deviation. Finally, we call `filter` to apply the filter to the input cloud and save the filtered cloud to a new PCD file using `pcl::io::savePCDFileASCII`. Note that `StatisticalOutlierRemoval` works by computing the mean distance of each point to its k nearest neighbors, and then removing any points whose distance is greater than a certain number of standard deviations from the mean. By adjusting the values of `setMeanK` and `setStddevMulThresh`, you can control the sensitivity of the filter to outliers.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值