我经常需要堆叠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
temp
中的所有数组都符合条件,则可以避免调用dstack
。此时,您只需调用stacked = np.concatenate(temp,axis=2)
来节省Python开销的小部分时间。如果您展示更多的代码,可能会有更好的解决方法,但是就所展示的代码而言,它已经几乎是最优的了。 - Danieltemp
中的所有数组都是3D的”。需要注意的是,这种节省对于非常大的temp
大小来说是微不足道的,可能每个数组约为2微秒。 - Daniel