簡體   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