从图像C#.NET中删除噪声

时间:2016-09-28 08:56:29

标签: c# .net image-processing bitmap imagefilter

新手在这里。有一个图像我在图像上添加了噪点,他们需要用噪声(或类似的东西)清除图像。接下来是不合理的algorythm:

  

如果像素的亮度大于局部邻域的平均亮度,则将像素的亮度替换为周围的平均亮度。

  public void AdjustBrightness(Bitmap Image, int limit)
    {
        try
        {
            int width = newImage.Width;
            int height = newImage.Height;
            for (int x = 0; x < width - 1;x++)
            {
                if (x - 1 > 0 && x < width)
                {
                    for (int y = 0; y < height - 1;y++)
                    {
                        if (y - 1 > 0 && y < height)
                        {
                            int local1 =
                                (int)
                                    (0.3*(newImage.GetPixel(x - 1, y - 1)).R +
                                     0.59*(newImage.GetPixel(x - 1, y - 1)).G +
                                     0.11*(newImage.GetPixel(x - 1, y - 1)).B);
                            int local2 =
                                (int)
                                    (0.3*(newImage.GetPixel(x - 1, y).R) + 0.59*(newImage.GetPixel(x - 1, y).G) +
                                     0.11*(newImage.GetPixel(x - 1, y).B));
                            int local3 =
                                (int)
                                    (0.3*(newImage.GetPixel(x - 1, y + 1).R) +
                                     0.59*(newImage.GetPixel(x - 1, y + 1).G) +
                                     0.11*(newImage.GetPixel(x - 1, y + 1).B));
                            int local4 =
                                (int)
                                    (0.3*(newImage.GetPixel(x, y - 1).R) + 0.59*(newImage.GetPixel(x, y - 1).G) +
                                     0.11*(newImage.GetPixel(x, y - 1).B));
                            int local6 =
                                (int)
                                    (0.3*(newImage.GetPixel(x, y + 1).R) + 0.59*(newImage.GetPixel(x, y + 1).G) +
                                     0.11*(newImage.GetPixel(x, y + 1).B));
                            int local7 =
                                (int)
                                    (0.3*(newImage.GetPixel(x + 1, y - 1).R) +
                                     0.59*(newImage.GetPixel(x + 1, y - 1).G) +
                                     0.11*(newImage.GetPixel(x + 1, y - 1).B));
                            int local8 =
                                (int)
                                    (0.3*(newImage.GetPixel(x + 1, y).R) + 0.59*(newImage.GetPixel(x + 1, y).G) +
                                     0.11*(newImage.GetPixel(x + 1, y).B));
                            int local9 =
                                (int)
                                    (0.3*(newImage.GetPixel(x + 1, y + 1).R) +
                                     0.59*(newImage.GetPixel(x + 1, y + 1).G) +
                                     0.11*(newImage.GetPixel(x + 1, y + 1).B));
                            int localAll =
                                (int)
                                    ((local1 + local2 + local3 + local4 + local6 + local7 + local8 + local9)*0.125);
                            int LOCAL5 =
                                (int)
                                    (0.3*(newImage.GetPixel(x, y).R) + 0.59*(newImage.GetPixel(x, y).G) +
                                     0.11*(newImage.GetPixel(x, y).B))+1;

                                int p1 = newImage.GetPixel(x, y).R;
                                int p3 = newImage.GetPixel(x, y).B;
                                int p2 = newImage.GetPixel(x, y).G;
                                int pixelR = (p1*(localAll/LOCAL5));
                                int pixelG = (p2*(localAll/LOCAL5));
                                int pixelB = (p3*(localAll/LOCAL5));
                                if (Math.Abs(LOCAL5 - localAll) > limit)
                                {
                                    newImage.SetPixel(x, y, Color.FromArgb(pixelR, pixelG, pixelB));
                                }

                            pictureIn.Image = newImage;
                        }
                    }
                }

            }
        }
        catch (Exception e)
        {
            MessageBox.Show(e.Message);

        }

        finally
        {
            newImage.Save("F:/filter.jpeg");
        }
    }

在这一行中我遇到了一个问题:System.ArgumentException,某些变量值大于255:

newImage.SetPixel(x, y, Color.FromArgb(pixelR, pixelG, pixelB));

0 个答案:

没有答案