也许你已经想出了这个,但是这里是为那些想要以自己的颜色“看到”他们分离的通道的人(即 - 红色中的红色,绿色中的绿色等)。
每个通道只是一个单值图像,可以解释为单色图像。但是您可以通过添加两个假的空通道(zero_channel
如下)为其“添加颜色”,并将其添加cv2.merge
到多通道图像中。
#!/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()