D
D
Daniel2017-05-06 09:24:28
Java
Daniel, 2017-05-06 09:24:28

How to split an image into three RGB layers?

Hello, how can I split one image into 3 images containing one channel from the original image and collect these three images back?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
L
leopik, 2017-05-06
@leopik

If it is possible to read the image pixel by pixel, then each pixel is RRGGBB in hex representation. Accordingly, to get the red channel, you need to apply a mask of 110000 to each pixel. Each channel has its own mask - green 001100 and blue - 000011.
To assemble it back - sum the three corresponding pixels from the three representations

L
Labunsky, 2017-05-06
@Labunsky

public static BufferedImage[] splitToRGB(BufferedImage original) {
    BufferedImage R =  new BufferedImage(
            original.getWidth(), original.getHeight(),
            BufferedImage.TYPE_INT_RGB
    );
    BufferedImage G =  new BufferedImage(
            original.getWidth(), original.getHeight(),
            BufferedImage.TYPE_INT_RGB
    );
    BufferedImage B =  new BufferedImage(
            original.getWidth(), original.getHeight(),
            BufferedImage.TYPE_INT_RGB
    );

    for (int x = 0; x < original.getWidth(); x++)
        for (int y = 0; y < original.getHeight(); y++) {
            final int rgb = original.getRGB(x, y);

            R.setRGB(x, y, rgb & 0xff0000);
            G.setRGB(x, y, rgb & 0xff00);
            B.setRGB(x, y, rgb & 0xff);
        }

    return new BufferedImage[]{R, G, B};
}

public static BufferedImage mergeRGB(BufferedImage R, BufferedImage G, BufferedImage B) {
    BufferedImage original =  new BufferedImage(
            R.getWidth(), R.getHeight(),
            BufferedImage.TYPE_INT_RGB
    );

    for (int x = 0; x < original.getWidth(); x++)
        for (int y = 0; y < original.getHeight(); y++) {
            final int rgb = R.getRGB(x, y) | G.getRGB(x, y) | B.getRGB(x, y);
            original.setRGB(x, y, rgb);
        }

    return original;
}

K
Kuznetsov Nikita, 2017-05-06
@thrhrthr

If you have photoshop
9918b5dc1ab54fe4b7afbe7500cc907e.png

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question