图像噪声是影响人们接受图像信息的因素,常见的噪声有高斯噪声和椒盐噪声。因为最近课程要求,做一个图像恢复的Project,所以掌握了给图像添加噪声以及去除噪声的方法。
给图像添加高斯噪声
高斯噪声是大量具有正太分布的随机变量性质的值加到原图像造成的,要给图像添加高斯噪声,其实问题就是怎么产生正太分布随机变量。首先用Randdom对象的NextDouble产生两个0-1之间的随机变量r1,r2,计算
double result = Math.Sqrt((-2) * Math.Log(r2)) * Math.Sin(2 * Math.PI * r1);
得到的result就是具有均值0,方差1的正太分布随机变量。这是box-muller方法,算法推导很复杂,但实现却很方便。因为对图像添加高斯噪声的时候,对于每一个像素都需要产生r1,r2以便得到噪声,这就需要快速大量地产生随机变量,一开始我发现产生的随机变量总是连续相同,也就是说在很短的时间内产生的随机数是相同的,因为毕竟C#Random产生的是伪随机数,是通过一定的算法算出来的,而且有依据“种子”来计算,默认情况下是依据电脑此时时间来计算,但是当快速大量此类的随机数时,会出现连续产生相同随机数的情况,因为电脑时间不是一个很好的“种子”。
C++ Primer Plus 第6版 中文版 清晰有书签PDF+源代码
C++11 获取系统时间库函数 time since epoch
所以我的程序里用了这样的办法:
static int GetRandomSeed( ) //产生随机种子
      { 
            byte[] bytes = new byte[4]; 
            System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider( ); 
            rng.GetBytes( bytes ); 
            return BitConverter.ToInt32( bytes , 0 ); 
      }
public double GaussNiose1()//用box muller的方法产生均值为0,方差为1的正太分布随机数
        {
          // Random ro = new Random(10);
          // long tick = DateTime.Now.Ticks;
            Random ran = new Random(GetRandomSeed());
          // Random rand = new Random();
            double r1 = ran.NextDouble();
            double r2 = ran.NextDouble();
            double result = Math.Sqrt((-2) * Math.Log(r2)) * Math.Sin(2 * Math.PI * r1);
            return result;//返回随机数
        }
这样问题就解决了,可以短时间快速产生随机数。
给图像添加椒盐噪声
添加椒盐噪声的方法如下:
private void AddSalt(object sender, EventArgs e)  
02.        {  
03.            if (textBox12.Text != "" && textBox13.Text != "")  
04.            {  
05.              // Bitmap pic = (Bitmap)Bitmap.FromFile(filename, false);  
06.                Bitmap pic = new Bitmap(pictureBox2.Image, WI, HE);  
07.                double Pa = Convert.ToDouble(textBox12.Text);//接受输入的Pa  
08.                double Pb = Convert.ToDouble(textBox13.Text);//接受输入的Pb  
09.                double P = Pb / (1 - Pa);//程序要,为了得到一个概率Pb事件  
10.                int width = pic.Width;  
11.                int height = pic.Height;  
12.                Random rand = new Random();  
13.                for (int i = 0; i < height; i++)  
14.                {  
15.                    for (int j = 0; j < width; j++)  
16.                    {  
17.                        int gray;  
18.                        int noise = 1;  
19.                        double probility = rand.NextDouble();  
20.                        if (probility < Pa)  
21.                        {  
22.                            noise = 255;//有Pa概率 噪声设为最大值  
23.                        }  
24.                        else  
25.                        {  
26.                            double temp = rand.NextDouble();  
27.                            if (temp < P)//有1 - Pa的几率到达这里,再乘以 P ,刚好等于Pb  
28.                                noise = 0;  
29.                        }  
30.                        if (noise != 1)  
31.                        {  
32.                            gray = noise;  
33.                        }  
34.                        else gray = pic.GetPixel(j, i).R;  
35.                        Color color = Color.FromArgb(gray, gray, gray);  
36.                        pic.SetPixel(j, i, color);  
37.                    }  
38.                }  
39.                Form2 f2 = new Form2();  
40.                f2.change_size(pic);  
41.                f2.Setname("图像添加椒盐噪声之后的图像,需要还原的话请先保存然后再打开^_^");  
42.                f2.Show();  
43.            }  
44.            else  
45.            {  
46.                MessageBox.Show("请先输入Pa和Pb^_^");  
47.            }  
48.        } 
