Take the 2-minute tour ×

Im looking to create a Java application that will take 3 grayscale images (each representing red, green and blue) and then merging them/flattening them/etc. to create one colour image, I was wondering if anyone knew if there are any existing algorithms or methods which I might be able to use? I understand there is a program called ImageJ which I have used before and it does exactly what im looking to; you choose 3 images and an RGB image can be created from them. I know this is done in Java by using a lookup table, this is something ive never encountered before so wouldnt even know where to begin.

If anyone has any ideas of the best way to approach it, existing algorithms, how I might be able to make my own, etc. that would be great. Im not looking for anyone to code for me, just to guide me in the right direction; my theory of iterating every pixel for each R, G and B grayscale image might not work?

Thanks for the help in advance

share|improve this question
I would try it and find out. –  Peter Lawrey Oct 20 '13 at 12:44

1 Answer 1

up vote 3 down vote accepted

Working in the sRGB colourspace, it is easy to implement a method that does this.

Consider the following method:

private static BufferedImage createColorFromGrayscale(BufferedImage red, BufferedImage green, BufferedImage blue){
    BufferedImage base = new BufferedImage(red.getWidth(), red.getHeight(), BufferedImage.TYPE_INT_ARGB);

    for(int x = 0;x < red.getWidth();x++){
        for(int y = 0; y < red.getHeight(); y++){
            int rgb = (red.getRGB(x, y) & 0x00FF0000) | (green.getRGB(x, y) & 0x0000FF00) | (blue.getRGB(x, y) & 0x000000FF);

            base.setRGB(x, y, (rgb | 0xFF000000));

    return base;

Creating a new base image, we create a colour component by using bitwise ANDs and ORs to create a 4 byte integer color in format ARGB which is assigned to the base image. Iterating through the whole image by the means of the for loops we are able to set each pixel of the resultant base image to the colours of each channel respectively.

This method assumes that all three images are equal in size. If images are not equal in size, you must handle that separately (e.g by means of stretching images before input or by modifying the method to accept images of different size.)

P.S: It might be more efficient to directly use one of the bufferedimage instances as the base image to save memory when dealing with large images...

share|improve this answer
Thanks for that, ive implemented something similar in a different way so that ive managed to read the value of every pixel for a chosen image. Im guessing I need to replicate that to do it for 2 more images and then somehow combine all 3 together? –  user2517280 Oct 20 '13 at 14:33
@user2517280 Yep... Just make sure you assign each channel from each image to the appropriate channel in the resultant image. –  CPU Terminator Oct 20 '13 at 14:44

Your Answer


