0

各ピクセルの RGB の値を抽出したこのコードがありますが、プロジェクトでさらに使用できるように、各 RGB 値を配列に格納する方法を考えています。これらの格納された RGB 値を、バックプロパゲーション アルゴリズムの入力として取得したいと考えています。

    import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import java.awt.Color;
import java.util.ArrayList;
import java.util.List;

public class PrintImageARGBPixels
{

public static void main(String args[])throws IOException
{
BufferedImage image = ImageIO.read(new File("C:\\Users\\ark\\Desktop\\project_doc\\logo_1004.jpg"));
int r=0,g=0,b=0;
int w = image.getWidth();
int h = image.getHeight();
System.out.println("Image Dimension: Height-" + h + ", Width-"+ w);
int total_pixels=(h * w);
ArrayList <Color> arr = new ArrayList<Color>();
for(int x=0;x<w;x++)
{
for(int y=0;y<h;y++)
{
int rgb = image.getRGB(x, y);
Color c = new Color(rgb);
r=c.getRed();
g=c.getGreen();
b=c.getBlue();
 }
}

Color co = new Color(r,g,b);
arr.add(co);
for(int i=0;i<total_pixels;i++)
System.out.println("Element 1"+i+1+", color: Red " + arr.get(i).getRed() + " Green +arr.get(i).getGreen()+ " Blue " + arr.get(i).getBlue());

}
}
4

3 に答える 3

2

このようなRGBオブジェクトを作成してみませんか

public class RGB {

    private int R, G, B;

    public RGB(int R, int G, int B){
        this.R = R;
        this.G = G;
        this.B = B;
    }

    public int getR() {
        return R;
    }

    public void setR(int r) {
        R = r;
    }

    public int getG() {
        return G;
    }

    public void setG(int g) {
        G = g;
    }

    public int getB() {
        return B;
    }

    public void setB(int b) {
        B = b;
    }

}

したがって、RGB オブジェクトを配列に格納して、後で使用することができます。

ご挨拶!

于 2013-11-12T10:47:32.060 に答える
2
// Store the color objects in an array
    int total_pixels = (h * w);
    Color[] colors = new Color[total_pixels];
    int i = 0;

    for (int x = 0; x < w; x++)
    {
      for (int y = 0; y < h; y++)
      {
        colors[i] = new Color(image.getRGB(x, y));
        i++;
      }
    }

// Later you can retrieve them
    for (int i = 0; i < total_pixels; i++)
    {
      Color c = colors[i];
      int r = c.getRed();
      int g = c.getGreen();
      int b = c.getBlue();
      System.out.println("Red" + r + "Green" + g + "Blue" + b);
    }

以下は無視してください、それは私の古い答えです

多次元配列を使用します。

[
 [255, 255, 255],
 [108, 106, 107],
 [100, 100, 55],
 ...
]

その後、各ピクセル [0][x] を参照して、色の値を取得できます。

于 2013-11-12T10:50:41.477 に答える
0

Key が int[] (x および y) で、value が別の int[] (r、g、b) である HashMap を使用すると、より簡単に実現できます。

于 2013-11-12T10:47:59.020 に答える