Pregunta

Im writing a program which combines the RGB pixel values for 3 images, e.g. red pixel of image 1, green pixel of image 2 and blue pixel of image 3 and I want to then create a final image of it. Im using the code below, but this seems to be incrementing x2 and x3 whilst x1 is the same, i.e. not giving the right pixel value for same co-ordinate for each image.

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++) {

So I was wondering if anyone can tell me how to iterate through each of the 3 images on the same co-ordinate, so for example read 1, 1 of each image and record the red, green and blue value accordingly. Apologies if it doesnt make complete sense, its a bit hard to explain. I can iterate the values for one image fine but when I add in another, things start to go a bit wrong as obviously its quite a bit more complicated! I was thinking it might be easier to create an array and replace the according values in that just not sure how to do that effectively either.

Thanks

¿Fue útil?

Solución

If I understand your question correctly, perhaps you could try something like:

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

For a more readable solution:

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

My solution is based on the assumption that all 3 images are similar (same dimensions and type).

Otros consejos

You could try a double for, just to iterate over the coordinates. I know it's not functional but the idea may help.

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
    }
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top