[英]How concatenate gray and rgb channel of image using python
I have following code: 我有以下代码:
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)
I get a exception as following : 我得到如下异常:
ValueError: all the input arrays must have same number of dimensions
I want to get result.shape = (50, 50, 4), 4 channels . 我想得到result.shape =(50,50,4), 4个通道 。 How to revise my code? 如何修改我的代码?
我相信你正在寻找np.dstack :
result = np.dstack((img, gray))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.