给图像添加随机噪声. [英] Adding random noise to an image.

查看:681
本文介绍了给图像添加随机噪声.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

感谢您的帮助,以增加噪音!! :-D

现在,我需要添加随机噪声.噪声输入将在0到1之间,0值将不影响原始图像,而value-1将完全扰乱图像.我在-noiseValue和noiseValue之间生成了一个随机数.

到目前为止,无论我为我的noiseValue输入什么值,生成的图像都是完全混乱的.这是我所拥有的:

Thanks for the help on adding noise!! :-D

Now I need to add random noise. The noise input will be between 0 and 1, 0-value will leave the original picture unaffected while value-1 will completely scramble the picture. I generated a random number in between -noiseValue and noiseValue.

So far, no matter what value I input for my noiseValue, the resulting picture is completely scrambled. Here''s what I have:

int Image32::AddRandomNoise(const float& noise,Image32& outputImage) const
{
	float rnoise;
	float range = 2*noise;
	
	if (noise >= 0 && noise <= 1)
	{
		for (int y = 0; y < outputImage.height(); y++)
		{
			for (int x = 0; x < outputImage.width(); x++)
			{				
				
				Pixel32& p1 = outputImage.pixel(x, y);
				
				rnoise = rand()*range-noise;			
				p1.r *= rnoise;
				p1.g *= rnoise;
				p1.b *= rnoise;
				
			}
		}

		return 1;
	}
		
	return 0;
}

推荐答案

您需要限制rand()的输出.

rnoise = rand()*range-noise;

看看rand()的定义.它返回的值在0-RANDMAX之间,您需要使用%运算符来限制rand()返回的数字.否则,该值将始终大于所需值.扰乱图像.

我相信您希望它成为:

rnoise = rand()%range-noise;
You need to restrict the output of rand().

rnoise = rand()*range-noise;

look at the defintion of rand(). It returns a value between 0-RANDMAX you need to restrict the number returned by rand() with a % operator. Otherwise the value will always be larger than you want; scrambling the image.

I believe you want it to be:

rnoise = rand()%range-noise;


代码完全按照您的要求进行操作.要添加随机噪声,需要将其添加到随机位置,而不是每个像素.
The code is doing exactly what you''re asking it to. To add random noise, you need to add it in random places, not every pixel.


这篇关于给图像添加随机噪声.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆