def windows(a, w, _as_strided=stride_tricks.as_strided):
    windows_shape = tuple(sa - sw + 1 for sa, sw in zip(a.shape, w))
windows_shape += w
windows_strides = a.strides + a.strides
return _as_strided(a, windows_shape, windows_strides)

def rotation_matrix1(section, _windows=windows):
    rot_filters = windows(section.reshape(5, 5), (3, 3)).reshape(9, 9)
return rot_filters[(rot_filters.std(1).argmin()), :].mean()
Optimizing implementation of a rotating mask in numpy/scipy