Python , 卷积

Python , Convolution

我想在每三张幻灯片后接下mat(矩阵)的部分。我的代码是在一张幻灯片后加入矩阵的一部分。 我想取矩阵的前 3 x 3 部分,然后向右滑动 3 列并取 3 x 3 部分,依此类推,遍历所有矩阵。

n1=np.array(([1,2,3,4,5,6],[7,8,9,10,11,12],[12,13,14,15,16,17],[18,19,20,21,22,23],[24,25,26,27,28,29],[30,31,32,33,34,35]))
print(n1)
k=3
    for i in range(3):
        for j in range(3):
            mat = n1[i:i+k, j:j+k]
            print(mat)

您可以像下面的代码一样对行和列使用切片... 查看文档 here

import numpy as np

n1=np.array(([1,2,3,4,5,6],[7,8,9,10,11,12],[12,13,14,15,16,17],[18,19,20,21,22,23],[24,25,26,27,28,29],[30,31,32,33,34,35]))
print(n1)

MAX_WIDTH = 3
MAX_HEIGHT = 3

for r in range(0, len(n1[0]), MAX_HEIGHT):
    for c in range(0, len(n1), MAX_WIDTH):
        print(n1[r:r+MAX_HEIGHT, c:c+MAX_WIDTH])