一. Emboss 滤波器介绍:
Emboss滤波器常用于检测图像的边缘和轮廓,能够有效地增强图像的高频信息(边缘和轮廓),并保留图像的低频信息(图像内容)。
二. 实验:实现Emboss滤波器,使用Emboss滤波器增强图像轮廓
1 import cv2 2 3 import numpy as np 4 5 # Gray scale 6 7 def BGR2GRAY(img): 8 9 b = img[:, :, 0].copy() 10 11 g = img[:, :, 1].copy() 12 13 r = img[:, :, 2].copy() 14 15 # Gray scale 16 17 out = 0.2126 * r + 0.7152 * g + 0.0722 * b 18 19 out = out.astype(np.uint8) 20 21 return out 22 23 # emboss filter 24 25 def emboss_filter(img, K_size=3): 26 27 H, W = img.shape 28 29 # zero padding 30 31 pad = K_size // 2 32 33 out = np.zeros((H + pad * 2, W + pad * 2), dtype=np.float) 34 35 out[pad: pad + H, pad: pad + W] = img.copy().astype(np.float) 36 37 tmp = out.copy() 38 39 # emboss kernel 40 41 K = [[-2., -1., 0.],[-1., 1., 1.], [0., 1., 2.]] 42 43 # filtering 44 45 for y in range(H): 46 47 for x in range(W): 48 49 out[pad + y, pad + x] = np.sum(K * (tmp[y: y + K_size, x: x + K_size])) 50 51 out = np.clip(out, 0, 255) 52 53 out = out[pad: pad + H, pad: pad + W].astype(np.uint8) 54 55 return out 56 57 # Read image 58 59 img = cv2.imread("../paojie.jpg").astype(np.float) 60 61 # BGR2GRAY 62 63 gray = BGR2GRAY(img) 64 65 # emboss filtering 66 67 out = emboss_filter(gray, K_size=3) 68 69 # Save result 70 71 cv2.imwrite("out.jpg", out) 72 73 cv2.imshow("result", out) 74 75 cv2.waitKey(0) 76 77 cv2.destroyAllWindows()
三. 实验结果:
可以看到,Emboss滤波器能够有效地增强图像的轮廓。
四. 参考内容: