1

我实现了我自己的函数来二值化指纹图像。在我的方法中,我第一次尝试使用LockBits

你能解释一下,为什么我的图像上有很多伪影吗?示例:

在此处输入图像描述

在左边的图片上,我对图像进行了二值化Get/SetPixel,它工作得很好,但是为什么我不能在右边的图像上得到同样好的结果(很多红点)?我是否忘记或不知道某事?

private Bitmap Binarization(Bitmap tempBmp)
    {

        int threshold = otsuValue(tempBmp); //calculating threshold with Otsu method

        unsafe
        {
            BitmapData bmpData = tempBmp.LockBits(new System.Drawing.Rectangle(0, 0, tempBmp.Width, tempBmp.Height), ImageLockMode.ReadWrite, tempBmp.PixelFormat);
            byte* ptr = (byte*)bmpData.Scan0;


            int height = tempBmp.Height;
            int width = bmpData.Width * 4;
            Parallel.For(0, height, y =>
            {
                byte* offset = ptr + (y * bmpData.Stride); //set row
                for (int x = 0; x < width; x = x + 4)
                {
                    //changing pixel value 
                    offset[x] = offset[x] > threshold ? Byte.MaxValue : Byte.MinValue;
                    offset[x+1] = offset[x+1] > threshold ? Byte.MaxValue : Byte.MinValue;
                    offset[x+2] = offset[x+2] > threshold ? Byte.MaxValue : Byte.MinValue;
                    offset[x+3] = offset[x+3] > threshold ? Byte.MaxValue : Byte.MinValue;

                }
            });

            tempBmp.UnlockBits(bmpData);
        }

        return tempBmp;
    }

同样的历史,当我想从图像中删除一点字节时,问题看起来有点复杂。

在此处输入图像描述

为什么它甚至没有进入好的“如果”声明?

private Bitmap Binarization(Bitmap tempBmp)
    {

        int threshold = otsuValue(tempBmp);

        unsafe
        {
            BitmapData bmpData = tempBmp.LockBits(new System.Drawing.Rectangle(0, 0, tempBmp.Width, tempBmp.Height), ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed); 
            //Format8bpp, not pixel format from image
            byte* ptr = (byte*)bmpData.Scan0;


            int height = tempBmp.Height;
            int width = bmpData.Width; //i cut "* 4" here because of one channel image
            Parallel.For(0, height, y =>
            {
                byte* offset = ptr + (y * bmpData.Stride); //set row
                for (int x = 0; x < width; x++)
                {
                    //changing pixel values
                    offset[x] = offset[x] > threshold ? Byte.MaxValue : Byte.MinValue;

                }
            });

            tempBmp.UnlockBits(bmpData);
        }

        return tempBmp;
    }

感谢您提供任何改进我的功能的建议。

4

2 回答 2

1

1) 您使用的算法不保证 ab/w 结果。

您正在单独测试RGB 通道,所以如果它会打开但不会等等。R > thresholdG or B < thresholdG and/or B

事实上,真正的问题是为什么没有更多的人工制品出现。必须与源图像有关。

解决方案:所有三个通道相加并与阈值 * 3 进行比较;然后将所有设置为相同的黑色或白色值!

2)还有另一个问题:您从某处复制的代码盲目地假设图像具有 32 bpp。但是,虽然您发布的图像是 apng它仍然只有 24 bpp。因此,您的代码将做各种有趣的事情..

更新和更正的示例:

        ..
        int pixWidth = tempBmp.PixelFormat == IMG.PixelFormat.Format24bppRgb ? 3 : 
                       tempBmp.PixelFormat == IMG.PixelFormat.Format32bppArgb ? 4 : 4;


        byte* ptr = (byte*)bmpData.Scan0;

        threshold3 = threshold * 3;
        int height = tempBmp.Height;
        int width = bmpData.Width * pixWidth;
        Parallel.For(0, height, y =>
        {
            byte* offset = ptr + (y * bmpData.Stride); //set row
            for (int x = 0; x < width; x = x + pixWidth)
            {
                //changing pixel value 
                int v = (offset[x] + offset[x + 1] + offset[x + 2]) > threshold3 ? 
                        Byte.MaxValue : Byte.MinValue;;
                offset[x] = (byte)v ;
                offset[x+1] = (byte)v;
                offset[x+2] = (byte)v;
                if (pixWidth == 4) offset[x+3] = 255;

            }
        });
于 2018-08-14T10:03:20.727 回答
0

对于这个问题,我的解决方案如下所示:

public static bool Offset(Bitmap b_mapa, int threshold)
        {
            int threshold3 = threshold * 3;
            int red, green, blue;
            BitmapData bmData = b_mapa.LockBits(new Rectangle(0, 0, b_mapa.Width, b_mapa.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
            int stride = bmData.Stride;
            System.IntPtr Scan0 = bmData.Scan0;
            unsafe
            {
                byte* p = (byte*)(void*)Scan0;
                int nOffset = stride - b_mapa.Width * 3;
                for (int y = 0; y < b_mapa.Height; ++y)
                {
                    for (int x = 0; x < b_mapa.Width; ++x)
                    {
                        blue = p[0];
                        green = p[1];
                        red = p[2];
                        int v = ((int)(blue + green + red) > threshold3 ? (int)255 : (int)0);
                        p[0] = p[1] = p[2] = (byte)v;
                        p += 3;
                    }
                    p += nOffset;
                }
            }
            b_mapa.UnlockBits(bmData);
            return true;
        }

此函数使用名为 b_mapa 的共享资源,我们的函数应根据处理结果返回一个布尔值。我在这里不使用并行功能,因为当我们使用 LockBits 时,我们直接使用内存,而不使用可以快 1000 倍且没有必要并行化的嵌入式函数。如果过滤器是卷积的,那么并行化可以用于更快的代码。购买方式,当我加载图像时,我将它们转换为位图,以后做所有事情都更简单,不要考虑我有多少位用于颜色表示。

如您所见,位图中的数组值是 BGR 而不是 RGB,只是为了让您知道,因为许多人犯了这个错误并弄乱了颜色。

于 2019-10-01T13:11:25.687 回答