2D Image Convolution
Implement a 2D image convolution operation in valid mode, which involves sliding a kernel over an image to generate a feature map. This process is fundamental in Computer Vision as it enables the extraction of relevant features from images by applying a set of learnable filters.
The concept of convolution is based on the idea of scanning an image with a smaller matrix, known as a kernel, to compute feature values at each position. The kernel is slid over the entire image, and at each position, the element-wise product between the kernel and the overlapping image region is computed, followed by summation of these products. The resulting feature map has a size of , where and are the dimensions of the image, and and are the dimensions of the kernel.
Here are the steps to perform the convolution:
- Initialize an empty output matrix with dimensions .
- Slide the kernel over the image, scanning each valid position.
- At each position, compute the element-wise product between the kernel and the overlapping image region.
- Sum up the products to obtain the feature value at that position.
This technique is widely used in image processing and analysis applications.
Example
image = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] kernel = [[1, 1, 1], [1, 1, 1], [1, 1, 1]]
[[45.0]]
imageis a grid of pixel values, andkernelis a grid of weights, all .- Output size is : a kernel as large as the image fits in exactly one place, so only is valid.
- There each pixel is multiplied by the weight on it and the products are summed: .
- Rounded to 4 decimal places, the sum appears as
45.0, in one row of one value:[[45.0]]. - Notice that an all-ones kernel just totals the pixels under it. The kernel is not flipped: weights directly. A kernel with in the top-left, in the bottom-left and elsewhere gives at , and at all four positions.
Constraints
- image and kernel are 2D lists of numbers
- kernel fits within the image
- Return 2D list of convolution results rounded to 4 decimal places