【发布时间】:2020-05-10 04:13:48
【问题描述】:
给定一个包含 6 个具有相同维度 (200, 200) 的二维数组的列表。每三个连续的数组可以堆叠成一个 3D 数组。
期望的输出:
array = (200, 200, 3, 2)
我熟悉 np.dstack:
n = 6
array = []
for i in range(n):
x = np.random.randn(n1, n2)
array.append(x)
array = np.dstack(array)
在处理大型数据集时应考虑速度。
编辑: 我创建了用于测试的图像: https://wetransfer.com/downloads/afb0a2fbfbd8b6047a68dad41b56a0d520200509184625/761355
改编帕迪的回答:
import numpy as np
from skimage import io
import glob
from natsort import natsorted
DIR = (Set folder path with the test images)
list_path = glob.glob(DIR + "/*.png")
list_path_sorted = natsorted(list_path)
array = []
for i in range(len(list_path_sorted)):
image = np.array( io.imread(list_path_sorted[i]) )
array.append(image)
a = np.dstack(array).reshape(200, 200, 3, 2)
a.shape
>>> (200, 200, 3, 2)
问题:第三维中的顺序不匹配。对此进行测试:
plt.imshow(a[:,:,0,0]) -> should show picture with A1
plt.imshow(a[:,:,2,0]) -> should show picture with A3
plt.imshow(a[:,:,0,1]) -> should show picture with B1
plt.imshow(a[:,:,2,1]) -> should show picture with B3
【问题讨论】:
-
问题是?
-
我坚持这样一个条件:一个列表中的三个连续数组形成一个 3D 堆栈。