一. 拉普拉斯滤波器简介:
我们知道:
![](https://upload-images.jianshu.io/upload_images/17221499-49df1c4141a3f5e2.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
拉普拉斯算子 ↑
![](https://upload-images.jianshu.io/upload_images/17221499-e3a07f2847ff8a02.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
x方向上二阶偏导数的数值近似计算 ↑
![](https://upload-images.jianshu.io/upload_images/17221499-40abc2dc2d2a8d0a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
y方向上二阶偏导数的数值近似计算 ↑
![](https://upload-images.jianshu.io/upload_images/17221499-3e786a6fb102af0e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
拉普拉斯算子在平面内的数值近似 ↑
![](https://upload-images.jianshu.io/upload_images/17221499-e20d129fc03194b2.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
拉普拉斯滤波器卷积核表示 ↑
二. 3*3的laplacian滤波器实现
# laplacian filter def laplacian_filter(img, K_size=3): H, W = img.shape # zero padding pad = K_size // 2 out = np.zeros((H + pad * 2, W + pad * 2), dtype=np.float) out[pad: pad + H, pad: pad + W] = img.copy().astype(np.float) tmp = out.copy() # laplacian kernle K = [[0., 1., 0.],[1., -4., 1.], [0., 1., 0.]] # filtering for y in range(H): for x in range(W): out[pad + y, pad + x] = np.sum(K * (tmp[y: y + K_size, x: x + K_size])) out = np.clip(out, 0, 255) out = out[pad: pad + H, pad: pad + W].astype(np.uint8) return out
三. 利用laplacian滤波器实现图像的锐化
由于拉普拉斯是一种微分算子,它的应用可增强图像中灰度突变的区域,减弱灰度的缓慢变化区域。
因此,锐化处理可选择拉普拉斯算子对原图像进行处理,产生描述灰度突变的图像,再将拉普拉斯图像与原始图像叠加而产生锐化图像:
![](https://upload-images.jianshu.io/upload_images/17221499-4d9aa12dca91d82e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
使用拉普拉斯滤波器实现的图像锐化算法 ↑
其中,f(x,y)为原始图像,g(x,y)为锐化后图像,c为-1(卷积核中间为负数时,若卷积核中间为正数,则c为1)。
四. 通过laplacian滤波器实现图像锐化 python源码
import cv2 import numpy as np # Image sharpening by laplacian filter def laplacian_sharpening(img, K_size=3): H, W = img.shape # zero padding pad = K_size // 2 out = np.zeros((H + pad * 2, W + pad * 2), dtype=np.float) out[pad: pad + H, pad: pad + W] = img.copy().astype(np.float) tmp = out.copy() # laplacian kernle K = [[0., 1., 0.],[1., -4., 1.], [0., 1., 0.]] # filtering and adding image -> Sharpening image for y in range(H): for x in range(W): # core code out[pad + y, pad + x] = (-1) * np.sum(K * (tmp[y: y + K_size, x: x + K_size])) + tmp[pad + y, pad + x] out = np.clip(out, 0, 255) out = out[pad: pad + H, pad: pad + W].astype(np.uint8) return out # Read Gray Scale image img = cv2.imread("../paojie_g.jpg",0).astype(np.float) # Image sharpening by laplacian filter out = laplacian_sharpening(img, K_size=3) # Save result cv2.imwrite("out.jpg", out) cv2.imshow("result", out) cv2.waitKey(0) cv2.destroyAllWindows()
五. 实验结果:
![](https://upload-images.jianshu.io/upload_images/17221499-d07f876233acc8f8.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
锐化后图像 ↑
![](https://upload-images.jianshu.io/upload_images/17221499-d637f9f3eed4ac8f.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
原图 ↑
六. 参考内容