Emboss Filter Implementation and Application

A filter Emboss Introduction:

        Emboss filter used in edge detection and contour images, can effectively enhance the high frequency information (edges and contours) of the image, preserving the low frequency information (image content) of the image.


Emboss filter convolution ↑
 

Two experiments: Emboss filter implemented using filters enhance contours 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()

 


Three results:


Emboss filter after image effect ↑
 

After the picture is converted to grayscale ↑
 

        Can be seen, Emboss filter can effectively enhance the contour of the image.


Four reference:

  https://www.jianshu.com/p/0f7102dec590

Guess you like

Origin www.cnblogs.com/wojianxin/p/12508170.html