锐化过滤器 - 没有效果

时间:2016-08-26 16:25:07

标签: c# image-processing filter bitmap convolution

以下例程仅锐化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");
        }
    }

1 个答案:

答案 0 :(得分:2)

我已经改变了你的功能以使其正常工作。 请注意强度参数对我的功能没有影响。您可以使用偏差和因子值来获得不同的亮度效果,等等。

<script>
    $('#YOUR SUBMIT ID').submit(function(){

       //Ajax Data
       $.ajax({  
          type:'POST',  
          url:'YOUR BACKEND.php',  
          data:'YOUR DATA',
          success: function() {

              //Hide Button
              $('#YOUR SUBMIT ID').attr("disabled", true);
          },
          error: function() { 

              //YOUR Code  
          }
       });

    });
</script>

我希望这会让你去:) 此致