2D convolution is the core operation in convolutional neural networks and classical image processing. A small kernel slides over an image, computing a weighted sum at each position to produce a filtered output.
Given a single-channel 2D image, a kernel, a stride, and a padding amount, compute the convolution output.
The output dimensions are:
Hout=⌊sH+2p−kh⌋+1Wout=⌊sW+2p−kw⌋+1Return the output as a 2D list of floats.
Input:
image = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] kernel = [[1, 0], [0, 1]] stride = 1, padding = 0
Output:
[[6, 8], [12, 14]]
The 2x2 kernel acts as a diagonal sum filter. At position (0,0): 11 + 20 + 40 + 51 = 6.
Input:
image = [[1, 2], [3, 4]] kernel = [[1, 1], [1, 1]] stride = 1, padding = 1
Output:
[[1, 3, 2], [4, 10, 6], [3, 7, 4]]
The image is padded with zeros, making it 4x4. The kernel sums a 2x2 neighborhood. Padding preserves spatial dimensions.
Build the padded image first as a separate 2D array, then iterate over valid kernel positions.
The output position (i, j) reads from padded image starting at row istride, column jstride.
Sign in to take notes on this problem
Accepts: array
Accepts: array
Accepts: number
Accepts: number
2D convolution is the core operation in convolutional neural networks and classical image processing. A small kernel slides over an image, computing a weighted sum at each position to produce a filtered output.
Given a single-channel 2D image, a kernel, a stride, and a padding amount, compute the convolution output.
The output dimensions are:
Hout=⌊sH+2p−kh⌋+1Wout=⌊sW+2p−kw⌋+1Return the output as a 2D list of floats.
Input:
image = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] kernel = [[1, 0], [0, 1]] stride = 1, padding = 0
Output:
[[6, 8], [12, 14]]
The 2x2 kernel acts as a diagonal sum filter. At position (0,0): 11 + 20 + 40 + 51 = 6.
Input:
image = [[1, 2], [3, 4]] kernel = [[1, 1], [1, 1]] stride = 1, padding = 1
Output:
[[1, 3, 2], [4, 10, 6], [3, 7, 4]]
The image is padded with zeros, making it 4x4. The kernel sums a 2x2 neighborhood. Padding preserves spatial dimensions.
Build the padded image first as a separate 2D array, then iterate over valid kernel positions.
The output position (i, j) reads from padded image starting at row istride, column jstride.
Sign in to take notes on this problem
Accepts: array
Accepts: array
Accepts: number
Accepts: number