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

有帮助吗?

解决方案

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top