python+opencv图像处理(四十四)

Luplacian算子2
1、Luplacian算子
前篇已经介绍过拉普拉斯算子,网上对拉普拉斯算子的模板有以下两种:四领域和八领域。
四邻域模板如下:
0 |
1 |
0 |
1 |
-4 |
1 |
0 |
1 |
0 |
八邻域模板如下:
-1 |
-1 |
-1 |
-1 |
4 |
-1 |
-1 |
-1 |
-1 |
2、Luplacian算子实现
下图中,左图为原图,中间采用四领域模板,右图采用八领域模板。

其完整代码如下:
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
def Luplacian1(img):
result=img
lap=np.array([[0,1,0],[1,-4,1],[0,1,0]])
x=cv.filter2D(img,cv.CV_32F,lap)
res=cv.convertScaleAbs(x)
return res
def Luplacian2(img):
result=img
lap=np.array([[-1,-1,-1],[-1,4,-1],[-1,-1,-1]])
x=cv.filter2D(img,cv.CV_32F,lap)
res=cv.convertScaleAbs(x)
return res
img = cv.imread("E:\image\lena.jpg",0)
cv.imshow("origin",img)
dst=Luplacian1(img)
cv.imshow("Luplacian",dst)
dst2=Luplacian2(img)
cv.imshow("Luplacian2",dst2)
cv.waitKey(0)
赞 (0)