如何使用python连接图像的灰色和rgb通道

时间:2018-06-04 10:30:54

标签: python opencv

我有以下代码:

import cv2
import numpy as np

img = cv2.imread('a.jpg')
gray = cv2.imread('b.jpg')
gray = cv2.cvtColor(gray, cv2.COLOR_BGR2GRAY)

print('a shape:', img.shape)  # a shape: (50,50,3)
print('b shape:', img.shape)  # b shape: (50,50)

result = np.concatenate((img, gray), axis=2)
print('result: ', result.shape)  # hope result shape: (50, 50, 4) 

我得到如下例外:

ValueError: all the input arrays must have same number of dimensions

我想得到result.shape =(50,50,4), 4个频道。如何修改我的代码?

1 个答案:

答案 0 :(得分:0)

我相信您正在寻找np.dstack

result = np.dstack((img, gray))