Perform 2D convolution between an input image and a kernel:
C[i,j]=k=0∑Kh−1l=0∑Kw−1A[i+k,j+l]⋅B[k,l]
The convolution operation slides the 2D kernel over the input image, computing the sum of element-wise multiplications at each position. Zero padding is used at the boundaries.
Input:
- Matrix A of size H×W (input image)
- Matrix B of size Kh×Kw (convolution kernel)
- Both Kh and Kw are odd and smaller than H and W respectively
Output:
- Matrix C of size H×W (convolved image)
Notes:
- All matrices A, B, and C are stored in row-major order
- Use zero padding at the boundaries where the kernel extends beyond the input image
- The kernel is centered at each position, with (Kh−1)/2 rows and (Kw−1)/2 columns on each side