快速提取3D numpy数组中的ROI

时间:2018-05-23 21:52:13

标签: python arrays numpy indexing

我有一个包含一些视频数据的一维数组:

data=np.random.randint(0,high=255,size=(500*500*100),dtype=np.uint8)
imgs=np.reshape(data,(100, 500,500)) # 100 frames, 500x500 pixels

我想沿所有帧提取某些感兴趣的区域(roi)

idx=np.random.randint(0,high=500*500,size=(49, 300)) #300 rois, 49 points each
rois=imgs.reshape(100, -1)[:,idx]

我将每一个框架展平,然后沿着第一维度采用rois。真实的imgs数组比此处显示的大,并且之前的索引操作可能有点慢。如果我以不同的方式重塑imgs(见下文),rois.size是相同的,索引速度要快得多,但这会检索错误的数据

%timeit imgs.reshape(100, -1)[:,idx] # 13 ms
%timeit imgs.reshape(-1, 100)[idx, :] # 1.2 ms, much faster but wrong data

在我的真实代码中差异几乎是50倍。有没有办法快速索引imgs

1 个答案:

答案 0 :(得分:1)

通过对ROI像素进行排序并使用转置坐标,似乎可以节省至少一点时间:

>>> def f_pp(im2D, idx):
...     s = np.argsort(idx.ravel())
...     out = np.empty((*idx.shape, im2D.shape[0]), im2D.dtype)
...     out.reshape(-1, im2D.shape[0])[s] = im2D.T[idx.ravel()[s]]
...     return out
... 

# results are the same:
>>> np.all(f_pp(imgs.reshape(100, -1), idx) == np.moveaxis(imgs.reshape(100, -1)[:, idx], 0, 2))
True

>>> timeit("imgs.reshape(100, -1)[:, idx]", globals=globals(), number=100)
1.3392871069954708
# transposing alone is not enough:
>>> timeit("imgs.reshape(100, -1).T[idx]", globals=globals(), number=100)
1.3336799899989273
# but together with sorting I see a 2x speedup
>>> timeit("f_pp(imgs.reshape(100, -1), idx)", globals=globals(), number=100)
0.5874412529956317
# still much worse than if we had a more favorable memory layout in
# the first place
>>> timeit("imgs.reshape(-1, 100)[idx]", globals=globals(), number=100)
0.06296327701420523