锐化图像滤镜

3

我有一段代码,可以将电脑上保存的图像处理成灰度、平滑灰度、Canny边缘图像。但我希望对图像进行锐化处理,添加某种滤镜,然后再转换为灰度和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个回答

1

我猜我可能错过了什么,但是看起来你从来没有在图像上调用过锐化代码。这肯定可以解释为什么结果没有改变!


在锐化图像的代码中,我将“img”更改为“frame”,因为这是原始图像的输入方式。这样做不正确吗? - LewSim
"Sharp"是一种以Bitmap为参数并返回Bitmap的方法。为了运行该方法内部的代码,需要调用它。就像你从“processFrame”方法内部调用“foo”方法一样,只是我假设你需要将OpenCV Image<T,U>类型转换为Bitmap类型,或者重写Sharp以使用CV的类。我希望OpenCV有一个内置函数可供使用。 - Mikeb
例如:请参见此处:https://dev59.com/nG445IYBdhLWcg3wLXWh - Mikeb

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接