锐化图像过滤器

时间:2011-12-23 14:09:44

标签: c# image-processing opencv emgucv

我的代码可以将保存在我的计算机上的图像处理成灰度,平滑的灰色和精细的边缘图像。我想要它做的是通过向图像添加某种类型的滤镜来锐化图像,然后转换为灰色和canny边框。我相信我的代码是正确的但是当我运行程序时它似乎没有做任何不同的事情。锐化图像是否在错误的位置?它是以某种方式被忽视了吗?任何建议请告诉我。

      private void ProcessFrame()
  {
      String fileName = @"C:\filename";

               **Sharpen Image Filter code**
  public static Bitmap sharp(Bitmap img)
  {
      Bitmap sharpenImage = new Bitmap(img.Width, img.Height);

      int filterWidth = 3;
      int filterHeight = 3;
      int w = img.Width;
      int h = img.Height;

      double[,] filter = new double[filterWidth, filterHeight];

      filter[0, 0] = filter[0, 1] = filter[0, 2] = filter[1, 0] = filter[1, 2] = 
     filter[2,0]   =  filter[2, 1] = filter[2, 2] = -1;
      filter[1, 1] = 9;

      double factor = 1.0;
      double bias = 0.0;

      Color[,] result = new Color[img.Width, img.Height];

      for (int x = 0; x < w; ++x)
      {
          for (int y = 0; y < h; ++y)
          {
              double red = 0.0, green = 0.0, blue = 0.0;


              for (int filterX = 0; filterX < filterWidth; filterX++)
              {
                  for (int filterY = 0; filterY < filterHeight; filterY++)
                  {
                      int imageX = (x - filterWidth / 2 + filterX + w) % w;
                      int imageY = (y - filterHeight / 2 + filterY + h) % h;

                      Color imageColor = img.GetPixel(imageX, imageY);
                      red += imageColor.R * filter[filterX, filterY];
                      green += imageColor.G * filter[filterX, filterY];
                      blue += imageColor.B * filter[filterX, filterY];
                  }
                  int r = Math.Min(Math.Max((int)(factor * red + bias), 0), 255);
                  int g = Math.Min(Math.Max((int)(factor * green + bias), 0), 255);
                  int b = Math.Min(Math.Max((int)(factor * blue + bias), 0), 255);

                  result[x, y] = Color.FromArgb(r, g, b);
              }
          }
      }
      for (int i = 0; i < w; ++i)
      {
          for (int j = 0; j < h; ++j)
          {
              sharpenImage.SetPixel(i, j, result[i, j]);
          }
      }
      return sharpenImage;
  } 

**Sharpen Image Code Ends**

1 个答案:

答案 0 :(得分:1)

我想我可能会错过它,但看起来你并没有在图像上调用锐化代码。这肯定会解释为什么结果不会改变!