python - How do I use Gimp / OpenCV Color to separate images into coloured RGB layers? -
i have jpg image, , find way to:
- decompose image red, green , blue intensity layers (8 bit per channel).
- colorise each of these 'grayscale' images appropriate color
- produce 3 output images in appropriate color, of each channel.
for example if have image: dog.jpg
i want produce: dog_blue.jpg dog_red.jpg , dog_green.jpg
i not want grayscale images each channel. want each image represented correct color.
i have managed use decompose function in gimp layers, each 1 grayscale , can't seem add color it.
i using opencv , python bindings other projects suitable code side may useful if not easy gimp
maybe figured 1 out, here's wants "see" separated channels in own color (that - red in red, green in green etc.).
each channel single value image, may interpreted monochromatic image. can "add color" adding 2 fake empty channels (zero_channel
below), , cv2.merge
multichannel image.
#!/usr/bin/env python import cv2 import numpy 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 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()
Comments
Post a Comment