也许你已经想通了这一个了,但在这里是谁想要“看到”自己的颜色的分离通道(即人 - 红的红,绿绿色等)。
每个通道只是一个单值图像,可能被解释为单色图像。但是,您可以通过将两个虚假的空通道(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()
如何colourise每个新的3个通道图像 – Steve
的。如果你想例如dog_blue.jpg为1通道3通道然后使用** cvCreateImage只需创建一个3通道图像**,然后做cvCvtColor(src,dst,CV_GRAY2BGR)。我相信你在问这个。 – Aizen