首先编写卷积代码
保证可以实现各种size滤波
def image_convolution(image,kernel):
[img_height,img_width] = image.shape
[kernel_height,kernel_width] = kernel.shape
expand_width = int((kernel_width - 1)/2)
expand_height = int((kernel_height - 1)/2)
con_height = int(img_height + expand_height*2)
con_width = int(img_width + expand_width*2)
#给结果图像、用于卷积处理的矩阵创建空间
result_image = np.zeros(image.shape)
con_image = np.zeros((con_height, con_width))
#填入图片
con_image[expand_height:expand_height+img_height, expand_width:expand_width+img_width]=image[ : , :]
#对每个像素点进行处理
for i in range(expand_height,expand_height+img_height):
for j in range(expand_width,expand_width+img_width):
result_image[i-expand_height][j-expand_width] = int(np.sum(con_image[i-expand_height:i+expand_h