gpt4 book ai didi

c# - 为什么这个图像阈值在 windows 中有效,但在 mono/linux 中无效?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:20:16 24 4
gpt4 key购买 nike

我有一个使用不安全代码的相对简单的阈值函数。这在 Windows 中有效,但是,在 Linux 上的 Mono 中,不会发生阈值。很难调试,因为它仅在 Linux 上,我检查了 bitsPerPixel 以及高度 h、宽度 w 和步幅 ws 是否正确也都是正确的。

我还能做些什么来缩小范围,或者这里有一个常见的问题吗?

public void threshold(Bitmap bmp, int thresh)
{
var bitsPerPixel = Image.GetPixelFormatSize(bmp.PixelFormat) / 8;

BitmapData bmData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height),
ImageLockMode.ReadOnly, bmp.PixelFormat);
unsafe
{
byte* p = (byte*)(void*)bmData.Scan0.ToPointer();
int h = bmp.Height;
int w = bmp.Width;
int ws = bmData.Stride;

for (int i = 0; i < h; i++)
{
byte* row = &p[i * ws];
for (int j = 0; j < w * bitsPerPixel; j += bitsPerPixel)
{
for (var k = 0; k < bitsPerPixel; k++)
{
row[j + k] = (byte)((row[j + k] > (byte)thresh) ? 255 : 0);
}
}
}
}
bmp.UnlockBits(bmData);
}

最佳答案

ImageLockMode设置为ReadWrite:

BitmapData bmData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height),
ImageLockMode.ReadWrite, bmp.PixelFormat);

例子:

public unsafe void Threshold(Bitmap bmp, int thresh)
{
var bmData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, bmp.PixelFormat);
var bitsPerPixel = Image.GetPixelFormatSize(bmp.PixelFormat);
var p = (byte*)bmData.Scan0.ToPointer();
for (int i = 0; i < bmData.Height; ++i)
{
for (int j = 0; j < bmData.Width; ++j)
{
byte* data = p + i * bmData.Stride + j * bitsPerPixel / 8;
data[0] = (byte)((data[0] > thresh) ? 255 : 0);
data[1] = (byte)((data[1] > thresh) ? 255 : 0);
data[2] = (byte)((data[2] > thresh) ? 255 : 0);
}
}
bmp.UnlockBits(bmData);

关于c# - 为什么这个图像阈值在 windows 中有效,但在 mono/linux 中无效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44218033/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com