Skip to content
Snippets Groups Projects
Commit e332373d authored by josephna's avatar josephna
Browse files

Upload New File- Convolutes the kernel for blurring out images

parent 5fb4b84e
No related branches found
No related tags found
No related merge requests found
import numpy as np
import cv2
import matplotlib.pyplot as plt
def convolution(image, kernel, average=False, verbose=False):
if len(image.shape) == 3:
# print("Found 3 Channels : {}".format(image.shape))
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# print("Converted to Gray Channel. Size : {}".format(image.shape))
# else:
# print("Image Shape : {}".format(image.shape))
# print("Kernel Shape : {}".format(kernel.shape))
if verbose:
plt.imshow(image, cmap='gray')
plt.title("Image")
plt.show()
image_row, image_col = image.shape
kernel_row, kernel_col = kernel.shape
output = np.zeros(image.shape)
pad_height = int((kernel_row - 1) / 2)
pad_width = int((kernel_col - 1) / 2)
padded_image = np.zeros((image_row + (2 * pad_height), image_col + (2 * pad_width)))
padded_image[pad_height:padded_image.shape[0] - pad_height, pad_width:padded_image.shape[1] - pad_width] = image
if verbose:
plt.imshow(padded_image, cmap='gray')
plt.title("Padded Image")
plt.show()
for row in range(image_row):
for col in range(image_col):
output[row, col] = np.sum(kernel * padded_image[row:row + kernel_row, col:col + kernel_col])
if average:
output[row, col] /= kernel.shape[0] * kernel.shape[1]
# print("Output Image size : {}".format(output.shape))
# gray = output
# backtorgb = cv2.cvtColor(gray,cv2.COLOR_GRAY2RGB)
if verbose:
plt.imshow(output, cmap='gray')
plt.title("Output Image using {}X{} Kernel".format(kernel_row, kernel_col))
plt.show()
return output.astype(np.float32)
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment