2016-08-26 4 views
2

다음 루틴은 8 비트 색인화 된 회색 음영 만 선명하게하는 것입니다.필터 선명하게 - 효과 없음

이 코드는 입력 이미지에 아무런 영향을 미치지 않는 것으로 보입니다. 즉, 무엇이 들어오고 있으며, 같은 것이 나오고 있습니다.

strength의 값을 늘리면 이미지가 어두워 지지만 필터링되지 않은 것처럼 보입니다.

무엇이 잘못 될 수 있습니까?

double[,] _numericalKernel = new double[,] 
          { { -1, -1, -1, }, 
           { -1, 9, -1, }, 
           { -1, -1, -1, }, }; 

다음 나는 다음과 같은 커널을 사용하고

, 선명 내 소스 코드, 당신의 기능이 작동하도록 조금 변경했습니다

public static Bitmap NonfftSharpen(Bitmap image, double[,] mask, double strength) 
    { 
     Bitmap bitmap = (Bitmap)image.Clone(); 

     if (bitmap != null) 
     { 
      int width = bitmap.Width; 
      int height = bitmap.Height; 

      if (mask.GetLength(0) != mask.GetLength(1)) 
      { 
       throw new Exception("_numericalKernel dimensions must be same"); 
      } 
      // Create sharpening filter. 
      int filterSize = mask.GetLength(0); 

      double[,] filter = (double[,])mask.Clone(); 

      int channels = sizeof(byte); 
      double bias = 1.0 - strength; 
      double factor = strength/16.0; 
      int halfOfFilerSize = filterSize/2; 

      byte[,] result = new byte[bitmap.Width, bitmap.Height]; 

      // Lock image bits for read/write. 

      BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, width, height), 
                 ImageLockMode.ReadWrite, 
                 PixelFormat.Format8bppIndexed); 

      // Declare an array to hold the bytes of the bitmap. 
      int memorySize = bitmapData.Stride * height; 
      byte[] memory = new byte[memorySize]; 

      // Copy the RGB values into the local array. 
      Marshal.Copy(bitmapData.Scan0, memory, 0, memorySize); 

      int rgb; 
      // Fill the color array with the new sharpened color values. 

      for (int y = halfOfFilerSize; y < height - halfOfFilerSize; y++) 
      { 
       for (int x = halfOfFilerSize; x < width - halfOfFilerSize; x++) 
       { 
        for (int filterY = 0; filterY < filterSize; filterY++) 
        { 
         double grayShade = 0.0; 

         for (int filterX = 0; filterX < filterSize; filterX++) 
         { 
          int imageX = (x - halfOfFilerSize + filterX + width) % width; 
          int imageY = (y - halfOfFilerSize + filterY + height) % height; 

          rgb = imageY * bitmapData.Stride + channels * imageX; 

          grayShade += memory[rgb + 0] * filter[filterX, filterY]; 
         } 

         rgb = y * bitmapData.Stride + channels * x; 

         int b = Math.Min(Math.Max((int)(factor * grayShade + (bias * memory[rgb + 0])), 0), 255); 

         result[x, y] = (byte)b; 
        } 
       } 
      } 

      // Update the image with the sharpened pixels. 
      for (int x = halfOfFilerSize; x < width - halfOfFilerSize; x++) 
      { 
       for (int y = halfOfFilerSize; y < height - halfOfFilerSize; y++) 
       { 
        rgb = y * bitmapData.Stride + channels * x; 

        memory[rgb + 0] = result[x, y]; 
       } 
      } 

      // Copy the RGB values back to the bitmap. 
      Marshal.Copy(memory, 0, bitmapData.Scan0, memorySize); 

      // Release image bits. 
      bitmap.UnlockBits(bitmapData); 

      return bitmap; 

     } 
     else 
     { 
      throw new Exception("input image can't be null"); 
     } 
    } 
+0

음을하려고 할텐데, 지금있는 : 필터 __nothing__을하거나 결과 __darken__ 매개 변수를하지 않습니다? – TaW

+0

@TaW, 필터는 아무 작업도 수행하지 않습니다. '힘'은 출력 이미지의 어두움만을 변경합니다. 실제로는 필터링되지 않습니다. – anonymous

+0

글쎄, 그건 아무것도 아니야, 네가 원하는게 아니야. 마스크 입력 값은 무엇입니까? – TaW

답변

2

입니다. strength 매개 변수가 내 기능에 영향을 미치지 않도록주의하십시오. 바이어스 및 계수 값을 사용하여 밝기 등 다른 결과를 얻을 수 있습니다.

public static Bitmap NonfftSharpen(Bitmap image, double[,] mask, double strength) 
    { 
     Bitmap bitmap = (Bitmap)image.Clone(); 

     if (bitmap != null) 
     { 
      int width = bitmap.Width; 
      int height = bitmap.Height; 

      if (mask.GetLength(0) != mask.GetLength(1)) 
      { 
       throw new Exception("_numericalKernel dimensions must be same"); 
      } 
      // Create sharpening filter. 
      int filterSize = mask.GetLength(0); 

      double[,] filter = (double[,])mask.Clone(); 

      int channels = sizeof(byte); 
      double bias = 0.0; // 1.0 - strength; 
      double factor = 1.0; // strength/16.0; 

      byte[,] result = new byte[bitmap.Width, bitmap.Height]; 

      // Lock image bits for read/write. 

      BitmapData bitmapData = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, width, height), 
                 ImageLockMode.ReadWrite, 
                 System.Drawing.Imaging.PixelFormat.Format8bppIndexed); 

      // Declare an array to hold the bytes of the bitmap. 
      int memorySize = bitmapData.Stride * height; 
      byte[] memory = new byte[memorySize]; 

      // Copy the RGB values into the local array. 
      Marshal.Copy(bitmapData.Scan0, memory, 0, memorySize); 

      int pixel; 
      // Fill the color array with the new sharpened color values. 

      for (int y = 0; y < height; y++) 
      { 
       for (int x = 0; x < width; x++) 
       { 
        double grayShade = 0.0; 

        for (int filterY = 0; filterY < filterSize; filterY++) 
        { 


         for (int filterX = 0; filterX < filterSize; filterX++) 
         { 

          int imageX = (x - filterSize/2 + filterX + width) % width; 
          int imageY = (y - filterSize/2 + filterY + height) % height; 

          pixel = imageY * bitmapData.Stride + channels * imageX; 
          grayShade += memory[pixel] * filter[filterX, filterY]; 

         } 

         int newPixel = Math.Min(Math.Max((int)(factor * grayShade + bias), 0), 255); 

         result[x, y] = (byte)newPixel; 
        } 
       } 
      } 

      // Update the image with the sharpened pixels. 
      for (int x = 0; x < width; x++) 
      { 
       for (int y = 0; y < height; y++) 
       { 
        pixel = y * bitmapData.Stride + channels * x; 

        memory[pixel] = result[x, y]; 
       } 
      } 

      // Copy the values back to the bitmap. 
      Marshal.Copy(memory, 0, bitmapData.Scan0, memorySize); 

      // Release image bits. 
      bitmap.UnlockBits(bitmapData); 

      return bitmap; 

     } 
     else 
     { 
      throw new Exception("input image can't be null"); 
     } 
    } 

나는 이것이 당신이 :) 감사