You can use as_strided
for this pretty efficiently I think.
(我认为您可以非常有效地使用as_strided
。)
def window_nd(a, window, steps = None):
ashp = np.array(a.shape)
wshp = np.array(window).reshape(-1)
if steps:
stp = np.array(steps).reshape(-1)
else:
stp = np.ones_like(ashp)
astr = np.array(a.strides)
assert np.all(np.r_[ashp.size == wshp.size, wshp.size == stp.size, wshp <= ashp])
shape = tuple((ashp - wshp) // stp + 1) + tuple(wshp)
strides = tuple(astr * stp) + tuple(astr)
as_strided = np.lib.stride_tricks.as_strided
aview = as_strided(a, shape = shape, strides = strides)
return aview
EDIT: Generalizing the striding method as much as I can.
(编辑:尽可能广泛地概括跨步方法。)
For your specific question:
(对于您的特定问题:)
aview = window_nd(a, (288, 288), (144, 144))
z = aview.copy().reshape(-1, wx, wy) #to match expected output
print(z.shape, z.dtype) # z.shape should be (num_patches, 288, 288)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…