如何将多个numpy 2d数组堆叠到一个3d数组中?

时间:2018-12-21 19:11:58

标签: python numpy matplotlib

这是我的代码:

img = imread("lena.jpg")
for channel in range(3):
    res = filter(img[:,:,channel], filter)
    # todo: stack to 3d here

如您所见,我正在为图片中的每个通道应用一些过滤器。如何将它们堆叠回3D阵列? (=原始图像形状)

谢谢

2 个答案:

答案 0 :(得分:1)

您可以使用np.dstack

import numpy as np

image = np.random.randint(100, size=(100, 100, 3))

r, g, b = image[:, :, 0], image[:, :, 1], image[:, :, 2]

result = np.dstack((r, g, b))

print("image shape", image.shape)
print("result shape", result.shape)

输出

image shape (100, 100, 3)
result shape (100, 100, 3)

答案 1 :(得分:1)

我之前用所需的形状初始化了一个变量

img = imread("lena.jpg")
res = np.zeros_like(img)     # or simply np.copy(img)
for channel in range(3):
    res[:, :, channel] = filter(img[:,:,channel], filter)
相关问题