图像锐化处理

时间:2021-07-09 18:21:55

Laplace算子

import cv2
import numpy as np
import matplotlib.pyplot as plt

img = cv2.imread('img/jy3.jpg') 
img1 = np.float32(img) #转化数值类型
kernel = np.array([[0, -1,  0],
                    [-1,  5, -1],
                    [0, -1,  0]]) #计算出来会是负数
#np.ones((5,5),np.float32)/25

dst = cv2.filter2D(img1,-1,kernel)
#cv2.filter2D(src,dst,kernel,auchor=(-1,-1))函数:
#输出图像与输入图像大小相同
#中间的数为-1,输出数值格式的相同plt.figure()
plt.subplot(1,2,1),plt.imshow(img1,'gray')#默认彩色,另一种彩色bgr
plt.subplot(1,2,2),plt.imshow(dst,'gray')
cv2.imshow("dst",dst)
cv2.imwrite("ttttt.jpg",dst)
cv2.waitKey(0)


Sobel算子

import cv2  
import numpy as np   

img=cv2.imread('img\gsyellow_3.jpg')  


x=cv2.Sobel(img,cv2.CV_16S,1,0)#对x求导 
y=cv2.Sobel(img,cv2.CV_16S,0,1)#对y求导 

absX=cv2.convertScaleAbs(x)   # 转回uint8 
absY=cv2.convertScaleAbs(y)  

dst = cv2.addWeighted(absX,0.5,absY,0.5,0)   

cv2.imshow('absX',absX)  
cv2.imshow('absY',absY)  
cv2.imshow('Result',dst)

cv2.waitKey(0)  
cv2.destroyAllWindows()

#cv2.imwrite('ttttt_sobel1.jpg',absX)
#cv2.imwrite('ttttt_sobel2.jpg',absY)
#cv2.imwrite('ttttt_sobel3.jpg',dst)


Canny算子

import cv2  
import numpy as np   

img=cv2.imread('img/gsyellow_3.jpg',0)
img1=cv2.imread('123.jpg')  

#为了让结果更清晰,这里的ksize设为3, 
gray_lap=cv2.Laplacian(img,cv2.CV_16S,ksize=3)#拉式算子
gray_lap1=cv2.Laplacian(img1,cv2.CV_16S,ksize=3)

dst=cv2.convertScaleAbs(gray_lap)
dst1=cv2.convertScaleAbs(gray_lap1)

cv2.imshow('laplacian',dst)
cv2.imshow('laplacian1',dst1)

cv2.imwrite('ttttt_canny1.jpg',dst)
cv2.imwrite('ttttt_canny2.jpg',dst1)


cv2.waitKey(0)  
cv2.destroyAllWindows()