How do I use Gimp / OpenCV Color to separate images into coloured RGB layers?
Asked Answered
F

4

1

I have a JPG image, and I would like to find a way to:

  • Decompose the image into red, green and blue intensity layers (8 bit per channel).
  • Colorise each of these now 'grayscale' images with its appropriate color
  • Produce 3 output images in appropriate color, of each channel.

For example if I have an image: dog.jpg

I want to produce: dog_blue.jpg dog_red.jpg and dog_green.jpg

I do not want grayscale images for each channel. I want each image to be represented by its correct color.

I have managed to use the decompose function in gimp to get the layers, but each one is grayscale and I can't seem to add color to it.

I am currently using OpenCV and Python bindings for other projects so any suitable code that side may be useful if it is not easy to do with gimp

Ferguson answered 6/10, 2013 at 20:9 Comment(0)
C
1

Maybe you already figured this one out, but here's for somebody who wants to "see" their separated channels in their own color (that is - red in red, green in green etc.).

Each channel is just a single value image, which may be interpreted as a monochromatic image. But you can "add color" to it by adding two fake empty channels (zero_channel below), and cv2.merge it into a multichannel image.

#!/usr/bin/env python

import cv2
import numpy as np
import os
import sys

SHOW = True
SAVE = True


def split_channels(filename):
    img = cv2.imread(filename)
    if len(img.shape) != 3 or img.shape[2] != 3:
        sys.stderr.write('{0}: not a correct color image'.format(filename))
        return
    channels = cv2.split(img)
    zero_channel = np.zeros_like(channels[0])
    red_img = cv2.merge([zero_channel, zero_channel, channels[2]])
    green_img = cv2.merge([zero_channel, channels[1], zero_channel])
    blue_img = cv2.merge([channels[0], zero_channel, zero_channel])
    if SHOW:
        cv2.imshow('Red channel', red_img)
        cv2.imshow('Green channel', green_img)
        cv2.imshow('Blue channel', blue_img)
        cv2.waitKey(0)
    if SAVE:
        name, extension = os.path.splitext(filename)
        cv2.imwrite(name+'_red'+extension, red_img)
        cv2.imwrite(name+'_green'+extension, green_img)
        cv2.imwrite(name+'_blue'+extension, blue_img)


def main():
    if len(sys.argv) < 2:
        print('Usage: {0} <rgb_image>...'.format(sys.argv[0]))
    map(split_channels, sys.argv[1:])


if __name__ == '__main__':
    main()
Conative answered 3/5, 2015 at 16:4 Comment(0)
Y
0

As the blue,green,red images each has 1 channel only.So, this is basically a gray-scale image. If you want to add colors in the dog_blue.jpg for example then you create a 3-channel image and copy the contents in all the channels or do cvCvtColor(src,dst,CV_GRAY2BGR). Now you will be able to add colors to it as it has become 3-channel image.

Yardmaster answered 7/10, 2013 at 5:10 Comment(2)
How do I colourise each of the new 3 channel imagesFerguson
If you want to make for example dog_blue.jpg which is 1 channel to 3 channel then Just create a 3 channel image using cvCreateImage and then do cvCvtColor(src,dst,CV_GRAY2BGR). I believe you are asking this.Yardmaster
C
0

You need the split image's channels. to do that you can use split function source

 // "channels" is a vector of 3 Mat arrays:
 vector<Mat> channels(3);
 // split img:
 split(img, channels);
 // get the channels (dont forget they follow BGR order in OpenCV)

 namedWindow("channelR",1);
 namedWindow("channelB",1);
 namedWindow("channelG",1);

 imshow("channelB",channels[0]);
 imshow("channelG",channels[1]);
 imshow("channelR",channels[2]);

 imwrite( "channelR.jpg", channels[2]);
 imwrite( "channelG.jpg", channels[1]);
 imwrite( "channelB.jpg", channels[0]);
Collenecollet answered 7/10, 2013 at 9:23 Comment(0)
T
0

In the BGR image, you have three channel. When you split the channel using the split() function, like B,G,R=cv2.split(img), then B,G,R becomes a single or monochannel image. So you need to add two extra channel with zeros to make it 3 channel image but activated for a specific color channel.

Teeters answered 1/4, 2019 at 4:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.