我实现了我自己的函数来二值化指纹图像。在我的方法中,我第一次尝试使用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;
}
感谢您提供任何改进我的功能的建议。