0

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

3
  • 1
    Are the images of the same size? What would you want to do if they werent? Commented Oct 26, 2013 at 15:46
  • They are the same size yes, its to combine multiple bands of the same image. I know if theyre not the same size it could be problematic but for this program I dont need to take that into consideration :) Commented Oct 26, 2013 at 15:48
  • I believe Josh M has provided your answer then. Commented Oct 26, 2013 at 15:54

2 Answers 2

2

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).

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks a lot, when I try that however, the saved image is just the same as the first inputted image, any ideas why that is?
Are you sure you are correctly invoking the method? BufferedImage combinedImage = combine(img1, img2, img3);?
I forgot to add static which then gave me an error but its working now :) The image is coming out rather blue but im guessing thats to be expected? Either way its working :) Thank you!
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.