PCL 点云添加随机噪声并保存
发布时间
阅读量:
阅读量
目录
一、概述
1.1原理
1.2实现步骤
1.3应用场景
二、代码实现
2.1关键函数
2.1.1 添加随机噪声实现
2.1.2 可视化函数
2.2完整代码
三、实现效果
PCL点云算法汇总及实战案例汇总的目录地址链接:
一、概述
通过给点云系统施加随机噪声是一种常见的数据增强手段,用于模拟传感器测量时产生的误差。与高斯噪声不同,在某些情况下需要具体说明:具体而言,在某个范围内生成均匀分布的随机数,并将其添加到点云坐标的各个维度上。这样便于对算法进行鲁棒性评估。
1.1原理
该系统中的随机噪声信号具有均匀分布特性,在其定义区间内能够等概率地取样数值,并将其叠加至点云坐标系中实现数据干扰处理功能。数学表达式如下所示:
𝑥_{noisy} = 𝑥 + \text{random}(𝑎,𝑏)
其中\text{random}(𝑎,𝑏)表示从区间[a,b]内抽取的均匀分布随机数。
1.2实现步骤
- 获取原始完整的三维空间信息。
- 对三维空间中的每一个数据样本加入随机扰动。
- 存储经过噪声处理后的完整数据集。
- 对比呈现原始与去噪版本的数据特征。
1.3应用场景
- 验证算法抗干扰能力: 通过评估点云处理算法在噪声环境下的性能来验证其抗干扰能力。
- 模拟传感器测量误差: 随机干扰可被用来模拟传感器测量误差。
- 数据增强策略: 通过增加点云数据的多样性来提升机器学习模型的泛化能力。
二、代码实现
2.1关键函数
2.1.1 添加随机噪声实现
通过为每个点的坐标添加随机噪声,生成带有随机噪声的点云。
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <random>
// 添加随机噪声函数
pcl::PointCloud<pcl::PointXYZ>::Ptr addRandomNoise(
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud, // 输入点云
float noise_min, // 噪声范围的最小值
float noise_max // 噪声范围的最大值
)
{
// 随机数生成器
std::default_random_engine generator;
std::uniform_real_distribution<float> distribution(noise_min, noise_max); // 均匀分布
// 创建带有噪声的点云
pcl::PointCloud<pcl::PointXYZ>::Ptr noisy_cloud(new pcl::PointCloud<pcl::PointXYZ>(*cloud)); // 拷贝原始点云
for (auto& point : noisy_cloud->points)
{
point.x += distribution(generator); // 为x坐标添加随机噪声
point.y += distribution(generator); // 为y坐标添加随机噪声
point.z += distribution(generator); // 为z坐标添加随机噪声
}
return noisy_cloud; // 返回带噪声的点云
}
代码解读
2.1.2 可视化函数
使用 PCL 可视化库展示原始点云和添加噪声后的点云。
#include <pcl/visualization/pcl_visualizer.h>
// 可视化原始点云和添加噪声后的点云
void visualizePointClouds(
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud, // 原始点云
pcl::PointCloud<pcl::PointXYZ>::Ptr noisy_cloud // 添加噪声后的点云
)
{
// 创建可视化器
pcl::visualization::PCLVisualizer::Ptr viewer(new pcl::visualization::PCLVisualizer("Noisy Point Cloud 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("Original Point Cloud", 10, 10, "vp1_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("Noisy Point Cloud", 10, 10, "vp2_text", vp_2); // 添加标题
// 设置带噪声点云的颜色为绿色
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> noisy_cloud_color_handler(noisy_cloud, 0, 255, 0); // 绿色
viewer->addPointCloud<pcl::PointXYZ>(noisy_cloud, noisy_cloud_color_handler, "noisy_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, "noisy_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/visualization/pcl_visualizer.h>
#include <random> // 随机数生成
// 添加随机噪声函数
pcl::PointCloud<pcl::PointXYZ>::Ptr addRandomNoise(
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud, // 输入点云
float noise_min, // 噪声范围的最小值
float noise_max // 噪声范围的最大值
)
{
// 随机数生成器
std::default_random_engine generator;
std::uniform_real_distribution<float> distribution(noise_min, noise_max); // 均匀分布
// 创建带有噪声的点云
pcl::PointCloud<pcl::PointXYZ>::Ptr noisy_cloud(new pcl::PointCloud<pcl::PointXYZ>(*cloud)); // 拷贝原始点云
for (auto& point : noisy_cloud->points)
{
point.x += distribution(generator); // 为x坐标添加随机噪声
point.y += distribution(generator); // 为y坐标添加随机噪声
point.z += distribution(generator); // 为z坐标添加随机噪声
}
return noisy_cloud; // 返回带噪声的点云
}
// 可视化原始点云和添加噪声后的点云
void visualizePointClouds(
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud, // 原始点云
pcl::PointCloud<pcl::PointXYZ>::Ptr noisy_cloud // 添加噪声后的点云
)
{
// 创建可视化器
pcl::visualization::PCLVisualizer::Ptr viewer(new pcl::visualization::PCLVisualizer("Noisy Point Cloud 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("Original Point Cloud", 10, 10, "vp1_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("Noisy Point Cloud", 10, 10, "vp2_text", vp_2); // 添加标题
// 设置带噪声点云的颜色为绿色
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> noisy_cloud_color_handler(noisy_cloud, 0, 255, 0); // 绿色
viewer->addPointCloud<pcl::PointXYZ>(noisy_cloud, noisy_cloud_color_handler, "noisy_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, "noisy_cloud", vp_2);
// 启动可视化循环
while (!viewer->wasStopped())
{
viewer->spinOnce(100); // 刷新可视化器
}
}
// 保存噪声点云到文件
void saveNoisyPointCloud(pcl::PointCloud<pcl::PointXYZ>::Ptr noisy_cloud, const std::string& filename)
{
pcl::io::savePCDFileBinary(filename, *noisy_cloud); // 保存为二进制 PCD 格式
std::cout << "带噪声的点云已保存至: " << filename << std::endl;
}
int main(int argc, char** argv)
{
// ------------------------------读取点云数据---------------------------------
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
if (pcl::io::loadPCDFile("bunny.pcd", *cloud) < 0)
{
PCL_ERROR("Could not read file\n");
return (-1); // 返回错误
}
// -------------------------------添加随机噪声---------------------------------
float noise_min = -0.01f; // 噪声最小值
float noise_max = 0.01f; // 噪声最大值
pcl::PointCloud<pcl::PointXYZ>::Ptr noisy_cloud = addRandomNoise(cloud, noise_min, noise_max); // 添加噪声
// -------------------------------保存噪声点云---------------------------------
//saveNoisyPointCloud(noisy_cloud, "random_noisy_bunny.pcd"); // 保存带噪声的点云
// ------------------------------可视化原始点云和带噪声的点云---------------------------------
visualizePointClouds(cloud, noisy_cloud); // 调用可视化函数
return 0;
}
代码解读
三、实现效果

全部评论 (0)
还没有任何评论哟~
