2 回答
TA贡献1793条经验 获得超6个赞
scipy.signal的convolve实际上是你想要的:
from scipy.signal import convolve
convolve(vals[None, :, :], w0)[:, 1:-1, 1:-1]
Out[]:
array([[[ 1, -9, 5, -2, 5],
[ -3, 9, -13, 13, -10],
[ -2, -2, 9, -4, 1],
[ 9, -3, 5, -5, -5],
[ 0, 1, -4, -1, 4]],
[[ -5, 4, 5, 3, 0],
[ 4, -2, -3, -7, -5],
[ -1, 3, 14, -3, 9],
[ 10, -12, 11, -16, 7],
[ 4, -4, 8, 9, 6]],
[[ -6, 7, 1, 8, 2],
[ 5, -11, 19, -12, -3],
[ 7, 0, -11, 8, -9],
[ -9, 13, -14, -10, 5],
[ -4, -7, 2, 0, 8]]])
TA贡献2019条经验 获得超9个赞
你可以看看scipy.ndimage.filters.convolve
例如:
>>> a = np.array([[1, 2, 0, 0],
.... [5, 3, 0, 4],
.... [0, 0, 0, 7],
.... [9, 3, 0, 0]])
>>> k = np.array([[1,1,1],[1,1,0],[1,0,0]])
>>> from scipy import ndimage
>>> ndimage.convolve(a, k, mode='constant', cval=0.0)
array([[11, 10, 7, 4],
[10, 3, 11, 11],
[15, 12, 14, 7],
[12, 3, 7, 0]])
添加回答
举报