以上用了锐化,只需将以下代码的
cov=np.ones([3,3])/5000
# cov[1,1]=1
改成
cov=(-1)*np.ones([3,3])
cov[1,1]=9
cov=cov/800
以下为简单卷积代码,代码很简单,仔细看将会明白其中含义。我简单说一下,代码思路:
读取一幅图,以3通道颜色读取,便得到维度为(300,533,3)的三维矩阵,随后使用3*3的
卷积矩阵,依次滑动,便可得到新的(300,533,3)维的新矩阵,然后将其矩阵为图像输出,
便得到以上类似前后的图像。
import cv2 as cv
import numpy as np
v_a=np.zeros([1,533])
img=cv.imread('C:\\Users\\HHQ\\Desktop\\tangjun\\3.jpg' ,cv.IMREAD_COLOR)
print(img.shape)
print(type(img))
img=np.array(img)
cv.imshow('display image',img)
cv.waitKey(0)
img0=np.vstack((v_a,img[:,:,0],v_a))
img1=np.vstack((v_a,img[:,:,1],v_a))
img2=np.vstack((v_a,img[:,:,2],v_a))
h_a=np.zeros([302,1])
img0=np.hstack((h_a,img0,h_a))
img1=np.hstack((h_a,img1,h_a))
img2=np.hstack((h_a,img2,h_a))
cov=np.ones([3,3])/5000
# cov[1,1]=1
img_cov0=np.zeros([300,533])
img_cov1=np.zeros([300,533])
img_cov2=np.zeros([300,533])
for i in range(300):
for j in range(533):
img_cov0[i, j] = cov[0, 0] * img0[i + 0, j + 0] + cov[0, 1] * img0[i + 0, j + 1] + cov[0, 2] * img0[
i + 0, j + 2] + cov[1, 0] * img0[i + 1, j + 0] + cov[1, 1] * img0[i + 1, j + 1] + cov[1, 2] * img0[
i + 1, j + 2] + cov[2, 0] * img0[i + 2, j + 0] + cov[2, 1] * img0[i + 2, j + 1] + cov[
2, 2] * img0[i + 2, j + 2]
img_cov1[i, j] = cov[0, 0] * img1[i + 0, j + 0] + cov[0, 1] * img1[i + 0, j + 1] + cov[0, 2] * img1[
i + 0, j + 2] + cov[1, 0] * img1[i + 1, j + 0] + cov[1, 1] * img1[i + 1, j + 1] + cov[1, 2] * img1[
i + 1, j + 2] + cov[2, 0] * img1[i + 2, j + 0] + cov[2, 1] * img1[i + 2, j + 1] + cov[
2, 2] * img1[i + 2, j + 2]
img_cov2[i, j] = cov[0, 0] * img2[i + 0, j + 0] + cov[0, 1] * img2[i + 0, j + 1] + cov[0, 2] * img2[
i + 0, j + 2] + cov[1, 0] * img2[i + 1, j + 0] + cov[1, 1] * img2[i + 1, j + 1] + cov[1, 2] * img2[
i + 1, j + 2] + cov[2, 0] * img2[i + 2, j + 0] + cov[2, 1] * img2[i + 2, j + 1] + cov[
2, 2] * img2[i + 2, j + 2]
img_cov=np.zeros([300,533,3])
img_cov[:,:,0]=img_cov0
img_cov[:,:,1]=img_cov1
img_cov[:,:,2]=img_cov2
# print(img_cov)
print(img_cov.shape)
cv.imshow('display img_cov',img_cov)
cv.waitKey(0)