2016-08-26 3 views
2

Следующая процедура заключается в том, чтобы затачивать только 8-точечный индексный оттенок серого.Sharpen filter - no effect

Этот код, похоже, не влияет на входное изображение. То есть, что происходит, то же самое выходит.

Если я увеличиваю значение 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

Um, теперь, который: имеет ли фильтр сделать __nothing__ или же параметр __darken__ результата? – TaW

+0

@TaW, фильтр ничего не делает. «Сила» только изменяет темноту выходного изображения, которое на самом деле никогда не фильтруется. – anonymous

+0

Ну, это не совсем так, просто не то, что вы хотите. Каковы входные значения маски? – TaW

ответ

2

Я изменил вашу функцию немного, чтобы заставить его работать. Позаботьтесь о том, чтобы параметр силы не влиял на мою функцию. Вы можете играть со значениями смещения и коэффициента, чтобы получить разные результаты в яркости и так далее.

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"); 
     } 
    } 

Я надеюсь, что это получает вы собираетесь :) С уважением

 Смежные вопросы

  • Нет связанных вопросов^_^