2D Convolution (Image Filtering)
2D Convolution (Image Filtering)
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.
Operation
- Pad the image with zeros on all sides by the padding amount.
- Slide the kernel over the padded image with the given stride.
- At each position, compute the element-wise product of the kernel and the image patch, then sum all values.
The output dimensions are:
Hout=⌊sH+2p−kh⌋+1Wout=⌊sW+2p−kw⌋+1Return the output as a 2D list of floats.
Examples
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.
Hint 1
Build the padded image first as a separate 2D array, then iterate over valid kernel positions.
Hint 2
The output position (i, j) reads from padded image starting at row istride, column jstride.
Requirements
- Apply zero-padding to the image before convolution
- Slide the kernel with the given stride
- Compute the correct output dimensions
- Support non-square images and kernels
Constraints
- 1 <= image height, width <= 100
- 1 <= kernel height, width <= image dimensions
- 1 <= stride <= 5
- 0 <= padding <= 5
- Time limit: 300 ms
Log in to take notes on this problem
Accepts: array
Accepts: array
Accepts: number
Accepts: number
2D Convolution (Image Filtering)
2D Convolution (Image Filtering)
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.
Operation
- Pad the image with zeros on all sides by the padding amount.
- Slide the kernel over the padded image with the given stride.
- At each position, compute the element-wise product of the kernel and the image patch, then sum all values.
The output dimensions are:
Hout=⌊sH+2p−kh⌋+1Wout=⌊sW+2p−kw⌋+1Return the output as a 2D list of floats.
Examples
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.
Hint 1
Build the padded image first as a separate 2D array, then iterate over valid kernel positions.
Hint 2
The output position (i, j) reads from padded image starting at row istride, column jstride.
Requirements
- Apply zero-padding to the image before convolution
- Slide the kernel with the given stride
- Compute the correct output dimensions
- Support non-square images and kernels
Constraints
- 1 <= image height, width <= 100
- 1 <= kernel height, width <= image dimensions
- 1 <= stride <= 5
- 0 <= padding <= 5
- Time limit: 300 ms
Log in to take notes on this problem
Accepts: array
Accepts: array
Accepts: number
Accepts: number