【发布时间】:2016-08-16 13:47:24
【问题描述】:
我尝试在 2 张图像之间比较 100x100 像素的小块。
我在LockBits 操作之后使用本机memcmp pinvoke 调用,这是我的代码:
private void CompareBlock(Bitmap bmp1,Bitmap bmp2)
{
Rectangle lockRect = new Rectangle(0, 0, bmp1.Width, bmp1.Height);
BitmapData bmData = bmp1.LockBits(lockRect, ImageLockMode.ReadWrite, PixelFormat.Format32bppRgb);
BitmapData bmData2 = bmp2.LockBits(lockRect, ImageLockMode.ReadWrite, PixelFormat.Format32bppRgb);
IntPtr scan0 = bmData.Scan0;
IntPtr scan02 = bmData2.Scan0;
int BytesPerPixel = 4;//images are 32bpprgb
Size BlockSize = new Size(100,100);
if (memcmp(scan0, scan02, (BlockSize.Width * BlockSize.Height * BytesPerPixel))==0)//need to compare only the the first 100x100 pixels block.
{
Console.WriteLine("Equal");
//do somthing with the block;
}
}
我不确定我做错了什么,但程序实际上进入了条件,并打印了"Equal",这是不正确的(根据给定的图像)。
如果有任何帮助,我将不胜感激。
谢谢。
//代码更新测试
private void CompareBlock(Bitmap bmp1,Bitmap bmp2)
{
Rectangle lockRect = new Rectangle(0, 0, bmp1.Width, bmp1.Height);
BitmapData bmData = bmp1.LockBits(lockRect, ImageLockMode.ReadWrite, PixelFormat.Format32bppRgb);
BitmapData bmData2 = bmp2.LockBits(lockRect, ImageLockMode.ReadWrite, PixelFormat.Format32bppRgb);
IntPtr scan0 = bmData.Scan0;
IntPtr scan02 = bmData2.Scan0;
int BytesPerPixel = 4;
Size BlockSize = new Size(100,100);
for (int y = 0; y < BlockSize.Height; y++)
{
if (memcmp(scan0, scan02, BlockSize.Width * BytesPerPixel) == 0)//need to compare only the the first 100x100 pixels block.
{
scan0 = IntPtr.Add(scan0, stride);//not sure about that advancement
scan02 = IntPtr.Add(scan02, stride2);//not sure about that advancement
//do somthing with the block;
}
else
break;
}
}
【问题讨论】:
-
我几天前向需要锁定位图的人推荐了this project。它可能会帮助您解决问题。
标签: c# image-processing bitmap