【问题标题】:Sharpen Image Filter锐化图像过滤器
【发布时间】:2011-12-23 14:09:44
【问题描述】:

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

      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**

【问题讨论】:

    标签: c# image-processing opencv emgucv


    【解决方案1】:

    我想我可能会错过它,但看起来您从未在图像上调用锐化代码。这当然可以解释为什么结果没有改变!

    【讨论】:

    • 在锐化图像代码中,我将“img”更改为“frame”,因为这是原始图像的输入方式。这不正确吗?
    • "Sharp"是一个以Bitmap为参数并返回Bitmap的方法。要运行该方法中的代码,需要调用它。就像您从方法“processFrame”内部调用方法“foo”一样,除了我假设您需要将 OpenCV Image 类型转换为 Bitmap 类型,或者重写 Sharp 以使用 CV 的类。我希望 OpenCV 有一个内置函数可以使用。
    猜你喜欢
    • 2016-08-09
    • 1970-01-01
    • 2017-01-01
    • 2011-11-09
    • 2020-03-08
    • 2012-12-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多