滤镜之高斯噪声GaussianNoise

首先看这两张效果图:

滤镜之高斯噪声GaussianNoise_第1张图片

滤镜之高斯噪声GaussianNoise_第2张图片滤镜之高斯噪声GaussianNoise_第3张图片

 

图像加上高斯噪声非常简单了,每个像素点叠加上高斯噪声即可。同理也可以叠加上其他的噪声。具体可以参考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;j255)
				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;
		}
	}
}



你可能感兴趣的:(滤镜)