手机版
你好,游客 登录 注册
背景:
阅读新闻

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

[日期:2014-06-11] 来源:Linux社区  作者:u012999424 [字体:大 中 小]

图像恢复(中值滤波)

对于每一个像素,取出周围九个像素,存到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)之后的图像和中值滤波处理之后的图像

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

本文永久更新链接地址:http://www.linuxidc.com/Linux/2014-06/103012.htm

linux
相关资讯       C#  高斯噪声  椒盐噪声 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款