import cv2 as cv
import numpy as np
定义卷积核
kernel = np.ones((3,3),np.uint8) #3x3的卷积核,1
kernel_1 = np.array([[1,1,1],[1,2,1],[1,1,1]],dtype = np.uint8)/10 #8邻域自定义3*3的卷积核
kernel_2 = np.array([[1,1,1,1,1],[1,1,1,1,1],[1,1,1,1,1],
[1,1,1,1,1],[1,1,1,1,1]],dtype = np.uint8)/25 #高斯自定义5*5的卷积核
kernel_3 = np.array([[1,0,0,0,0],[1,0,0,0,0],[1,0,0,0,0],
[1,0,0,0,0],[1,0,0,0,0]],dtype = np.uint8) /5 # 一字结构
kernel_4 = np.array([[0,0,1,0,0],[0,0,1,0,0],[1,1,1,1,1],
[0,0,1,0,0],[0,0,1,0,0]],dtype = np.uint8)/9 # 十字结构
kernel_5 = np.array([[0,0,1,0,0],[0,1,1,1,0],[1,1,1,1,1],
[0,1,1,1,0],[0,0,1,0,0]],dtype = np.uint8)/15 # 菱形结构
读取照片
frame = cv.imread("/home/zhaoyang/001/cut/100000.jpg") #读取图像
卷积滤波
mask_kernel_1 = cv.filter2D(frame,-1,kernel_1) #3*3的卷积核
mask_kernel_2 = cv.filter2D(frame,-1,kernel_2) #5*5的卷积核
保存文件或读取文件
cv.imshow('100001.jpg',mask_kernel_1)
cv.imwrite('100001.jpg',mask_kernel_1)
cv.imwrite('100002.jpg',mask_kernel_2)
cv.imwrite('100003.jpg',mask_kernel_3)
网友评论