简体   繁体   English

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

[英]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.

相关问题 Python 将 3 通道 rgb 彩色图像更改为 1 通道灰色的速度有多快? - How fast in Python change 3 channel rgb color image to 1 channel gray? 如何使用 rgb 对 python 中的图片进行灰度化? - how to gray scale a picture in python using rgb? 如何在另一个图像上合成一个带有alpha通道的图像(RGB,没有alpha通道)? 使用python PIL - How to compose an image with an alpha channel over another image(RGB, without alpha channel)? Using python PIL 如何使用opencv python将红色通道与灰度图像合并? - How to merge red channel with gray scale image with opencv python? 如何使用opencv为python编写灰色(1通道)图像 - how to write gray (1-channel) image with opencv for python 如何使用Scikit-Image库从Python中的RGB图像中提取绿色通道? - How to extract green channel from RGB image in Python using Scikit-Image library? 我如何在 python 中使用 for 循环打印二维数组中的特定列和行,并且还想将灰色图像更改为 RGB - How I can print specific cols and row from 2d array using for loop in python and also want to change gray image to RGB 使用PIL或Scipy将Python图像从RGB转换为单通道 - Convert Python image to Single Channel from RGB using PIL or Scipy 我想使用Python将32 * 32 * 3 rgb图像转换为32 * 32 * 1维度的灰度 - I want to convert 32*32*3 rgb image to gray scale with 32*32*1 dimension using Python 无法使用 scikit 图像将 RGB 图像转换为灰度 - Not able to convert RGB image to gray scale using scikit image
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM