2016-10-04 4 views
1

나는 기상 RGB 유형 BufferedImage을 가지고 있습니다. 나는 그들에 대한 평균적인 이미지를 원한다. 이것으로 각 픽셀의 평균값을 얻고 그 값에서 새로운 이미지를 만듭니다. 내가 시도한 것은 :자바에서 일련의 이미지의 평균 이미지를 얻으십시오

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; } 

는 다른 방법이 색상 클래스를 사용할 수 있습니다.

1

다른 해결책은 두 홀수의 경우를 처리하기 위해, 2 화의 마지막 멤버를 분할

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

이진 우측 쉬프트로

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

를 대체 할 수있다.

+0

멋진 비트 - twiddling! – weston