Image convolutions
The convolution of an image with a kernel summarizes a part of the image as the sum of the multiplication of that part of the image with the kernel. In this exercise, you will write the code that executes a convolution of an image with a kernel using Numpy. Given a black and white image that is stored in the variable im
, write the operations inside the loop that would execute the convolution with the provided kernel.
This exercise is part of the course
Image Modeling with Keras
Exercise instructions
- Select the right window from the image in each iteration and multiply this part of the image with the kernel.
- Sum the result and allocate the sum to the correct entry in the output array (
results
).
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
kernel = np.array([[0, 1, 0], [1, 1, 1], [0, 1, 0]])
result = np.zeros(im.shape)
# Output array
for ii in range(im.shape[0] - 3):
for jj in range(im.shape[1] - 3):
result[ii, jj] = (____[ii:ii+3, jj:____+____] * ____).____
# Print result
print(result)