2015-06-26 34 views
-1

我有不同的軟件在多個文件夾中生成多個標籤。標籤的大小是相同的,但它們的透明度標籤是不同的。理想上它們是相同的,但是當我使用我的內部應用程序比較時,它會引發錯誤,說它們不匹配。將圖像與透明度級別的變化進行比較並比較其中的文本

我知道類似的問題,但他們並不完全符合我的問題。我需要用c#實現相關的算法。

我正在探索一些API的AForge.Net,ImageMagick。

直到現在我比較字節

filename1 = Path.Combine(directory.ToString(), new1[i].ToString()); 
filename2 = Path.Combine(directory1.ToString(), new2[i].ToString()); 

using (Bitmap bm1 = new Bitmap(filename1)) 
{ 
    using (Bitmap bm2 = new Bitmap(filename2)) 
    { 
     // Make a difference image. 
     int wid = Math.Min(bm1.Width, bm2.Width); 
     int hgt = Math.Min(bm1.Height, bm2.Height); 
     Bitmap bm3 = new Bitmap(wid, hgt); 

     // Create the difference image. 
     bool are_identical = true; 
     Color eq_color = Color.White; 
     Color ne_color = Color.Red; 
     for (int x = 0; x < wid; x++) 
     { 
      for (int y = 0; y < hgt; y++) 
      { 
       //if (bm1.GetPixel(x, y).Equals(bm2.GetPixel(x, y))) 
       if (bm1.GetPixel(x, y) != (bm2.GetPixel(x, y))) 
       { 
        //bm3.SetPixel(x, y, eq_color); 
        bm3.SetPixel(x, y, ne_color); 
        are_identical = false; 

       } 
       else 
       { 
        //bm3.SetPixel(x, y, ne_color); 
        //are_identical = false; 

       } 
      } 
      //I kept the code here before 
     } 
     if (!are_identical) 
     { 
      bm3.Save(@"C:\Users\XPS Files\DiffrenceofImages" + new1[i]); 
     } 
     // Display the result. 
     //picResult.Image = bm3; 
     //bm3.Save(@"C:\Users\XPS Files\DiffrenceofImages\" + new1[i]); 
     this.Cursor = Cursors.Default; 
     if ((bm1.Width != bm2.Width) || (bm1.Height != bm2.Height)) are_identical = false; 
     if (are_identical) 
     { 
      //richTextBox1.Text = string.Format("Images are identical", "\r\n"); 
      sb.AppendLine("Image Name=" + new1[i] + " are identical at both folder"); 
     } 
     else 
     { 
      //richTextBox1.Text = string.Format("Images are NOT MATCHING", "\r\n"); 
      sb.AppendLine("Image Name=" + new1[i] + " are Not Matching at both folder"); 
     } 

     //sb.AppendLine(f1.Name + ": " + (equal ? "Images are equal" : "Images are NOT equal")); 

    } 
} 
result.Add(sb.ToString()); 
+0

看來你的代碼有問題。但是,除非我們有[可以重現問題的代碼或信息](http://stackoverflow.com/help/mcve),否則我們無法提供幫助。否則,我們只是盲目猜測。 – gunr2171

+0

請提供一些你已經做了什麼的代碼,以及你的代碼失敗了。這聽起來像你只需要忽略alpha通道,但這只是第一個想法。 – Nitram

+0

爲什麼是負面的?我的代碼爲emgucv圖片位圖=新圖片(@「D:\ red3.bmp」); 圖片 bitmap1 =新圖片(@「D:\ red3.bmp」); Hsv lowerLimit = new Hsv(0,0,200); Hsv upperLimit = new Hsv(5,255,255); var imageHSVDest = bitmap.InRange(lowerLimit,upperLimit); CvInvoke.cvShowImage(「imageHSVDest」,imageHSVDest); bitmap.AbsDiff(bitmap1); –

回答

0

我不知道從你的問題,如果你這樣做,或者不這樣做,想使用ImageMagick的。如果這樣做,則可以使用+matte-alpha off禁用Alpha /透明度通道。

所以,如果我創建了兩個紅色的圖像,一個徑向漸變透明度是這樣的:

convert -size 500x500 xc:red \(radial-gradient:black-gray90 -sigmoidal-contrast 10,50% \) -compose copy-opacity -composite radial.png 

enter image description here

和一個帶有直梯度透明度是這樣的:

convert -size 500x500 xc:blue \(gradient:white-gray40 \) -compose copy-opacity -composite straight.png 

enter image description here

我就可以比較他們使用ImageMagick而忽視這樣的透明的區別:

convert radial.png straight.png +matte -metric AE -compare format "Count of differing pixels:%[distortion]" info: 
Count of differing pixels:0 

該命令說... 負荷radial.pngstraight.png到圖像棧和刪除都透明度,然後計算和報告不同像素的總數 - 由於透明度已被刪除,因此爲零。

+0

對不起,延遲迴復,爲什麼我仍然面臨問題。我在不同的文件夾中有相同名稱的圖像,我需要比較它們。他們是黑白圖像。 (具體如本鏈接所示,例如http://info.pcforms.com/DIY-Printing-blog/bid/26211/Why-can-tI-tape-over-the-barcode-on-my-USPS-運輸標籤)由於構建問題,儘管圖像containts是相同的,但一些時間間隔和字體大小在兩個圖像之間變化。我的顧客對結果不滿(顯示不同),因爲在肉眼看來他們看起來都一樣。這個問題我想在這裏解決。我嘗試了OCR,但顯示差異。 –

相關問題