2013-12-22 32 views
-1

這是方法:我試圖比較兩個使用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; 
     } 
+1

「不起作用」不是一個非常具有描述性的描述。告訴我們你的預期,告訴我們你收到的是什麼。 – nvoigt

+0

出了什麼問題?如果差異超出容差,將設置哪些像素值? – JeffRSon

+0

這裏有一些非常明顯的錯誤。最糟糕的是當結果存儲在bmp3而不是解鎖bmp3時返回bmp1。 –

回答

1

看來你不設置任何像素值的像素超過你的容忍度。在其他情況下,您將它們設置爲黑色。黑色實際上是默認的,所以你只需要設置「錯誤」像素。

此外,你應該返回bmp3(這需要解鎖,順便說一句),而不是bmp1。

最後,應該創建bmp3的大小爲bmp1或bmp2(它們需要相同)。

+0

JeffRSon我改變了方法(更新我的問題的變化),但我想設置像素/顏色的rgbValues1是錯誤的IF內。你能告訴我如何解決它? – user3117033

+0

bmp3是512,512它的大小bmp1和bmp2 – user3117033

+0

那麼'rgbValues1 [counter] = dr; rgbValues1 [counter + 1] = dg; rgbValues1 [counter + 2] = db;'?或複製bmp1的值(在覆蓋rgbvalue1之前) – JeffRSon