繁体   English   中英

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

[英]How concatenate gray and rgb channel of image using python

我有以下代码:

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个通道 如何修改我的代码?

我相信你正在寻找np.dstack

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM