繁体   English   中英

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

[英]How to stack multiple numpy 2d arrays into one 3d array?

这是我的代码:

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

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

谢谢

您可以使用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)

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

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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM