How Do I Use Gimp / Opencv Color To Separate Images Into Coloured Rgb Layers?
Solution 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 pythonimport cv2
import numpy as np
import os
import sys
SHOW = True
SAVE = Truedefsplit_channels(filename):
img = cv2.imread(filename)
iflen(img.shape) != 3or 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)
defmain():
iflen(sys.argv) < 2:
print('Usage: {0} <rgb_image>...'.format(sys.argv[0]))
map(split_channels, sys.argv[1:])
if __name__ == '__main__':
main()
Solution 2:
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.
Solution 3:
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]);
Solution 4:
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.
Post a Comment for "How Do I Use Gimp / Opencv Color To Separate Images Into Coloured Rgb Layers?"