去除斑点 - 去除图像中的斑点或点

taj*_*i01 5 .net c# image winforms

我正在使用Winforms.在我的表格中,我有一个显示黑白图像的图片框.我还有一个按钮,如果你点击它,该按钮将删除图像上的斑点/点.当图像尺寸不大时,它会快速消除斑点.如果图像很大,则需要一段时间.此外,有时这个函数会从它认为是一个点的图像中删除一些单词.如何提高此功能的性能,更准确地去除斑点或点基本上去除图像?

更新 研究后,我发现这个库对这个问题似乎很有希望:

http://www.aforgenet.com/framework/docs/html/cdf93487-0659-e371-fed9-3b216efb6954.htm

斑点图片链接: http ://www.filedropper.com/testing-image3

图像示例

注意链接中的图像有更大的版本:

在此输入图像描述

图像信息

这里需要注意的是它是一张黑白图像 - Bit Depth 1

在此输入图像描述

我的守则

    private int[] mask = new int[9];
    private void remove_spot_btn_Click(object sender, EventArgs e)
    {
        Bitmap img = new Bitmap(pictureBox1.Image);
        Color c;

        for (int ii = 0; ii < img.Width; ii++)
        {
            for (int jj = 0; jj < img.Height; jj++)
            {

                if (ii - 1 >= 0 && jj - 1 >= 0)
                {
                    c = img.GetPixel(ii - 1, jj - 1);
                    mask[0] = Convert.ToInt16(c.R);
                }
                else
                {
                    mask[0] = 0;
                }

                if (jj - 1 >= 0 && ii + 1 < img.Width)
                {
                    c = img.GetPixel(ii + 1, jj - 1);
                    mask[1] = Convert.ToInt16(c.R);
                }
                else
                    mask[1] = 0;

                if (jj - 1 >= 0)
                {
                    c = img.GetPixel(ii, jj - 1);
                    mask[2] = Convert.ToInt16(c.R);
                }
                else
                    mask[2] = 0;

                if (ii + 1 < img.Width)
                {
                    c = img.GetPixel(ii + 1, jj);
                    mask[3] = Convert.ToInt16(c.R);
                }
                else
                    mask[3] = 0;

                if (ii - 1 >= 0)
                {
                    c = img.GetPixel(ii - 1, jj);
                    mask[4] = Convert.ToInt16(c.R);
                }
                else
                    mask[4] = 0;

                if (ii - 1 >= 0 && jj + 1 < img.Height)
                {
                    c = img.GetPixel(ii - 1, jj + 1);
                    mask[5] = Convert.ToInt16(c.R);
                }
                else
                    mask[5] = 0;

                if (jj + 1 < img.Height)
                {
                    c = img.GetPixel(ii, jj + 1);
                    mask[6] = Convert.ToInt16(c.R);
                }
                else
                    mask[6] = 0;


                if (ii + 1 < img.Width && jj + 1 < img.Height)
                {
                    c = img.GetPixel(ii + 1, jj + 1);
                    mask[7] = Convert.ToInt16(c.R);
                }
                else
                    mask[7] = 0;
                c = img.GetPixel(ii, jj);
                mask[8] = Convert.ToInt16(c.R);
                Array.Sort(mask);
                int mid = mask[4];
                img.SetPixel(ii, jj, Color.FromArgb(mid, mid, mid));
            }
        }

        pictureBox1.Image = img;
        MessageBox.Show("Complete");
    }
Run Code Online (Sandbox Code Playgroud)

Sim*_*ier 4

正如您所发现的,使用AForge.NET是一个好主意(您只需将其添加为 nuget)。我建议您使用它的中值滤波器,它通常用于去噪(请参阅中值滤波器)。

AForge 需要 24bpp RGB 图像,因此您需要首先在示例案例中对其进行转换,但这里有一个代码示例,似乎可以很好地处理它:

  // load the file as 24bpp RGB
  using (var bmp = LoadForFiltering(@"C:\temp\Testing-Image3.tif"))
  {
      var filter = new Median();

      // run the filter 
      filter.ApplyInPlace(bmp);

      // save the file back (here, I used png as the output format)
      bmp.Save(@"C:\temp\Testing-Image3.png");
  }


  private static Bitmap LoadForFiltering(string filePath)
  {
      var bmp = (Bitmap)Bitmap.FromFile(filePath);
      if (bmp.PixelFormat == PixelFormat.Format24bppRgb)
          return bmp;

      try
      {
          // from AForge's sample code
          if (bmp.PixelFormat == PixelFormat.Format16bppGrayScale || Bitmap.GetPixelFormatSize(bmp.PixelFormat) > 32)
              throw new NotSupportedException("Unsupported image format");

          return AForge.Imaging.Image.Clone(bmp, PixelFormat.Format24bppRgb);
      }
      finally
      {
          bmp.Dispose();
      }
  }
Run Code Online (Sandbox Code Playgroud)

如果您确实需要高性能,那么您可以选择 NVidia CUDA/NPP(直接使用 GPU),但这需要更多的工作,不直接由 C# 支持(当然需要 NVidia 卡)。相关问题在这里:2D CUDA 中值滤波器优化和 CUDA 白皮书:使用 CUDA 的图像处理和视频算法

  • 没关系,我所要做的就是“pictureBox1.Image = AForge.Imaging.Image.Clone(bmp, PixelFormat.Format24bppRgb);” (2认同)