-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
answer_80.py
108 lines (74 loc) · 2.29 KB
/
answer_80.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import cv2
import numpy as np
import matplotlib.pyplot as plt
# Grayscale
def BGR2GRAY(img):
# Grayscale
gray = 0.2126 * img[..., 2] + 0.7152 * img[..., 1] + 0.0722 * img[..., 0]
return gray
# Gabor
def Gabor_filter(K_size=111, Sigma=10, Gamma=1.2, Lambda=10, Psi=0, angle=0):
# get half size
d = K_size // 2
# prepare kernel
gabor = np.zeros((K_size, K_size), dtype=np.float32)
# each value
for y in range(K_size):
for x in range(K_size):
# distance from center
px = x - d
py = y - d
# degree -> radian
theta = angle / 180. * np.pi
# get kernel x
_x = np.cos(theta) * px + np.sin(theta) * py
# get kernel y
_y = -np.sin(theta) * px + np.cos(theta) * py
# fill kernel
gabor[y, x] = np.exp(-(_x**2 + Gamma**2 * _y**2) / (2 * Sigma**2)) * np.cos(2*np.pi*_x/Lambda + Psi)
# kernel normalization
gabor /= np.sum(np.abs(gabor))
return gabor
def Gabor_filtering(gray, K_size=111, Sigma=10, Gamma=1.2, Lambda=10, Psi=0, angle=0):
# get shape
H, W = gray.shape
# padding
gray = np.pad(gray, (K_size//2, K_size//2), 'edge')
# prepare out image
out = np.zeros((H, W), dtype=np.float32)
# get gabor filter
gabor = Gabor_filter(K_size=K_size, Sigma=Sigma, Gamma=Gamma, Lambda=Lambda, Psi=0, angle=angle)
# filtering
for y in range(H):
for x in range(W):
out[y, x] = np.sum(gray[y : y + K_size, x : x + K_size] * gabor)
out = np.clip(out, 0, 255)
out = out.astype(np.uint8)
return out
def Gabor_process(img):
# get shape
H, W, _ = img.shape
# gray scale
gray = BGR2GRAY(img).astype(np.float32)
# define angle
As = [0, 45, 90, 135]
# prepare pyplot
plt.subplots_adjust(left=0, right=1, top=1, bottom=0, hspace=0, wspace=0.2)
out = np.zeros([H, W], dtype=np.float32)
# each angle
for i, A in enumerate(As):
# gabor filtering
_out = Gabor_filtering(gray, K_size=11, Sigma=1.5, Gamma=1.2, Lambda=3, angle=A)
# add gabor filtered image
out += _out
# scale normalization
out = out / out.max() * 255
out = out.astype(np.uint8)
return out
# Read image
img = cv2.imread("imori.jpg").astype(np.float32)
# gabor process
out = Gabor_process(img)
cv2.imwrite("out.jpg", out)
cv2.imshow("result", out)
cv2.waitKey(0)