我需要扩展一个图像数组,该数组当前仅包含以下形状的灰度值:(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/