這是方法:我試圖比較兩個使用LockBits的圖像,但它不工作 - 爲什麼?
public static Bitmap FastComparison(Bitmap bmp1,Bitmap bmp2)
{
Bitmap bmp3 = new Bitmap(512,512);
PixelFormat pxf = PixelFormat.Format24bppRgb;
Rectangle rect = new Rectangle(0, 0, bmp1.Width, bmp1.Height);
BitmapData bmpData1 = bmp1.LockBits(rect, ImageLockMode.ReadWrite, pxf);
BitmapData bmpData2 = bmp2.LockBits(rect, ImageLockMode.ReadWrite, pxf);
BitmapData bmpData3 = bmp3.LockBits(rect, ImageLockMode.ReadWrite, pxf);
IntPtr ptr1 = bmpData1.Scan0;
IntPtr ptr2 = bmpData2.Scan0;
IntPtr ptr3 = bmpData3.Scan0;
int numBytes = bmpData1.Stride * bmp1.Height;
byte[] rgbValues1 = new byte[numBytes];
Marshal.Copy(ptr1, rgbValues1, 0, numBytes);
bmp1.UnlockBits(bmpData1);
byte[] rgbValues2 = new byte[numBytes];
Marshal.Copy(ptr2, rgbValues2, 0, numBytes);
bmp2.UnlockBits(bmpData2);
for (int counter = 0; counter < rgbValues1.Length; counter += 3)
{
int dr, dg, db;
dr = (int)rgbValues1[counter] - (int)rgbValues2[counter];
dg = (int)rgbValues1[counter + 1] - (int)rgbValues2[counter + 1];
db = (int)rgbValues1[counter + 2] - (int)rgbValues2[counter + 2];
int error = dr * dr + dg * dg + db * db;
if (error < tolerancenumeric)
{
rgbValues1[counter] = 0;
rgbValues1[counter + 1] = 0;
rgbValues1[counter + 2] = 0;
}
}
Marshal.Copy(rgbValues1, 0, ptr3, numBytes);
return bmp1;
}
這是我如何使用/調用它在Form1中:
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
CloudEnteringAlert.tolerancenum = (int)numericUpDown1.Value;
pictureBox1.Image = CloudEnteringAlert.FastComparison(bitmapwithclouds, bitmapwithoutclouds);
}
我要的是改變公差所以最終我會得到一個導致圖像變量bmp1將只與黑色背景上的雲彩一起。
我做錯了什麼?
這是我現在改變它的新方法。現在即時只獲得黑色圖像:
public static Bitmap FastComparison(Bitmap bmp1,Bitmap bmp2)
{
Bitmap bmp3 = new Bitmap(512,512);
PixelFormat pxf = PixelFormat.Format24bppRgb;
Rectangle rect = new Rectangle(0, 0, bmp1.Width, bmp1.Height);
BitmapData bmpData1 = bmp1.LockBits(rect, ImageLockMode.ReadWrite, pxf);
BitmapData bmpData2 = bmp2.LockBits(rect, ImageLockMode.ReadWrite, pxf);
BitmapData bmpData3 = bmp3.LockBits(rect, ImageLockMode.ReadWrite, pxf);
IntPtr ptr1 = bmpData1.Scan0;
IntPtr ptr2 = bmpData2.Scan0;
IntPtr ptr3 = bmpData3.Scan0;
int numBytes = bmpData1.Stride * bmp1.Height;
byte[] rgbValues1 = new byte[numBytes];
Marshal.Copy(ptr1, rgbValues1, 0, numBytes);
bmp1.UnlockBits(bmpData1);
byte[] rgbValues2 = new byte[numBytes];
Marshal.Copy(ptr2, rgbValues2, 0, numBytes);
bmp2.UnlockBits(bmpData2);
for (int counter = 0; counter < rgbValues1.Length; counter += 3)
{
int dr, dg, db;
dr = (int)rgbValues1[counter] - (int)rgbValues2[counter];
dg = (int)rgbValues1[counter + 1] - (int)rgbValues2[counter + 1];
db = (int)rgbValues1[counter + 2] - (int)rgbValues2[counter + 2];
int error = tolerancenumeric * tolerancenumeric + tolerancenumeric + tolerancenumeric + tolerancenumeric * tolerancenumeric;//dr * dr + dg * dg + db * db;
if (error < tolerancenumeric)
{
}
else
{
rgbValues1[counter] = 0;
rgbValues1[counter + 1] = 0;
rgbValues1[counter + 2] = 0;
}
}
Marshal.Copy(rgbValues1, 0, ptr3, numBytes);
bmp3.UnlockBits(bmpData3);
return bmp3;
}
「不起作用」不是一個非常具有描述性的描述。告訴我們你的預期,告訴我們你收到的是什麼。 – nvoigt
出了什麼問題?如果差異超出容差,將設置哪些像素值? – JeffRSon
這裏有一些非常明顯的錯誤。最糟糕的是當結果存儲在bmp3而不是解鎖bmp3時返回bmp1。 –