2011-12-23 107 views
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

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

+0

在锐化图像代码中,我将'img'更改为'frame',因为原始图像是如何进来的。这是不正确的吗? – LewSim 2011-12-23 14:52:59

+0

“Sharp”是一种将位图作为参数并返回位图的方法。要运行该方法中的代码,需要调用它。就像你从方法“processFrame”中调用方法“foo”一样,除非我假设你需要将OpenCV Image 类型转换为位图类型,或者重写Sharp以使用CV的类。我希望OpenCV有一个内置的函数来使用。 – Mikeb 2011-12-23 16:11:28

+0

例如:见这里:http://stackoverflow.com/questions/4993082/how-to-sharpen-an-image-in-opencv – Mikeb 2011-12-23 16:11:55