'Why is the result of my conv2D function centrally symmetric with the result of scipy?

I am trying to implement a conv2D function just by using numpy. But compared with scipy.sig.convolve2d, my result is centrosymmetric with its result. Could someone help me? here is my function.

import numpy as np
import scipy.signal as sig

def conv2D(image, kernel):
    i_r = image.shape[0]
    i_c = image.shape[1]
    
    k_r = kernel.shape[0]
    k_c = kernel.shape[1]
    
    # padding
    pad_height = max((i_r - 1) + k_r - i_r, 0)
    pad_width = max((i_c - 1) + k_c - i_c, 0)
    
    pad_top = int(pad_height / 2)
    pad_bottom = int(pad_height - pad_top)
    pad_left = int(pad_width / 2)
    pad_right = int(pad_width - pad_left)
    
    top = np.zeros((pad_top, i_c))
    bottom = np.zeros((pad_bottom, i_c))
    img =  np.vstack((top, image))
    img = np.vstack((img, bottom))
    
    left = np.zeros((i_r + pad_top + pad_bottom, pad_left))
    right = np.zeros((i_r + pad_top + pad_bottom, pad_right))
    img = np.hstack((left, img))
    img = np.hstack((img, right))

    out = np.zeros((i_r, i_c))
    for i in range(i_r):
        for j in range(i_c):
            if i + k_r < img.shape[0] and j + k_c  < img.shape[1]:
                tmp = img[i:i+k_r, j:j+k_c]
                out[i,j] = np.sum(tmp.T*kernel)
    return out

below is the test.


kernel = np.array([[1,0,1],[0,0,0],[1,0,0]])
test_img = np.zeros((9, 9))
test_img[3:6, 3:6] = 1

out = conv2D(test_img, kernel)
print(out)
print(sig.convolve2d(test_img, kernel, mode="same"))

Here are the results, you can see that they are centrosymmetric.

my:
[[0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 1. 1. 1. 0. 0.]
 [0. 0. 0. 0. 1. 1. 1. 0. 0.]
 [0. 0. 1. 1. 3. 2. 2. 0. 0.]
 [0. 0. 1. 1. 2. 1. 1. 0. 0.]
 [0. 0. 1. 1. 2. 1. 1. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0.]]

scipy:
[[0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 1. 1. 2. 1. 1. 0. 0.]
 [0. 0. 1. 1. 2. 1. 1. 0. 0.]
 [0. 0. 2. 2. 3. 1. 1. 0. 0.]
 [0. 0. 1. 1. 1. 0. 0. 0. 0.]
 [0. 0. 1. 1. 1. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0.]]


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source