对点云添加高斯噪声并保存到本地(PCL 附C++代码)

代码

#include <stdio.h>
#include <stdlib.h>
#include <pcl/point_types.h>
#include <iostream>
#include <pcl/io/ply_io.h>
#include <pcl/io/pcd_io.h>
#include <pcl/features/normal_3d.h>
#include <pcl/visualization/cloud_viewer.h>
#include <boost/random.hpp>
using namespace std;


void AddNoise()
{
	string cleanCloud;
	pcl::PointCloud<pcl::PointXYZRGB>::Ptr cc(new pcl::PointCloud<pcl::PointXYZRGB>);
	//pcl::PointCloud<pcl::Normal>::Ptr cloudNormals(new pcl::PointCloud<pcl::Normal>);
	cout << "input filename of clean cloud: ";
	cin >> cleanCloud;
	
	pcl::PLYReader reader;
	reader.read(cleanCloud+".ply", *cc);
	//reader.read(cleanCloud, *cloudNormals);

	float sigmaGaussian;
	cout << "input the noise level need to be added: ";
	cin >> sigmaGaussian;

	boost::mt19937 rng; 
	rng.seed(static_cast<unsigned int>(time(0)));
	boost::normal_distribution<> nd(0, sigmaGaussian);
	boost::variate_generator<boost::mt19937&, boost::normal_distribution<>> var_nor(rng, nd);

	for (size_t i = 0; i < cc->points.size(); i++)
	{
		cc->points[i].x = cc->points[i].x + static_cast<float> (var_nor());
		cc->points[i].y = cc->points[i].y + static_cast<float> (var_nor());
		cc->points[i].z = cc->points[i].z + static_cast<float> (var_nor());
		cc->at(i).r = 150;
		cc->at(i).g = 150;
		cc->at(i).b = 150;
	}

	ofstream fout("NoisyFandisk-"+std::to_string(sigmaGaussian) + ".ply");
	fout << "ply" << endl << "format ascii 1.0" << endl
		<< "element vertex " + std::to_string(cc->points.size()) << endl
		<< "property float x" << endl << "property float y" << endl << "property float z" << endl
		<< "property uchar red" << endl << "property uchar green" << endl << "property uchar blue" << endl
		<< "end_header" << endl;
	for (int i = 0; i < cc->points.size(); i++)
	{
		fout << cc->points[i].x
			<< " " << cc->points[i].y
			<< " " << cc->points[i].z
			<< " " << std::to_string(130) << " " << std::to_string(130) << " " << std::to_string(130)
			<< endl;
	}
	fout.close();

	pcl::visualization::CloudViewer viewer("view");
	viewer.showCloud(cc);
	system("PAUSE");

}

int main()
{
	AddNoise();
	return 0;
}

结果

添加 σ = 0.08 \sigma=0.08 σ=0.08噪声前:
在这里插入图片描述
添加 σ = 0.08 \sigma=0.08 σ=0.08噪声后(未加法线):
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值