2016-10-04 95 views
1

我有一套氣象RGB類型BufferedImage s。我想獲得他們的平均形象。由此,我的意思是獲得每個像素的平均值,並從這些值中創建一個新圖像。我試過的是:在java中獲取一組圖像的平均圖像

public void getWaveImage(BufferedImage input1, BufferedImage input2){ 
    // images are of same size that's why i'll use first one's width and height 
    int width = input1.getWidth(), height = input1.getHeight(); 

    BufferedImage output = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); 

    int[] rgb1 = input1.getRGB(0, 0, width, height, new int[width * height], 0, width); 
    int[] rgb2 = input2.getRGB(0, 0, width, height, new int[width * height], 0, width); 
    for(int i=0; i<width; i++){ 
     for(int j=0; j<height; j++){ 
     int rgbIndex = i * width + j; 
     rgb1[rgbIndex] = (rgb1[rgbIndex] + rgb2[rgbIndex])/2; 
     } 
    } 

    output.setRGB(0, 0, width, height, rgb1, 0, width); 
    return output; 
} 

我在做什麼錯了?先謝謝你。

輸入1:

enter image description here

輸入2:

enter image description here

輸出:

enter image description here

+0

首先定義你的平均含義...... – Tschallacka

+1

我不認爲使用''rgb1 [rgbIndex] + rgb2 [rgbIndex])/ 2''會給你兩種輸入顏色之間的顏色。 – f1sh

+0

@Tschallacka對不起,我現在添加了。 – halil

回答

5

希望每個平均COM顏色的分量,平均紅色,平均綠色,平均藍色。

相反,你正在平均整個int。

Color c1 = new Color(rgb1[rgbIndex]); 
Color c2 = new Color(rgb2[rgbIndex]); 
Color cA = new Color((c1.getRed() + c2.getRed())/2, 
        (c1.getGreen() + c2.getGreen())/2, 
        (c1.getBlue() + c2.getBlue())/2); 
rgb1[rgbIndex] = cA.getRGB(); 

這可能不是最有效的,由於創造這麼多的對象,這樣更直接的方式是像這樣:

public static int average(int argb1, int argb2){ 
    return (((argb1  & 0xFF) + (argb2  & 0xFF)) >> 1)  | //b 
      (((argb1 >> 8 & 0xFF) + (argb2 >> 8 & 0xFF)) >> 1) << 8 | //g 
      (((argb1 >> 16 & 0xFF) + (argb2 >> 16 & 0xFF)) >> 1) << 16 | //r 
      (((argb1 >> 24 & 0xFF) + (argb2 >> 24 & 0xFF)) >> 1) << 24; //a 
} 

用法:

rgb1[rgbIndex] = average(rgb1[rgbIndex], rgb2[rgbIndex]); 
3

如果您有:

int rgb1, rgb2; //the rgb value of a pixel in image 1 and 2 respectively 

「平均」顏色是:

int r = (r(rgb1) + r(rgb2))/2; 
int g = (g(rgb1) + g(rgb2))/2; 
int b = (b(rgb1) + b(rgb2))/2; 

int rgb = ((r & 0xFF) << 16) | ((g & 0xFF) << 8) | ((b & 0xFF) << 0); 

與下面的「幫手」的方法:

private static int r(int rgb) { return (rgb >> 16) & 0xFF; } 
private static int g(int rgb) { return (rgb >> 8) & 0xFF; } 
private static int b(int rgb) { return (rgb >> 0) & 0xFF; } 

或者您可以使用Color類,如果你不希望處理位操作。

1

另一種解決方案可以是具有

rgb1[rgbIndex] = ((rgb1[rgbIndex]>>1)&0x7f7f7f7f)+((rgb2[rgbIndex]>>1)&0x7f7f7f7f)+(rgb1[rgbIndex]&rgb2[rgbIndex]&0x01010101); 

二進制右移位,以取代

rgb1[rgbIndex] = (rgb1[rgbIndex] + rgb2[rgbIndex])/2; 

除以2,總和的最後一個成員來處理兩個奇數的情況下。

+0

好點兒 - 扭曲! – weston