import numpy as np
"""
please implement convolution on an image
arguments:
image: input color image, numpy array of shape (in_height, in_width, in_channel)
kernel: weigths, numpy array of shape (kernel_size, kernel_size, in_channel, out_channel)
bias: biases, numpy array of shape (1,1,1,out_channel)
stride: stride, scalar
padding: the number of zero padding along image boundary, scalar
returns:
result: results of convolution, numpy array of shape (out_height, out_width, out_channel)
"""
import matplotlib.pyplot as plt
import cv2
import os
from scipy import ndimage as nd
def processImage(image):
image = cv2.cvtColor(src=image, code=cv2.COLOR_BGR2GRAY)
return image
def convolve(image, kernel, bias, strides, padding):
result = nd.convolve(image, kernel, mode='reflect', cval=1.0)
print(image.shape)
return result
if __name__ == '__main__':
kernel = np.array([[1, 1, 1],
[1, 1, 1],
[1, 1, 1]])
video = cv2.VideoCapture(r'video.avi')
try:
if not os.path.exists('pet'):
os.makedirs('pet')
except OSError:
print('Error')
currentframe = 0
while True:
ret, frame = video.read()
if ret:
cv2.imshow("original", frame)
image = processImage(frame)
output = convolve(image, kernel, 0, 1, 0)
cv2.imshow("convert", output)
if cv2.waitKey(27) & 0xFF == ord('q'):
break
else:
break
video.release()
cv2.destroyAllWindows()