Python - 堆叠3D图像(存储为数组)以创建4D阵列

时间:2018-01-14 17:20:11

标签: python-3.x python-3.6

所有

我有25,000个RGB图像试图在python中堆叠以创建一个4D阵列。我的每个25k图像都是150x150x3阵列(dtype = uint16)。我想创建一个大小为[25000,150,150,3]的4D阵列。这是我到目前为止的伪代码:

import numpy as np
myfourdarray = np.empty[25000,150, 150, 3]
for idx in range(25000):
    img = read from file which returns a 150x150x3
    myfourdarray[idx] = img

上面的代码已经运行了8个小时,现在还在继续。 idx现在高达12000,代码仍在继续。这真的很慢。我用使用随机数生成的图像替换了文件读取部分代码,代码表现相同 - 缓慢并且持续数小时。有人可以帮我弄清楚我做错了什么吗?是否有更好,更快的方法来堆叠3D阵列并创建4D阵列?

由于 P. S. Am使用Python 3.6.4

1 个答案:

答案 0 :(得分:0)

import numpy as np

myfourdarray = []

for idx in range(25000):

    img = read from file which returns a 150x150x3

    myfourdarray.append(img)

out = np.stack(myfourdarray, axis = 0)

可能更快,但是我没有验证它。