# -*- coding: utf-8 -*-
"""
sobel 边缘提取 一阶导数
拉普拉斯边缘提取 二阶导数
@author: LNP
"""
import cv2 as cv
import numpy as np
#拉普拉斯默认4卷积核
def laplace_demo(image):
lpls=cv.Laplacian(image,cv.CV_32F)
dst=cv.convertScaleAbs(lpls)
cv.imshow('laplace_demo',dst)
#自定义拉普拉斯卷积核 增强边缘
def custom_laplace_demo(image):
kernel=np.array([[1,1,1],[1,-8,1],[1,1,1]])
lpls=cv.filter2D(image,cv.CV_32F,kernel=kernel)
dst=cv.convertScaleAbs(lpls)
cv.imshow('custom_laplace_demo',dst)
def sobel_demo(image):
grad_x=cv.Sobel(image,cv.CV_32F,1,0)
grad_y=cv.Sobel(image,cv.CV_32F,0,1)
#grad_x=cv.Scharr(image,cv.CV_32F,1,0) #增强边缘
#grad_y=cv.Scharr(image,cv.CV_32F,0,1)
gradx=cv.convertScaleAbs(grad_x)
grady=cv.convertScaleAbs(grad_y)
gradxy=cv.addWeighted(gradx,0.5,grady,0.5,0)
cv.imshow('sobel_x',gradx)
cv.imshow('sobel_y',grady)
cv.imshow('sobel_demo',gradxy)
src=cv.imread("D:/Study/opencv/code/321.jpg")
cv.imshow('src',src)
sobel_demo(src)
laplace_demo(src)
custom_laplace_demo(src)
cv.waitKey(0)
cv.destroyAllWindows()