C#图像添加高斯噪声和椒盐噪声及图像恢复

       图像噪声是影响人们接受图像信息的因素,常见的噪声有高斯噪声和椒盐噪声。因为最近课程要求,做一个图像恢复的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产生的是伪随机数,是通过一定的算法算出来的,而且有依据“种子”来计算,默认情况下是依据电脑此时时间来计算,但是当快速大量此类的随机数时,会出现连续产生相同随机数的情况,因为电脑时间不是一个很好的“种子”。所以我的程序里用了这样的办法:

        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)
        {
            if (textBox12.Text != "" && textBox13.Text != "")
            {
               // Bitmap pic = (Bitmap)Bitmap.FromFile(filename, false);
                Bitmap pic = new Bitmap(pictureBox2.Image, WI, HE);
                double Pa = Convert.ToDouble(textBox12.Text);//接受输入的Pa
                double Pb = Convert.ToDouble(textBox13.Text);//接受输入的Pb
                double P = Pb / (1 - Pa);//程序要,为了得到一个概率Pb事件
                int width = pic.Width;
                int height = pic.Height;
                Random rand = new Random();
                for (int i = 0; i < height; i++)
                {
                    for (int j = 0; j < width; j++)
                    {
                        int gray;
                        int noise = 1;
                        double probility = rand.NextDouble();
                        if (probility < Pa)
                        {
                            noise = 255;//有Pa概率 噪声设为最大值
                        }
                        else
                        {
                            double temp = rand.NextDouble();
                            if (temp < P)//有1 - Pa的几率到达这里,再乘以 P ,刚好等于Pb
                                noise = 0;
                        }
                        if (noise != 1)
                        {
                            gray = noise;
                        }
                        else gray = pic.GetPixel(j, i).R;
                        Color color = Color.FromArgb(gray, gray, gray);
                        pic.SetPixel(j, i, color);
                    }
                }
                Form2 f2 = new Form2();
                f2.change_size(pic);
                f2.Setname("图像添加椒盐噪声之后的图像,需要还原的话请先保存然后再打开^_^");
                f2.Show();
            }
            else
            {
                MessageBox.Show("请先输入Pa和Pb^_^");
            }
        }
图像恢复(中值滤波)

       对于每一个像素,取出周围九个像素,存到filter,然后对这个数组进行排序,最后取到中间值作为目标值。下面程序是在取值的时候就同时进行排序,采用插入排序法。  

   private void MedianFilter(object sender, EventArgs e)
        {
            Bitmap pic = new Bitmap(pictureBox2.Image, WI, HE);
            int width = pic.Width;
            int height = pic.Height;
            int[,] resultPic = new int[height, width];
            int index;
            int[] filter = new int[9];
            for (int i = 0; i < height; i++)
            {
                for (int j = 0; j < width; j++)
                {
                    index = 0;
                    int nowGray;
                    for (int ii = -1; ii < 2; ii++)
                    {
                        for (int jj = -1; jj < 2; jj++)
                        {
                            if (j + jj >= 0 && j + jj < width && i + ii >= 0 && i + ii < height)
                           {
                                nowGray = pic.GetPixel(j + jj, i + ii).R;
                           }
                            else { nowGray = 0; }
                                if (index == 0) { filter[index] = nowGray; index++; }
                                else
                                {
                                    if (nowGray >= filter[index-1])
                                    {
                                        filter[index++] = nowGray;
                                    }
                                    else
                                    {
                                        int current = index-1;
                                        while (current > 0 && filter[current] > nowGray)
                                        {
                                            filter[current + 1] = filter[current];
                                            current--;
                                        }
                                        filter[current+1] = nowGray;
                                        index++;
                                    }
                                  
                                }
                            
                           
                        }
                    }
                    resultPic[i,j] = filter[4];
                   // int temp = filter[4];
                  //  Color color = Color.FromArgb(temp, temp, temp);
                   // pic.SetPixel(j, i, color);
                }
            }
            for (int i = 0; i < height; i++)
            {
                for (int j = 0; j < width; j++)
                {
                    int temp = resultPic[i,j];
                    Color color = Color.FromArgb(temp, temp, temp);
                    pic.SetPixel(j, i, color);
                }
            }
            Form2 f2 = new Form2();
            f2.change_size(pic);
            f2.Setname("中值滤波之后的图像");
            f2.Show();
        }

下面是是加椒盐噪声(Pa=Pb0.2)之后的图像和中值滤波处理之后的图像






  • 7
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值