不知道为什么中值滤波器不能正常工作

时间:2017-03-25 23:43:59

标签: c# image pointers image-processing unsafe

我在c#中使用中值滤镜实现的问题用于图像处理。我用不安全的锁定位方法实现了这个过滤器,问题是我收到的图像以某种方式模糊不清(尽管噪音减少了)。 代码如下:

       public static Bitmap ArithmeticMean(Bitmap bitmap, int filterSize)
    {
        if (bitmap == null)
        {
            throw new ArgumentNullException("bitmap");
        }
        Bitmap clonnedBitmap = (Bitmap)bitmap.Clone();
        Bitmap secondClonnedBitmap = (Bitmap)bitmap.Clone();
        BitmapData srcData = clonnedBitmap.LockBits(new Rectangle(0, 0, clonnedBitmap.Width, clonnedBitmap.Height),
            ImageLockMode.ReadWrite, bitmap.PixelFormat);
        BitmapData newData = secondClonnedBitmap.LockBits(new Rectangle(0, 0, secondClonnedBitmap.Width, secondClonnedBitmap.Height),
            ImageLockMode.ReadWrite, bitmap.PixelFormat);
        int bytesPerPixel = Image.GetPixelFormatSize(srcData.PixelFormat) / 8;
        int stride0 = srcData.Stride;
        int stride1 = newData.Stride;
        int sideOfLoop = (filterSize-1)/2;
        unsafe
        {
            byte* scan0 = (byte*)srcData.Scan0.ToPointer();
            byte* scan1 = (byte*)newData.Scan0.ToPointer();
            int width = clonnedBitmap.Width * bytesPerPixel;
            int height = clonnedBitmap.Height;
            int i, j;

            for (int y = sideOfLoop; y < height - sideOfLoop; y++)
            {
                byte* currentLine = scan0 + y * stride0;
                byte* currentLineNewObject = scan1 + y* stride1;
                for (int x = sideOfLoop; x < width - sideOfLoop*bytesPerPixel; x += bytesPerPixel)
                {
                    var sumRed = 0;
                    var sumGreen = 0;
                    var sumBlue = 0;
                    for ( i = -sideOfLoop; i <= sideOfLoop; i++)
                    {
                        for (j = -sideOfLoop; j <= sideOfLoop; j++)
                        {
                            int oldBlue = (int) (currentLine[x + i*bytesPerPixel] + (byte) (j* stride0));
                            int oldGreen = (int)(currentLine[x + 1 + i * bytesPerPixel] + (byte)(j * stride0));
                            int oldRed = (int)(currentLine[x + 2 + i * bytesPerPixel] + (byte)(j * stride0));
                            sumBlue += oldBlue;
                            sumGreen += oldGreen;
                            sumRed += oldRed;
                        }
                    }
                    int newBlue = sumBlue / (filterSize * filterSize);
                    int newGreen = sumGreen / (filterSize * filterSize);
                    int newRed = sumRed / (filterSize * filterSize);

                    currentLineNewObject[x + 2] = (byte)(newRed);
                    currentLineNewObject[x + 1] = (byte)(newGreen);
                    currentLineNewObject[x] = (byte)(newBlue);
                }
            }
        }
        clonnedBitmap.UnlockBits(srcData);
        secondClonnedBitmap.UnlockBits(newData);

        return secondClonnedBitmap;
    }

1 个答案:

答案 0 :(得分:0)

你不想要“+(字节)(j * stride0));”成为索引者?

为什么要将位置添加到值?

 int oldBlue = (int) (currentLine[x + i*bytesPerPixel */]*/  + (byte) (j* stride0))

<强>