Question: python 2d convolutin by using numpy code is : def convolve2d(image, kernel): This function which takes an image and a kernel and returns the
python 2d convolutin by using numpy
code is :
def convolve2d(image, kernel): """ This function which takes an image and a kernel and returns the convolution of them. :param image: a numpy array of size [image_height, image_width]. :param kernel: a numpy array of size [kernel_height, kernel_width]. :return: a numpy array of size [image_height, image_width] (convolution output). """ # Flip the kernel kernel = np.flipud(np.fliplr(kernel)) # convolution output output = np.zeros_like(image) # Add zero padding to the input image image_padded = np.zeros((image.shape[0] + 2, image.shape[1] + 2)) image_padded[1:-1, 1:-1] = image # Loop over every pixel of the image for x in range(image.shape[1]): for y in range(image.shape[0]): # element-wise multiplication of the kernel and the image output[y, x]=(kernel * image_padded[y: y+3, x: x+3]).sum() return output # kernel to be used to get sharpened image KERNEL = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]]) image_sharpen = convolve2d(input_image, kernel=KERNEL) cv2.imwrite('sharpened_image.jpg', image_sharpen)
This code is used for 2d convolution of black and white photos. If I want to 2d convolution a color photo, how do I modify which part of the function im_filter of this code?
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
