C# 为什么这个图像阈值可以在windows中工作,而不能在mono/linux中工作?
我有一个相对简单的阈值函数,它使用不安全的代码。这在Windows中很有效,但是在linux上的Mono中,没有阈值发生。调试很困难,因为它仅在Linux上,我检查了bitsPerPixel是否正确,高度C# 为什么这个图像阈值可以在windows中工作,而不能在mono/linux中工作?,c#,linux,image,mono,unsafe,C#,Linux,Image,Mono,Unsafe,我有一个相对简单的阈值函数,它使用不安全的代码。这在Windows中很有效,但是在linux上的Mono中,没有阈值发生。调试很困难,因为它仅在Linux上,我检查了bitsPerPixel是否正确,高度h、宽度w和跨步ws是否正确 我还能做些什么来缩小范围,或者这里有一个常见的陷阱吗 public void threshold(Bitmap bmp, int thresh) { var bitsPerPixel = Image.GetPixelForma
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);
}
公共无效阈值(位图bmp,int thresh)
{
var bitsPerPixel=Image.GetPixelFormatSize(bmp.PixelFormat)/8;
BitmapData bmData=bmp.LockBits(新矩形(0,0,bmp.Width,bmp.Height),
ImageLockMode.ReadOnly,bmp.PixelFormat);
不安全的
{
byte*p=(byte*)(void*)bmData.Scan0.ToPointer();
int h=bmp.高度;
int w=bmp.宽度;
intws=bmData.Stride;
对于(int i=0;i(字节)阈值)?255:0;
}
}
}
}
bmp.解锁位(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);
公共不安全无效阈值(位图bmp,int thresh)
{
var bmData=bmp.LockBits(新矩形(0,0,bmp.Width,bmp.Height),ImageLockMode.ReadWrite,bmp.PixelFormat);
var bitsPerPixel=Image.GetPixelFormatSize(bmp.PixelFormat);
var p=(字节*)bmData.Scan0.ToPointer();
对于(int i=0;i阈值)?255:0);
数据[1]=(字节)((数据[1]>阈值)?255:0);
数据[2]=(字节)((数据[2]>阈值)?255:0);
}
}
bmp.解锁位(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);
公共不安全无效阈值(位图bmp,int thresh)
{
var bmData=bmp.LockBits(新矩形(0,0,bmp.Width,bmp.Height),ImageLockMode.ReadWrite,bmp.PixelFormat);
var bitsPerPixel=Image.GetPixelFormatSize(bmp.PixelFormat);
var p=(字节*)bmData.Scan0.ToPointer();
对于(int i=0;i阈值)?255:0);
数据[1]=(字节)((数据[1]>阈值)?255:0);
数据[2]=(字节)((数据[2]>阈值)?255:0);
}
}
bmp.解锁位(bmData);
似乎GDIPlus.GdipBitmapLockBits
符合传递给Bitmap.LockBits
的ImageLockMode
类型,我必须比较MS参考源如何处理该标志…您应该在处填充一个bug以报告不一致性。似乎GDIPlus.GdipBitmapLockBits
符合s将ImageLockMode
类型传递到Bitmap.LockBits
,我必须比较MS参考源如何处理该标志…您应该在处填充一个bug以报告不一致性。