image - 扩展灰度图像以适应 RGB 图像

标签 image numpy

我需要扩展一个图像数组,该数组当前仅包含以下形状的灰度值:(640,480)(640,480,3) .最终我需要concatenate两者 - RGB numpy带灰度的数组 numpy大批。greyscalearray[..., np.newaxis]结果 (640,480,1) - 有没有办法添加 np.zeros(3,1)对于最后一个轴?

最佳答案

我想你想要:

RGB = np.dstack((grey, np.zeros_like(grey), np.zeros_like(grey)))

使用 np.zeros_like() 的好处是你得到一个匹配维度 的数组和 dtype 您的单 channel 灰度图像,而无需指定!

所以这里是完整的代码:
import numpy as np
grey = np.ones((64,48),dtype=np.uint8) 

print(grey.shape)      # prints (64, 48)

# Make 3-channel from singe-channel
RGB = np.dstack((grey, np.zeros_like(grey), np.zeros_like(grey))) 

print(RGB.shape)       # prints (64, 48, 3)
print(RGB[0,0])        # prints [1,0,0]
np.dstack()堆栈在 深度轴。它的 friend 是np.vstack()堆栈 垂直 连接一张图片 垂直下方 另一个,和 np.hstack()堆栈 水平 连接一张图片 水平旁边其他。

关于image - 扩展灰度图像以适应 RGB 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59219210/

相关文章:

java - 我应该为 Graphics.drawImage() 函数的 ImageObserver 参数使用 null 吗?

java - 如何从 RESTful Web 服务发送图像?

python - pandas pivot_table 百分位数/分位数

python - OpenCV Python 不支持 src 数据类型 17 错误

python - 如何优化循环以在 python 中生成新的随机泊松数组?

python - 在没有 opencv 的情况下使用 python 实现 Sobel 运算符

html - 在任何视口(viewport)中居中图像而不拉伸(stretch)

c# - 如何在 WPF 中绑定(bind)来自 XML 元素的图像

python - tensorflow 预测的顺序

python - 将一维数组转换为下三角矩阵