在numpy中,如何有效列出所有固定大小的子矩阵?


问题内容

我有一个任意的NxM矩阵,例如:

1 2 3 4 5 6
7 8 9 0 1 2
3 4 5 6 7 8
9 0 1 2 3 4

我想获得此矩阵中所有3x3子矩阵的列表:

1 2 3       2 3 4               0 1 2
7 8 9   ;   8 9 0   ;  ...  ;   6 7 8
3 4 5       4 5 6               2 3 4

我可以使用两个嵌套循环来做到这一点:

rows, cols = input_matrix.shape
patches = []
for row in np.arange(0, rows - 3):
    for col in np.arange(0, cols - 3):
        patches.append(input_matrix[row:row+3, col:col+3])

但是对于大的输入矩阵,这很慢。 有没有办法用numpy更快地做到这一点?

我已经看过了np.split,但是这给了我不重叠的子矩阵,而我想要所有可能的子矩阵,而不管它们是否重叠。


问题答案:

您需要一个窗口视图:

from numpy.lib.stride_tricks import as_strided

arr = np.arange(1, 25).reshape(4, 6) % 10
sub_shape = (3, 3)
view_shape = tuple(np.subtract(arr.shape, sub_shape) + 1) + sub_shape
arr_view = as_strided(arr, view_shape, arr.strides * 2
arr_view = arr_view.reshape((-1,) + sub_shape)

>>> arr_view
array([[[[1, 2, 3],
         [7, 8, 9],
         [3, 4, 5]],

        [[2, 3, 4],
         [8, 9, 0],
         [4, 5, 6]],

        ...

        [[9, 0, 1],
         [5, 6, 7],
         [1, 2, 3]],

        [[0, 1, 2],
         [6, 7, 8],
         [2, 3, 4]]]])

这样做的好处在于,您无需复制任何数据,而只是以不同的方式访问原始数组的数据。对于大型阵列,这可以节省大量内存。