Add Gaussian noise to a picture

I.To generate Gaussian noise

Box-Muller:if random variable x and y are respectively independent generated from a uniform distribution with [0,1].we can use the formula below to produce a variable with Gaussian distribution.

 z=sqrt(-2*lny)*cos(2*pi*x)

function:

double generateGaussianNoise(double mu, double sigma)
{
const double epsilon = std::numeric_limits<double>::min();
const double two_pi = 2.0*3.14159265358979323846;

static double z0, z1;
static bool generate;
generate = !generate;

if (!generate)
return z1 * sigma + mu;

double u1, u2;
do
{
u1 = rand() * (1.0 / RAND_MAX);
u2 = rand() * (1.0 / RAND_MAX);
} while (u1 <= epsilon);

z0 = sqrt(-2.0 * log(u1)) * cos(two_pi * u2);
z1 = sqrt(-2.0 * log(u1)) * sin(two_pi * u2);
return z0 * sigma + mu;
}

II.Add Gaussian noise to a picture

Mat AddGaussianNoise(const Mat& I, double mu, double sigma)
{
// accept only char type matrices
CV_Assert(I.depth() != sizeof(uchar));

int channels = I.channels();

int nRows = I.rows;
int nCols = I.cols * channels;

if (I.isContinuous()) {
nCols *= nRows;
nRows = 1;
}

Mat result = I.clone();

int i, j;
const uchar* p;
uchar* rp;
for (i = 0; i < nRows; ++i) {
p = I.ptr<uchar>(i);
rp = result.ptr<uchar>(i);
for (j = 0; j < nCols; ++j) {
double val = p[j] + generateGaussianNoise(mu, sigma);
if (val < 0)
val = 0;
if (val > 255)
val = 255;
rp[j] = (uchar)val;
}
}
return result;
}

 

转载于:https://www.cnblogs.com/ssMellon/p/6618071.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值