scipy convolve2d outputs wrong values

I think the problem is that you did not do what SciPy implemented. I won’t dwell on the details or the foundations but only provide you with a solution:

Reverse the kernel.

>>> import numpy as np

>>> arr = np.array([[0, 0, 0],
                    [1, 1, 2],
                    [1, 3, 0]])

>>> kernel = np.array([[4, 1, 1],
                       [0, 3, 3],
                       [2, 1, 2]])

>>> from scipy.signal import convolve2d

>>> convolve2d(arr, kernel[::-1, ::-1])
array([[ 0,  0,  0,  0,  0],
       [ 2,  3,  7,  4,  4],
       [ 5, 13, 14, 12,  0],
       [ 4, 14, 16,  6,  8],
       [ 1,  4,  7, 12,  0]])

>>> convolve2d(arr, kernel[::-1, ::-1], 'valid')
array([[14]])

Leave a Comment