C++實(shí)現(xiàn)點(diǎn)云添加高斯噪聲功能
0 添加高斯噪聲后的點(diǎn)云
紅色為添加的高斯噪聲點(diǎn),白色為原始點(diǎn)

1 什么是高斯噪聲
高斯噪聲是指它的概率密度函數(shù)服從高斯分布(即正態(tài)分布)的一類噪聲。(百度百科)
高斯分布,也稱正態(tài)分布,又稱常態(tài)分布,記為 N ( μ , σ 2 ) ),其中 μ , σ 2 為分布的參數(shù),分別為高斯分布的期望和方差,其中 σ > 0 ,稱為標(biāo)準(zhǔn)差。當(dāng) μ , σ 有確定值時(shí),p ( x ) 也就確定了,特別當(dāng) μ = 0 , σ 2 = 1時(shí),x 的分布為標(biāo)準(zhǔn)正態(tài)分布。

高斯分布函數(shù)
2 怎樣添加高斯噪聲
磨刀不誤砍柴工,將添加高斯噪聲封裝到 CreatGaussNoise類 中,只需在main.cpp中設(shè)置輸入點(diǎn)云(要添加噪聲的點(diǎn)云)、設(shè)置高斯噪聲參數(shù)(μ , σ)即可。
實(shí)現(xiàn)代碼
main.cpp
#include "add_gauss_noise.h"
int main()
{
//-------------------加載點(diǎn)云-------------------
pcl::PointCloud<pcl::PointXYZ> cloud_in;
if (pcl::io::loadPCDFile("Armadillo.pcd", cloud_in) < 0)
{
PCL_ERROR("->點(diǎn)云文件不存在!\a\n");
system("pause");
return -1;
}
//-------------------添加高斯噪聲-------------------
AddGaussNoise agn; //創(chuàng)建高斯噪聲對(duì)象agn
pcl::PointCloud<pcl::PointXYZ> cloud_out; //保存結(jié)果的點(diǎn)云
agn.setInputCloud(cloud_in); //設(shè)置輸入點(diǎn)云
agn.setParameters(0,2); //設(shè)置高斯噪聲參數(shù)mu,sigma
agn.addGaussNoise(cloud_out); //執(zhí)行添加高斯噪聲,并將結(jié)果保存在cloud_out中
//-------------------保存添加高斯噪聲后的點(diǎn)云-------------------
pcl::io::savePCDFileBinary("addGaussNoise.pcd", cloud_out);
return 0;
}
add_gauss_noise.h
#pragma once
#include <iostream>
#include <pcl/io/pcd_io.h>
#include <boost/random.hpp> //隨機(jī)數(shù)所需頭文件
class AddGaussNoise
{
public:
/**
* @brief : 設(shè)置輸入點(diǎn)云
* @param[I]: cloud_in (輸入點(diǎn)云)
* @param[O]: none
* @return : none
* @note :
**/
void setInputCloud(pcl::PointCloud<pcl::PointXYZ> &cloud_in);
/**
* @brief : 設(shè)置高斯噪聲參數(shù)
* @param[I]: mu (均值,默認(rèn)0)
* @param[I]: sigma (標(biāo)準(zhǔn)差,默認(rèn)1)
* @param[O]: none
* @return : none
* @note :
**/
void setParameters(double mu = 0.0, double sigma = 1.0);
/**
* @brief : 執(zhí)行添加高斯噪聲
* @param[I]: cloud_out (添加高斯噪聲后的點(diǎn)云)
* @param[O]: none
* @return : none
* @note :
**/
void addGaussNoise(pcl::PointCloud<pcl::PointXYZ> &cloud_out);
private:
pcl::PointCloud<pcl::PointXYZ> m_cloud_in; //輸入點(diǎn)云
bool is_setInputCloud = false; //是否設(shè)置輸入點(diǎn)云
double m_mu, m_sigma; //高斯分布參數(shù)
bool is_setParameters = false; //是否設(shè)置高斯分布參數(shù)
};
add_gauss_noise.cpp
#include "add_gauss_noise.h"
/**
* @brief : 設(shè)置輸入點(diǎn)云
* @param[I]: cloud_in (輸入點(diǎn)云)
* @param[O]: none
* @return : none
* @note :
**/
void AddGaussNoise::setInputCloud(pcl::PointCloud<pcl::PointXYZ> &cloud_in)
{
m_cloud_in = cloud_in;
is_setInputCloud = true;
}
/**
* @brief : 設(shè)置高斯噪聲參數(shù)
* @param[I]: mu (均值,默認(rèn)0)
* @param[I]: sigma (標(biāo)準(zhǔn)差,默認(rèn)1)
* @param[O]: none
* @return : none
* @note :
**/
void AddGaussNoise::setParameters(double mu, double sigma)
{
if (sigma > 0)
{
m_mu = mu;
m_sigma = sigma;
is_setParameters = true;
}
else
{
PCL_ERROR("->sigma應(yīng)大于0!\a\n");
system("pause");
abort();
}
}
/**
* @brief : 執(zhí)行添加高斯噪聲
* @param[I]: cloud_out (添加高斯噪聲后的點(diǎn)云)
* @param[O]: none
* @return : none
* @note :
**/
void AddGaussNoise::addGaussNoise(pcl::PointCloud<pcl::PointXYZ> &cloud_out)
{
boost::mt19937 zgy; //等分布均勻偽隨機(jī)數(shù)發(fā)生器
zgy.seed(static_cast<unsigned int>(time(0))); //隨機(jī)種子
boost::normal_distribution<> nd(m_mu, m_sigma); //定義正態(tài)分布,均值為mu,標(biāo)準(zhǔn)差為sigma
boost::variate_generator<boost::mt19937&, boost::normal_distribution<>> gauss_noise(zgy, nd); //生成高斯噪聲
pcl::PointCloud<pcl::PointXYZ> cloud_gauss; //聲明高斯噪聲點(diǎn)云
cloud_gauss = m_cloud_in; //將原始點(diǎn)云拷貝給高斯噪聲點(diǎn)云,用于下面的平移
for (size_t i = 0; i < cloud_gauss.size(); i++)
{
cloud_gauss.points[i].x += static_cast<float> (gauss_noise());
cloud_gauss.points[i].y += static_cast<float> (gauss_noise());
cloud_gauss.points[i].z += static_cast<float> (gauss_noise());
}
cloud_out = m_cloud_in + cloud_gauss; //將原始點(diǎn)云與噪聲點(diǎn)云合并,得到添加高斯噪聲后的點(diǎn)云
}
參考鏈接
C++ normal_distribution高斯正態(tài)分布函數(shù)用法詳解
總結(jié)
到此這篇關(guān)于C++實(shí)現(xiàn)點(diǎn)云添加高斯噪聲功能的文章就介紹到這了,更多相關(guān)C++點(diǎn)云高斯噪聲內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
VC++?2019?"const?char*"類型的實(shí)參與"LPCTSTR"
這篇文章主要給大家介紹了關(guān)于VC++?2019?"const?char*"類型的實(shí)參與"LPCTSTR"類型的形參不兼容的解決方法,文中通過(guò)圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2023-03-03
C++中volatile和mutable關(guān)鍵字用法詳解
這篇文章主要介紹了C++中volatile和mutable關(guān)鍵字用法詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02
C語(yǔ)言實(shí)現(xiàn)貪吃蛇游戲(單人版)
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)貪吃蛇游戲單人版,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-06-06
C語(yǔ)言實(shí)現(xiàn)隨機(jī)發(fā)撲克牌
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)隨機(jī)發(fā)撲克牌,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-04-04
C語(yǔ)言小項(xiàng)目計(jì)時(shí)器的實(shí)現(xiàn)思路(倒計(jì)時(shí)+報(bào)警提示)
這篇文章主要介紹了C語(yǔ)言小項(xiàng)目計(jì)時(shí)器(倒計(jì)時(shí)+報(bào)警提示)的實(shí)現(xiàn)思路,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-11-11
Qt使用SqlLite實(shí)現(xiàn)權(quán)限管理的示例代碼
本文主要介紹了Qt使用SqlLite實(shí)現(xiàn)權(quán)限管理的示例代碼,管理員針對(duì)不同人員進(jìn)行權(quán)限設(shè)定,具有一定的參考價(jià)值,感興趣的可以了解一下2023-09-09

