一. LoG滤波器
LoG 即 高斯-拉普拉斯(Laplacian of Gaussian)的缩写。为了防止拉普拉斯滤波器计算二次微分时使图像的噪声更加明显,所以先使用高斯滤波器使图像更加平滑,再使用拉普拉斯滤波器使图像的轮廓更加清晰。
二. 实验:LoG滤波器的实现及用于图像边缘检测
实验中,我们先将BGR图像转换为灰度图像,再给灰度图像添加高斯噪声,再使用LoG滤波器对图像进行处理,最后输出处理结果
1 import cv2 2 import numpy as np 3 4 # Gray scale 5 def BGR2GRAY(img): 6 b = img[:, :, 0].copy() 7 g = img[:, :, 1].copy() 8 r = img[:, :, 2].copy() 9 10 # Gray scale 11 out = 0.2126 * r + 0.7152 * g + 0.0722 * b 12 out = out.astype(np.uint8) 13 14 return out 15 16 # add gaussian noise 17 def gasuss_noise(image, mean=0, var=0.001): 18 ''' 19 添加高斯噪声 20 mean : 均值 21 var : 方差 22 ''' 23 image = np.array(image/255, dtype=float) 24 noise = np.random.normal(mean, var ** 0.5, image.shape) 25 out = image + noise 26 if out.min() < 0: 27 low_clip = -1. 28 else: 29 low_clip = 0. 30 out = np.clip(out, low_clip, 1.0) 31 out = np.uint8(out*255) 32 return out 33 34 # LoG filter 35 def LoG_filter(img, K_size=5, sigma=3): 36 H, W = img.shape 37 38 # zero padding 39 pad = K_size // 2 40 out = np.zeros((H + pad * 2, W + pad * 2), dtype=np.float) 41 out[pad: pad + H, pad: pad + W] = gray.copy().astype(np.float) 42 tmp = out.copy() 43 44 # LoG Kernel 45 K = np.zeros((K_size, K_size), dtype=np.float) 46 for x in range(-pad, -pad + K_size): 47 for y in range(-pad, -pad + K_size): 48 K[y + pad, x + pad] = (x ** 2 + y ** 2 - sigma ** 2) * np.exp( -(x ** 2 + y ** 2) / (2 * (sigma ** 2))) 49 K /= (2 * np.pi * (sigma ** 6)) 50 K /= K.sum() 51 52 # filtering 53 for y in range(H): 54 for x in range(W): 55 out[pad + y, pad + x] = np.sum(K * tmp[y: y + K_size, x: x + K_size]) 56 57 out = np.clip(out, 0, 255) 58 out = out[pad: pad + H, pad: pad + W].astype(np.uint8) 59 60 return out 61 62 63 # Read image 64 img = cv2.imread("../paojie.jpg") 65 66 # grayscale 67 gray = BGR2GRAY(img) 68 # add gaussian noise 69 gray = gasuss_noise(gray) 70 # LoG filtering 71 out = LoG_filter(gray, K_size=5, sigma=3) 72 73 # Save result 74 cv2.imwrite("out_g_g.jpg",gray) 75 cv2.imshow("result_g_g",gray) 76 cv2.imwrite("out.jpg", out) 77 cv2.imshow("result", out) 78 cv2.waitKey(0) 79 cv2.destroyAllWindows()
三. 实验结果:
从图像中可以观察到,LoG滤波器不仅有效地抑制了高斯噪声,还一定程度上增强了图像的边缘。
四. 参考内容:
https://www.jianshu.com/p/c70be0cccd46