【问题标题】:Python - Stacking 3D images (stored as arrays) to create a 4D arrayPython - 堆叠 3D 图像(存储为数组)以创建 4D 数组
【发布时间】:2018-01-14 17:20:10
【问题描述】:

全部,

我有 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. 我正在使用 Python 3.6.4

【问题讨论】:

标签: python-3.x python-3.6


【解决方案1】:
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)

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

【讨论】:

    猜你喜欢
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 2018-10-16
    • 1970-01-01
    • 2021-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多