Python opencv convolution
# please use python 3.x
import numpy as np
# def convolve(image, kernel, bias, stride, padding):
"""
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)
"""
# return result
import matplotlib.pyplot as plt
import cv2
import os
from scipy import ndimage as nd
def processImage(image):
# image = cv2.imread(image)
# plt.imshow(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
# image = processImage('images.jpg')
# print(image.shape)
# plt.imshow(image)
# plt.show()
# kernel = np.array([[-1, -1, -1], [-1, 8, -1], [-1, -1, -1]])
# image_conv = convolve(image, kernel, 1, 1)
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()
# Grayscale Image
# image = processImage('images.jpg')
# Edge Detection Kernel
# Convolve and Save Output
# output = convolve(image, kernel, 0, 1, 15)
# plt.imshow(output)
# plt.show()
# cv2.imwrite('2DConvolved.jpg', output)