python - 将图像堆叠为 numpy 数组更快(比预分配)?

标签 python arrays image numpy

我经常需要堆叠 2d numpy 数组(tiff 图像)。为此,我首先将它们附加到列表中并使用 np.dstack。这似乎是获得 3D 阵列堆叠图像的最快方法。但是,是否有更快/内存效率更高的方法?

from time import time
import numpy as np

# Create 100 images of the same dimention 256x512 (8-bit). 
# In reality, each image comes from a different file
img = np.random.randint(0,255,(256, 512, 100))

t0 = time()
temp = []
for n in range(100):
    temp.append(img[:,:,n])
stacked = np.dstack(temp)
#stacked = np.array(temp)  # much slower 3.5 s for 100

print time()-t0  # 0.58 s for 100 frames
print stacked.shape

# dstack in each loop is slower
t0 = time()
temp = img[:,:,0]
for n in range(1, 100):
    temp = np.dstack((temp, img[:,:,n]))
print time()-t0  # 3.13 s for 100 frames
print temp.shape

# counter-intuitive but preallocation is slightly slower
stacked = np.empty((256, 512, 100))
t0 = time()
for n in range(100):
    stacked[:,:,n] = img[:,:,n]
print time()-t0  # 0.651 s for 100 frames
print stacked.shape

# (Edit) As in the accepted answer, re-arranging axis to mainly use 
# the first axis to access data improved the speed significantly.
img = np.random.randint(0,255,(100, 256, 512))

stacked = np.empty((100, 256, 512))
t0 = time()
for n in range(100):
    stacked[n,:,:] = img[n,:,:]
print time()-t0  # 0.08 s for 100 frames
print stacked.shape

最佳答案

在与 otterb 共同努力后,我们得出结论,预分配数组是可行的方法。显然,性能瓶颈是数组布局,其中图像编号 (n) 是变化最快的索引。如果我们将 n 作为数组的第一个索引(默认为“C”顺序:第一个索引最慢,最后一个索引变化最快)我们将获得最佳性能:

from time import time
import numpy as np

# Create 100 images of the same dimention 256x512 (8-bit). 
# In reality, each image comes from a different file
img = np.random.randint(0,255,(100, 256, 512))

# counter-intuitive but preallocation is slightly slower
stacked = np.empty((100, 256, 512))
t0 = time()
for n in range(100):
    stacked[n] = img[n]
print time()-t0  
print stacked.shape

关于python - 将图像堆叠为 numpy 数组更快(比预分配)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23416888/

相关文章:

python - 在 tkinter 中创建音量 slider

python - 如何在抽象模型的 ManyToMany 字段中设置 related_name?

arrays - 从数组 Zod 中对象的键推断类型

image - 在 Flutter 中将具有自定义大小和旋转度的图像绘制到 CustomPaint 中

html - CSS - 纵横比填充图像

python - 如何将私有(private) python 包添加到 cloud composer 的要求中?

python - 正则表达式,选择最接近的匹配项

javascript - 什么是将事物分类为类型的良好 JavaScript 模式?

arrays - 收到错误 : “Cannot subscript a value of type ‘[Double]’ with an index of type ‘(Any) -> Int’ ”

python - 为什么有各种 JPEG 扩展?