scipy convolve2d outputs wrong values scipy convolve2d outputs wrong values numpy numpy

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]])


The expression (X[:3,:3]*K).sum() is not correct. For convolution, you have to reverse the kernel, e.g. (X[:3,:3]*K[::-1,::-1]).sum()