目录
一、概述
1.1原理
1.2实现步骤
1.3应用场景
二、代码实现
2.1关键函数
2.1.1 半径滤波实现
2.1.2 可视化函数
2.2完整代码
三、实现效果
PCL点云算法汇总及实战案例汇总的目录地址链接:
PCL点云算法与项目实战案例汇总(长期更新)
一、概述
半径滤波(Radius Outlier Removal) 是一种点云滤波技术,主要用于去除孤立点或稀疏点。半径滤波通过检查每个点的邻域内的点的数量,来判断该点是否保留。如果某个点在给定半径内的邻居点数量低于用户定义的阈值,则该点会被认为是离群点并被删除。
1.1原理
半径滤波的基本思想是:对于点云中的每个点,检查在一定半径内的邻居点数量。假如某点的邻居数量低于预定阈值,则认为该点为噪声点或异常点,将其剔除。半径和邻居数量这两个参数决定了滤波的效果。
1.2实现步骤
- 读取点云数据。
- 设置半径滤波的参数,包括搜索半径和最小邻居数。
- 应用半径滤波,生成过滤后的点云。
- 可视化原始点云和过滤后的点云。
1.3应用场景
- 去除噪声点:在点云数据采集中,由于传感器噪声、环境干扰等原因,可能会产生一些离群点,半径滤波可以有效去除这些噪声点。
- 稀疏点云处理:在处理稀疏点云时,半径滤波可以剔除那些孤立存在的点。
- 数据预处理:为点云的其他处理任务提供更干净的输入数据。
二、代码实现
2.1关键函数
2.1.1 半径滤波实现
通过设置邻域半径和最小邻居数量,应用半径滤波。
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/filters/radius_outlier_removal.h>
// 半径滤波函数
pcl::PointCloud<pcl::PointXYZ>::Ptr radiusOutlierRemoval(
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud, // 输入点云
float radius, // 半径大小
int min_neighbors // 最小邻居数量
)
{
// 创建半径滤波对象,并设置参数
pcl::RadiusOutlierRemoval<pcl::PointXYZ> outrem;
outrem.setInputCloud(cloud); // 设置输入点云
outrem.setRadiusSearch(radius); // 设置半径
outrem.setMinNeighborsInRadius(min_neighbors); // 设置最小邻居数
// 滤波后的点云
pcl::PointCloud<pcl::PointXYZ>::Ptr filtered_cloud(new pcl::PointCloud<pcl::PointXYZ>);
outrem.filter(*filtered_cloud); // 应用滤波
return filtered_cloud; // 返回过滤后的点云
}
2.1.2 可视化函数
使用 PCL 可视化库展示原始点云和半径滤波后的点云。
#include <pcl/visualization/pcl_visualizer.h>
// 可视化原始点云和过滤后的点云
void visualizePointClouds(
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud, // 原始点云
pcl::PointCloud<pcl::PointXYZ>::Ptr filtered_cloud // 过滤后的点云
)
{
// 创建可视化器
pcl::visualization::PCLVisualizer::Ptr viewer(new pcl::visualization::PCLVisualizer("Radius Outlier Removal Viewer"));
// 创建视口1,显示原始点云
int vp_1;
viewer->createViewPort(0.0, 0.0, 0.5, 1.0, vp_1); // 创建左侧窗口
viewer->setBackgroundColor(1.0, 1.0, 1.0, vp_1); // 设置白色背景
viewer->addText("Raw Point Clouds", 10, 10, "v1_text", vp_1); // 添加标题
// 设置原始点云的颜色为红色
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> cloud_color_handler(cloud, 255, 0, 0); // 红色
viewer->addPointCloud<pcl::PointXYZ>(cloud, cloud_color_handler, "original_cloud", vp_1); // 添加原始点云
// 创建视口2,显示过滤后的点云
int vp_2;
viewer->createViewPort(0.5, 0.0, 1.0, 1.0, vp_2); // 创建右侧窗口
viewer->setBackgroundColor(0.98, 0.98, 0.98, vp_2); // 设置浅灰色背景
viewer->addText("Filtered Point Clouds", 10, 10, "v2_text", vp_2); // 添加标题
// 设置过滤后的点云的颜色为绿色
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> filtered_cloud_color_handler(filtered_cloud, 0, 255, 0); // 绿色
viewer->addPointCloud<pcl::PointXYZ>(filtered_cloud, filtered_cloud_color_handler, "filtered_cloud", vp_2); // 添加过滤后的点云
// 设置点的大小(可选)
viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 2, "original_cloud", vp_1);
viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 2, "filtered_cloud", vp_2);
// 启动可视化循环
while (!viewer->wasStopped())
{
viewer->spinOnce(100); // 刷新可视化器
}
}
2.2完整代码
// C++头文件
#include <iostream>
// PCL头文件
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/filters/radius_outlier_removal.h> // 半径滤波
#include <pcl/visualization/pcl_visualizer.h>
// 半径滤波函数
pcl::PointCloud<pcl::PointXYZ>::Ptr radiusOutlierRemoval(
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud, // 输入点云
float radius, // 半径大小
int min_neighbors // 最小邻居数量
)
{
// 创建半径滤波对象,并设置参数
pcl::RadiusOutlierRemoval<pcl::PointXYZ> outrem;
outrem.setInputCloud(cloud); // 设置输入点云
outrem.setRadiusSearch(radius); // 设置半径
outrem.setMinNeighborsInRadius(min_neighbors); // 设置最小邻居数
// 滤波后的点云
pcl::PointCloud<pcl::PointXYZ>::Ptr filtered_cloud(new pcl::PointCloud<pcl::PointXYZ>);
outrem.filter(*filtered_cloud); // 应用滤波
return filtered_cloud; // 返回过滤后的点云
}
// 可视化原始点云和过滤后的点云
void visualizePointClouds(
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud, // 原始点云
pcl::PointCloud<pcl::PointXYZ>::Ptr filtered_cloud // 过滤后的点云
)
{
// 创建可视化器
pcl::visualization::PCLVisualizer::Ptr viewer(new pcl::visualization::PCLVisualizer("Radius Outlier Removal Viewer"));
// 创建视口1,显示原始点云
int vp_1;
viewer->createViewPort(0.0, 0.0, 0.5, 1.0, vp_1); // 创建左侧窗口
viewer->setBackgroundColor(1.0, 1.0, 1.0, vp_1); // 设置白色背景
viewer->addText("Raw Point Clouds", 10, 10, "v1_text", vp_1); // 添加标题
// 设置原始点云的颜色为红色
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> cloud_color_handler(cloud, 255, 0, 0); // 红色
viewer->addPointCloud<pcl::PointXYZ>(cloud, cloud_color_handler, "original_cloud", vp_1); // 添加原始点云
// 创建视口2,显示过滤后的点云
int vp_2;
viewer->createViewPort(0.5, 0.0, 1.0, 1.0, vp_2); // 创建右侧窗口
viewer->setBackgroundColor(0.98, 0.98, 0.98, vp_2); // 设置浅灰色背景
viewer->addText("Filtered Point Clouds", 10, 10, "v2_text", vp_2); // 添加标题
// 设置过滤后的点云的颜色为绿色
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> filtered_cloud_color_handler(filtered_cloud, 0, 255, 0); // 绿色
viewer->addPointCloud<pcl::PointXYZ>(filtered_cloud, filtered_cloud_color_handler, "filtered_cloud", vp_2); // 添加过滤后的点云
// 设置点的大小(可选)
viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 2, "original_cloud", vp_1);
viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 2, "filtered_cloud", vp_2);
// 启动可视化循环
while (!viewer->wasStopped())
{
viewer->spinOnce(100); // 刷新可视化器
}
}
int main(int argc, char** argv)
{
// ------------------------------读取点云数据---------------------------------
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
if (pcl::io::loadPCDFile("ro_bunny.pcd", *cloud) < 0)
{
PCL_ERROR("Could not read file\n");
return (-1); // 返回错误
}
// -------------------------------半径滤波---------------------------------
float radius = 0.002; // 设置搜索半径
int min_neighbors = 6; // 设置最小邻居数
pcl::PointCloud<pcl::PointXYZ>::Ptr filtered_cloud = radiusOutlierRemoval(cloud, radius, min_neighbors); // 应用半径滤波
// ------------------------------可视化原始点云和过滤后的点云---------------------------------
visualizePointClouds(cloud, filtered_cloud); // 调用可视化函数
return 0;
}