// addnoise.cpp : Defines the entry point for the console application.
//From GGbondg
#include "stdafx.h"
#define EXTRA_NAME "$Gaussiannoise."
#include "loadbmp.h"
#define U 20 //高斯分布的均值
#define D 30 //高斯分布的均方差
int Gaus_S(){ //产生高斯样本,以U为均值,D为均方差
double sum=0;
for(int i=0;i<12;i++) sum+=rand()/32767.00;
//计算机中rand()函数为-32767~+32767(2^15-1)
//故sum+为0~1之间的均匀随机变量
return int(U+D*(sum-6));
//产生均值为U,标准差为D的高斯分布的样本,并返回
}
void Gaussian_Noise()
{
if (lpBitmap==0) return;
int x,y,p;
srand((unsigned)time(NULL)); //种下随机种子
for(y=0;y<nHeight;y++)
{
for(x=0;x<nWidth;x++)
{
p=x*3+y*nByteWidth; //p为现在处理的象素点
//向R、G、B三个分量分别添加高斯噪声
lpBits[p+2]+=Gaus_S();
lpBits[p+1]+=Gaus_S();
lpBits[p]+=Gaus_S();
//由于lpBits为BYTE类型值为0~255,故不作后期处理
}
}
}
void main(int argc, char *argv[])
{
//FileName="d://black.bmp";
FileName="d://lena_gray.bmp";
LoadBitmap();
Gaussian_Noise();
SaveAs();
delete lpBitmap;
}