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

如何使用python连接图像的灰度和rgb通道,python,opencv,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 = n

我有以下代码:

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

我想得到结果。shape=50,50,4,4个通道。如何修改我的代码?

我相信您正在寻找:

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