1 __author__ = "WSX" 2 import cv2 as cv 3 import numpy as np 4 5 def lapalian_demo(image): #拉普拉斯算子 6 #dst = cv.Laplacian(image, cv.CV_32F) #内置函数来实现 7 #lpls = cv.convertScaleAbs(dst) 8 kernel = np.array([[1, 1, 1], [1, -8, 1], [1, 1, 1]]) #自定义来实现 9 dst = cv.filter2D(image, cv.CV_32F, kernel=kernel) 10 lpls = cv.convertScaleAbs(dst) 11 cv.imshow("lapalian_demo", lpls) 12 13 14 def sobel_demo(image): #sobel算子 15 grad_x = cv.Scharr(image, cv.CV_32F, 1, 0) #x的一阶导数 16 grad_y = cv.Scharr(image, cv.CV_32F, 0, 1) 17 gradx = cv.convertScaleAbs(grad_x) # 先绝对值 再转到8位图像上 18 grady = cv.convertScaleAbs(grad_y) 19 cv.imshow("gradient-x", gradx) #左右有差异的表现 20 cv.imshow("gradient-y", grady) #上下有差异的表现 21 gradxy = cv.addWeighted(gradx, 0.5, grady, 0.5, 0) #一起表现 22 cv.imshow("gradient", gradxy) 23 24 def main(): 25 img = cv.imread("1.JPG") 26 cv.namedWindow("Show", cv.WINDOW_AUTOSIZE) 27 cv.imshow("Show", img) 28 sobel_demo(img) 29 30 cv.waitKey(0) 31 cv.destroyAllWindows() 32 33 main()