两种点云类型,均可以使用StatisticalOutlierRemoval进行滤波:
StatisticalOutlierRemoval滤波原理:对每个点的邻域进行一个统计分析,并修剪掉那些不符合一定标准的点。稀疏离群点移除方法基于在输入数据中对点到邻近点的距离分布的计算。对每个点,我们计算其到所有邻近点的平均距离。假设得到的结果是一个高斯分布,其形状由均值和标准差决定,平均距离在标准范围(由全局距离平均值和方差定义)之外的点,可被定义为离群点并从数据集中去除掉。
一、PointXYZ类型
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_filtered(new pcl::PointCloud<pcl::PointXYZ>);
pcl::StatisticalOutlierRemoval<pcl::PointXYZ> outrem_(true);
outrem_.setInputCloud(cloud);
outrem_.setMeanK(50);//对每个点分析的邻近点个数设为50
outrem_.setStddevMulThresh(1.0);//标准差倍数设为1
outrem_.filter(*cloud_filtered);//注意前面加“*”
注意,需要给输入的cloud赋值:
cloud->width = pointcloud_all.size();
cloud->height = 1;
cloud->is_dense = true;
for (int k = 0; k < pointcloud_all.size(); k++)
{
pcl::PointXYZ p;
p.x = pointcloud_all[k].x;
p.y = pointcloud_all[k].y;
p.z = pointcloud_all[k].z;
cloud->points.push_back(p);
}
二、PCLPointCloud2类型
pcl::PCLPointCloud2::Ptr cloud_blob(new pcl::PCLPointCloud2);
【为cloud_blob赋值:不能直接给PCLPointCloud2类型的点云赋值,需要将已赋值的PointXYZ转为PCLPointCloud2:
pcl::toPCLPointCloud2(*cloud, *cloud_blob);】
pcl::PCLPointCloud2 filtered_pointcloud2;
pcl::StatisticalOutlierRemoval<pcl::PCLPointCloud2> outrem2;
outrem2.setInputCloud(cloud_blob);
outrem2.setMeanK(50);
outrem2.setStddevMulThresh(1.0);
outrem2.filter(filtered_pointcloud2);
三、两种类型之间的转换函数
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
pcl::PCLPointCloud2::Ptr cloud_blob(new pcl::PCLPointCloud2);
1)PointXYZ->PCLPointCloud2
pcl::toPCLPointCloud2(*cloud, *cloud_blob);
2)PCLPointCloud2->PointXYZ
pcl::fromPCLPointCloud2(*cloud_blob, *cloud);
注意,前面加“*”。