代码
#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噪声后(未加法线):