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)文章
C語(yǔ)言中的結(jié)構(gòu)體的入門學(xué)習(xí)教程
這篇文章主要介紹了C語(yǔ)言中的結(jié)構(gòu)體的入門學(xué)習(xí)教程,以struct語(yǔ)句定義的結(jié)構(gòu)體是C語(yǔ)言編程中的重要基礎(chǔ),需要的朋友可以參考下2015-12-12
C語(yǔ)言簡(jiǎn)單實(shí)現(xiàn)銀行ATM存取款功能
這個(gè)是大一時(shí)期寫(xiě)的。大四的時(shí)候整理了一下(本人C語(yǔ)言學(xué)的也不太好)??隙ê芏嗖蛔愫痛嬖诼┒吹牡胤?、僅供借鑒、僅供借鑒,代碼中有大量注釋,新手看起來(lái)也沒(méi)有困難2021-11-11
C++類和對(duì)象實(shí)戰(zhàn)之Date類的實(shí)現(xiàn)方法
C++ 標(biāo)準(zhǔn)庫(kù)沒(méi)有提供所謂的日期類型,C++ 繼承了C語(yǔ)言用于日期和時(shí)間操作的結(jié)構(gòu)和函數(shù),這篇文章主要給大家介紹了C++類和對(duì)象實(shí)戰(zhàn)之Date類的實(shí)現(xiàn)方法,需要的朋友可以參考下2021-12-12
C++之std::vector刪除元素的幾種方式及區(qū)別說(shuō)明
這篇文章主要介紹了C++之std::vector刪除元素的幾種方式及區(qū)別說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08
C語(yǔ)言從基礎(chǔ)到進(jìn)階全面講解數(shù)組
數(shù)組是一組有序的數(shù)據(jù)的集合,數(shù)組中元素類型相同,由數(shù)組名和下標(biāo)唯一地確定,數(shù)組中數(shù)據(jù)不僅數(shù)據(jù)類型相同,而且在計(jì)算機(jī)內(nèi)存里連續(xù)存放,地址編號(hào)最低的存儲(chǔ)單元存放數(shù)組的起始元素,地址編號(hào)最高的存儲(chǔ)單元存放數(shù)組的最后一個(gè)元素2022-05-05
C++?電話號(hào)碼的字母組合功能實(shí)現(xiàn)
這篇文章主要介紹了C++?電話號(hào)碼的字母組合,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-08-08

