如何将多个二维阵列堆叠成一个三维阵列?

2024-03-28 15:28:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我的代码是:

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

如你所见,我对图片中的每个通道都应用了一些过滤器。如何将它们堆叠回三维阵列?(=原始图像形状)

谢谢


Tags: to代码inimgforherestackchannel
2条回答

我先用所需的形状初始化变量:

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)

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

输出

^{pr2}$

相关问题 更多 >