2012-12-11 67 views
1

得到我所用的follwing代碼一個圖像的RGB值片斷比較兩個圖像的RGB顏色值的Android

int[] pix = new int[picw * pich]; 
       bitmap.getPixels(pix, 0, picw, 0, 0, picw, pich); 

       int R, G, B,Y; 

       for (int y = 0; y < pich; y++){ 
       for (int x = 0; x < picw; x++) 
        { 
        int index = y * picw + x; 
        int R = (pix[index] >> 16) & 0xff;  //bitwise shifting 
        int G = (pix[index] >> 8) & 0xff; 
        int B = pix[index] & 0xff; 

        //R,G.B - Red, Green, Blue 
         //to restore the values after RGB modification, use 
    //next statement 
        pix[index] = 0xff000000 | (R << 16) | (G << 8) | B; 
      }} 

我想比較兩個圖像,我知道,比較像素值會比較昂貴。我也分析了OpenCV庫,但我不會 進入我的要求。

是否有任何算法來比較使用Android中的RGB值的圖像?

是否有其他方法比較RGB值?

感謝,

回答

0

我不知道你的要求是什麼,但是如果你想要做的是比較兩個圖像(RGB)色調色板,你可能想使用PaletteFactory方法從Apache Commons Imaging (fka "Sanselan")

PaletteFactory方法建立集合(int[]List<>),然後可以迭代。我不確定你需要做什麼樣的比較,但是一個相當簡單的例子, makeExactRgbPaletteSimple(),將是:

final File img1 = new File("path/to/image_1.ext") 
final File img2 = new File("path/to/image_2.ext") 
final PaletteFactory pf; 
final int MAX_COLORS = 256; 
final Palette p1 = pf.makeExactRgbPaletteSimple(img1, MAX_COLORS); 
final Palette p2 = pf.makeExactRgbPaletteSimple(img2, MAX_COLORS); 

final ArrayList<Int> matches = new ArrayList<Int>(Math.max(p1.length(), p2.length())); 
int matchPercent; 

// Palette objects are pre-sorted, afaik 

if ((p1 != null) && (p2 != null)) { 
    if (p1.length() > p2.length()) { 
    for (int i = 0; i < p1.length(); i++) { 
     final int c1 = p1.getEntry(i); 
     final int c2 = p2.getPaletteIndex(c1); 
     if (c2 != -1) { 
     matches.add(c1); 
     } 
    } 
    matchPercent = ((int)((float)matches.size())/((float)p1.length) * 100)) 
    } else if (p2.length() >= p1.length()) { 
    for (int i = 0; i < p1.length(); i++) { 
     final int c1 = p2.getEntry(i); 
     final int c2 = p1.getPaletteIndex(c1); 
     if (c2 != -1) { 
     matches.add(c1); 
     } 
    } 
    matchPercent = ((int)((float)matches.size())/((float)p2.length) * 100)) 
    } 
} 

這只是可能會或可能不會進行編譯,並且幾乎可以肯定不是你在比較邏輯方面找什麼小例子。

它基本上是檢查每個成員p1是否也是p2的成員,如果是,則將其添加到matches。希望邏輯是正確的,沒有保證。 matchPercentPalette s中存在的顏色的百分比。

這可能是而不是你想要的比較方法。這只是一個簡單的例子。

你一定會需要用第二個參數發揮各地makeExactRgbPaletteSimple()int max,因爲我選擇了256任意 - 記住,該方法將(煩人,IMO)返回null如果max太小。

我會建議從源建設,因爲回購協議尚未更新一段時間。該項目絕對不成熟,但對於中型圖像和純Java來說,它相當小,速度相當快。

希望這會有所幫助。