0

3 つの画像の RGB ピクセル値を組み合わせるプログラムを書いています。たとえば、画像 1 の赤のピクセル、画像 2 の緑のピクセル、画像 3 の青のピクセルです。以下のコードを使用していますが、これは x2 と x3 をインクリメントしているように見えますが、x1 は同じです。つまり、各画像の同じ座標に対して正しいピクセル値を与えていません。

for (int x = 0; x < image.getWidth(); x++) {
            for (int x2 = 0; x2 < image2.getWidth(); x2++) {
                for (int x3 = 0; x3 < image3.getWidth(); x3++) {

       for (int y = 0; y < image.getHeight(); y++) {
           for (int y2 = 0; y2 < image2.getHeight(); y2++) {
               for (int y3 = 0; y3 < image3.getHeight(); y3++) {

だから、同じ座標で3つの画像のそれぞれを反復処理する方法を誰かが教えてくれるかどうか疑問に思っていたので、たとえば、各画像の1、1を読み取り、それに応じて赤、緑、青の値を記録します。完全に意味をなさない場合はお詫びします。説明が少し難しいです。1 つの画像の値をうまく繰り返すことはできますが、別の画像を追加すると、明らかにかなり複雑になるため、少しおかしくなり始めます。配列を作成し、それに応じた値を置き換える方が簡単かもしれないと考えていましたが、それを効果的に行う方法もわかりません。

ありがとう

4

2 に答える 2

2

あなたの質問を正しく理解できれば、おそらく次のようなことを試すことができます。

public BufferedImage combine(final BufferedImage image1, final BufferedImage image2, final BufferedImage image3){
    final BufferedImage image = new BufferedImage(image1.getWidth(), image1.getHeight(), image1.getType());
    for(int x = 0; x < image.getWidth(); x++)
        for(int y = 0; y < image.getHeight(); y++)
            image.setRGB(x, y, new Color(new Color(image1.getRGB(x, y)).getRed(), new Color(image2.getRGB(x, y)).getGreen(), new Color(image3.getRGB(x, y)).getBlue()).getRGB());
    return image;
}

より読みやすいソリューションの場合:

public BufferedImage combine(final BufferedImage image1, final BufferedImage image2, final BufferedImage image3){
    final BufferedImage image = new BufferedImage(image1.getWidth(), image1.getHeight(), image1.getType());
    for(int x = 0; x < image.getWidth(); x++){
        for(int y = 0; y < image.getHeight(); y++){
            final int red = new Color(image1.getRGB(x, y)).getRed();
            final int green = new Color(image2.getRGB(x, y)).getGreen();
            final int blue = new Color(image3.getRGB(x, y)).getBlue();
            final int rgb = new Color(red, green, blue).getRGB();
            image.setRGB(x, y, rgb);
        }
    }
    return image;
}

私の解決策は、3 つの画像すべてが類似している (同じ寸法とタイプ) という仮定に基づいています。

于 2013-10-26T15:50:07.180 に答える
1

座標を反復処理するためだけに、double for を試すことができます。機能しないことはわかっていますが、アイデアが役立つ場合があります。

for(int i = 0; i < width; i++) {
    for(int j = 0; j < height; j++) {
         int R = image1.R(x, y);
         int G = image2.G(x, y);             
         int B = image3.B(x, y);
         // Some other stuff here
    }
}
于 2013-10-26T15:53:15.103 に答える