-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexam5.py
124 lines (106 loc) · 4 KB
/
exam5.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import ImageTools as it
import numpy as np
def ideal_low_filter(image, D0, save_path, show: bool = False):
"""
频域平滑滤波器1: 理想低通滤波器
"""
image = np.array(image.convert("L"))
# 生成滤波器
h, w = image.shape[:2]
image_filter = np.ones((h, w))
u = np.fix(h / 2)
v = np.fix(w / 2)
for i in range(h):
for j in range(w):
d = np.sqrt((i - u) ** 2 + (j - v) ** 2)
image_filter[i, j] = 0 if d > D0 else 1
# 将滤波器应用到图像,生成理想低通滤波图像
filtered_image = it.fromarray(
it.restore255(filter_use(image, image_filter)))
it.save_image(filtered_image, save_path)
if show:
it.compare_image_show(image, filtered_image)
return filtered_image
def butterworth_low_filter(image, D0, rank, save_path, show: bool = False):
"""
频域平滑滤波器2: Butterworth低通滤波器
"""
image = np.array(image.convert("L"))
# 生成滤波器
h, w = image.shape[:2]
image_filter = np.zeros((h, w))
u = np.fix(h / 2)
v = np.fix(w / 2)
for i in range(h):
for j in range(w):
d = np.sqrt((i - u) ** 2 + (j - v) ** 2)
image_filter[i, j] = 1 / (1 + 0.414 * (d / D0) ** (2 * rank))
# 将滤波器应用到图像,生成Butterworth低通滤波图像
filtered_image = it.fromarray(
it.restore255(filter_use(image, image_filter)))
it.save_image(filtered_image, save_path)
if show:
it.compare_image_show(image, filtered_image)
return filtered_image
def ideal_high_filter(image, D0, save_path, show: bool = False):
"""
频域锐化滤波器1: 理想高通滤波器
"""
image = np.array(image.convert("L"))
# 生成滤波器
h, w = image.shape[:2]
image_filter = np.zeros((h, w))
u = np.fix(h / 2)
v = np.fix(w / 2)
for i in range(h):
for j in range(w):
d = np.sqrt((i - u) ** 2 + (j - v) ** 2)
image_filter[i, j] = 0 if d < D0 else 1
# 将滤波器应用到图像,生成Butterworth低通滤波图像
filtered_image = it.fromarray(
it.restore255(filter_use(image, image_filter)))
it.save_image(filtered_image, save_path)
if show:
it.compare_image_show(image, filtered_image)
return filtered_image
def butterworth_high_filter(image, D0, rank, save_path, show: bool = False):
"""
频域锐化滤波器2: Butterworth高通滤波器
"""
image = np.array(image.convert("L"))
# 生成滤波器
h, w = image.shape[:2]
image_filter = np.zeros((h, w))
u = np.fix(h / 2)
v = np.fix(w / 2)
for i in range(h):
for j in range(w):
d = np.sqrt((i - u) ** 2 + (j - v) ** 2)
image_filter[i, j] = 1 / (1 + (D0 / (d+1e-7)) ** (2 * rank))
# 将滤波器应用到图像,生成Butterworth低通滤波图像
filtered_image = it.fromarray(
it.restore255(filter_use(image, image_filter)))
it.save_image(filtered_image, save_path)
if show:
it.compare_image_show(image, filtered_image)
return filtered_image
def filter_use(img, filter):
"""
将图像与滤波器结合,生成对应的滤波图像数组
"""
# 首先进行傅里叶变换
f = np.fft.fft2(img)
f_center = np.fft.fftshift(f)
# 应用滤波器进行反变换
S = np.multiply(f_center, filter) # 频率相乘——l(u,v)*H(u,v)
f_origin = np.fft.ifftshift(S) # 将低频移动到原来的位置
f_origin = np.fft.ifft2(f_origin) # 使用ifft2进行傅里叶的逆变换
f_origin = np.abs(f_origin) # 设置区间
return f_origin
if __name__ == "__main__":
image = it.read_image('static/image_in/nana.jpg').convert("L")
save_path = 'static/image_out/' + it.gen_timestamp_name() + '.jpg'
# ideal_low_filter(image, 50, save_path, show=True)
# butterworth_low_filter(image, 50, 2, save_path, show=True)
# ideal_high_filter(image, 50, save_path, show=True)
butterworth_high_filter(image, 50, 2, save_path, show=True)