滤镜之高斯噪声GaussianNoise

首先看这两张效果图:



 

图像加上高斯噪声非常简单了,每个像素点叠加上高斯噪声即可。同理也可以叠加上其他的噪声。具体可以参考GIMP代码

 

算法原理:

1.     首先随机产生高斯噪声,可以是RGB同一个随机噪声,也可以是三通道不同的随机噪声。

2.     将随机噪声的值叠加到像素中,并判断是否越界。

 

该算法存在一个参数:

1.     噪声程度:越大则噪声越大,图像越脏


高斯噪声产生的代码是从The Science Of FractalImages一书中得来的,GIMP中也是采用类似代码:

/*
 * Return a Gaussian (aka normal) random variable.
 *
 * Adapted from ppmforge.c, which is part of PBMPLUS.
 * The algorithm comes from:
 * 'The Science Of Fractal Images'. Peitgen, H.-O., and Saupe, D. eds.
 * Springer Verlag, New York, 1988.
 */
static int
gauss(int scale)
{
    double sum;

    sum = (RANDOM() & 0x7FFF) + (RANDOM() & 0x7FFF) +
	(RANDOM() & 0x7FFF) + (RANDOM() & 0x7FFF);

    return (int) (scale * (sum * 5.28596089837e-5 - 3.46410161514));
}

算法代码:没有经过任何优化


int GenGauss(int nLevel)
{
	double   d = (rand() + rand() + rand() + rand()) * 5.28596089837e-5 - 3.46410161514 ;
	return (int)(nLevel * d * 127.0 / 100.0) ;
}

void GuassianNoisyRGB(unsigned char* pInput,unsigned char* pOutput,int width,int height,int nStride,int nLevel)
{
	int bRandom = 0;
	int     n1, n2, n3 ;
	int i,j;
	int temp,index;

	if(nLevel<0)
		nLevel = 0;
	if(nLevel > 100)
		nLevel = 100;

	if(pInput == NULL || pOutput == NULL)
		return;

	if(width <= 0 || height <= 0)
		return;

	srand ((unsigned int)time(0)) ;

	for (j=0;j<height;j++)
	{
		for (i=0;i<width;i++)
		{
			if (bRandom)
			{
				n1=GenGauss(nLevel);
				n2=GenGauss(nLevel);
				n3=GenGauss(nLevel);
			}
			else
			{
				n1=n2=n3=GenGauss(nLevel) ;
			}

			index = j*nStride+i*3;
			temp = pInput[index] + n1;
			if(temp<0)
				temp = 0;
			if(temp>255)
				temp = 255;
			 
			pOutput[index] = temp;

			temp = pInput[index+1] + n2;
			if(temp<0)
				temp = 0;
		   if(temp>255)
				temp = 255;
			 
			pOutput[index+1] = temp;

			temp = pInput[index+2] + n3;
			if(temp<0)
				temp = 0;
			if(temp>255)
				temp = 255;
			 
			pOutput[index+2] = temp;
		}
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值