我需要扩展一个图像数组,当前只包含灰度值,形状为:(640,480)
,要将其扩展为 (640,480,3)
。最终,我需要将rgb的numpy
数组和灰度的numpy
数组进行连接(concatenate)
。
greyscalearray[..., np.newaxis]
的结果是 (640,480,1)
,是否有办法在最后一个轴上添加 np.zeros(3,1)
?
我需要扩展一个图像数组,当前只包含灰度值,形状为:(640,480)
,要将其扩展为 (640,480,3)
。最终,我需要将rgb的numpy
数组和灰度的numpy
数组进行连接(concatenate)
。
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
匹配的数组,而无需指定任何一个!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()
函数将数组沿深度(即z轴)方向堆叠起来。它的“伙伴”包括np.vstack()
函数,它可以将多个图像垂直堆叠在一起,即一个图像在另一个图像下面;还有np.hstack()
函数,它可以将多个图像水平堆叠在一起,即一个图像在另一个图像旁边。
np.zeros_like(grey) + 255
。很高兴它能对您有帮助 - 祝您的项目好运。 - Mark Setchell