2012-02-07 50 views
5

我需要計算Android上java中兩個圖像之間的像素差異。問題是我有代碼返回不準確的結果。Android java百分比位圖兩個圖像之間的像素差異

例如我有3個非常相似的圖片,但它們返回的結果明顯不同: pic1 vs pic2 = 1.71%; pic1 vs pic3 = 0.0045%; pic2 vs pic3 = 36.7%。

BitmapFactory.Options opt = new BitmapFactory.Options(); 
    opt.inPreferredConfig = Bitmap.Config.ARGB_8888; 
    opt.inSampleSize = 5; 
    Bitmap mBitmap1 = BitmapFactory.decodeFile("/sdcard/pic1.jpg", opt); 
    Bitmap mBitmap2 = BitmapFactory.decodeFile("/sdcard/pic2.jpg", opt); 

    int intColor1 = 0; 
    int intColor2 = 0; 
    for (int x = 0; x < mBitmap1.getWidth(); x++) { 
     for (int y = 0; y < mBitmap1.getHeight(); y++) { 
      intColor1 = mBitmap1.getPixel(x, y); 
      intColor2 = mBitmap2.getPixel(x, y); 
      //System.out.print(" ("+ x + ","+ y +") c:" + intColor1); 
     } 
     String resultString = String.valueOf(intColor1); 

    } 
    //now calculate percentage difference 
    double razlika = (((double)intColor1 - intColor2)/intColor2)*100; 

} 

我認爲我需要爲兩個圖像的每個像素進行比較(intColor1(X,Y)與intColor2(X,Y)),但我怎麼能做到這一點,並在以後計算百分比差異?

+0

我必須獲取並列出所有類似的圖片將幫助你... – 2018-01-04 06:45:10

回答

2

您使用的百分比公式是錯誤的。例如,#333333與#333332幾乎相同(並且您的公式顯示它們的差異爲0.003%)。另外#323333與#333333幾乎相同,但是您的公式顯示它們有3%的差異。

您應該提取每個顏色像素(Color.red(),Color.green(),Color.blue())的每個組合位,然後計算每個像素的差異,然後獲取組合差異百分比。

雖然這種獲得兩幅圖像差異的方法簡單而有效,但它有一個很大的警告:如果圖像內容相同但向右移動一個像素(例如向右),則您的方法將顯示他們完全不同。

+0

那麼有沒有什麼有效的方法來計算Android上的圖像與百分比之間的差異?代碼將更有用!謝謝 – supermus 2012-02-08 09:30:10