如何通过块循环ndarray

时间:2019-07-03 10:11:45

标签: numpy-ndarray

我想对ndarray进行过滤,就像scipy.signal.medfilt

x = np.arange(1, 26).reshape((5, 5))
y = np.copy(x)

kernel_size = 3
radius = kernel_size // 2
for index, val in np.ndenumerate(x):
    top = 0 if index[0] < radius else index[0] - radius
    bottom = x.shape[0] if index[0] + radius >= x.shape[0] \
        else index[0] + radius + 1
    left = 0 if index[1] < radius else index[1] - radius
    right = x.shape[1] if index[1] + radius >= x.shape[1]\
        else index[1] + radius + 1

    print(f"{x[top:bottom, left:right]}")

    y[index] = x[top:bottom, left:right].mean()

可以将迭代向量化吗?或更有效的代码?

0 个答案:

没有答案