-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathspatial_filters.py
272 lines (218 loc) · 7.25 KB
/
spatial_filters.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import numpy as np
# 2-Dimensional Convolution in spatial domain
###############################################################################
# INPUT
# f: Input image
# w: Input filter
###############################################################################
# OUTPUT
# result: Output image
###############################################################################
def spatial_convolution2d(f, w):
# Get filter size
m, n = w.shape
# Pad impulse image
f = np.pad(f, (m-1, n-1), 'constant', constant_values=0)
# Get padded f size
x, y = f.shape
edgex = m/2
edgey = n/2
# Initialize result image
result = np.zeros_like(f)
# Rotate filter 180 degrees
rot_w = np.flipud(np.fliplr(w))
# Loop through padded f
for i in range(edgex, x - edgex):
for j in range(edgey, y - edgey):
# Loop through filter on each pixel in padded f and convolute
for s in range(m):
for t in range(n):
result[i, j] += rot_w[s,t] * f[i + s - edgex, j + t - edgey]
# Crop result image back to original size
result = result[m-1:-(m-1), n-1:-(n-1)]
return result
# Mean Filter
###############################################################################
# INPUT
# img: Input image
# s: Shape of filter (default is 3x3)
# ftype: Filter type ('arithmetic' or 'geometric')
###############################################################################
# OUTPUT
# result: Output image
###############################################################################
def mean_filter(img, s=3, ftype='geometric'):
x, y = img.shape
# Initialize result image
result = np.zeros_like(img)
filter_edge = s/2
# Traverse through image
for i in range(0,x):
for j in range(0,y):
if ftype == 'arithmetic':
sum_values = 0
elif ftype=='geometric':
product_values = 1
count = 0
# Traverse through filter
for u in range(s):
for v in range(s):
# Get current position
cur_x = (i + u - filter_edge)
cur_y = (j + v - filter_edge)
# Stay inside image boundaries
if((cur_x >= 0) and (cur_y >= 0) and (cur_x < x) and (cur_y < y)):
if ftype == 'arithmetic':
# Get sum of values
sum_values += img[cur_x, cur_y]
elif ftype=='geometric':
# Get product of values
product_values *= img[cur_x, cur_y]
count+=1
if ftype == 'arithmetic':
# Get arithmetic mean value
mean = sum_values/count
elif ftype=='geometric':
# Get geometric mean value
mean = product_values**(1.0/count)
# print mean
# Round off to closest integer
result[i, j] = mean
return result
# Median Filter
###############################################################################
# INPUT
# img: Input image
# s: Shape of filter (default is 3x3)
###############################################################################
# OUTPUT
# result: Output image
###############################################################################
def median_filter(img, s=3):
x, y = img.shape
# Initialize result image
result = np.zeros_like(img)
# Traverse through image
for i in range(0, x):
for j in range(0, y):
# Create new filter list
filtr = []
filter_edge = s/2
# Traverse through filter
for u in range(s):
for v in range(s):
# Get current position
cur_x = (i + u - filter_edge)
cur_y = (j + v - filter_edge)
# Stay inside image boundaries
if((cur_x >= 0) and (cur_y >= 0) and (cur_x < x) and (cur_y < y)):
# Append value to filter list
filtr.append(img[cur_x, cur_y])
# Convert filter list to numpy array
filtr = np.asarray(filtr)
# Output median value in filter region
result[i, j] = np.median(filtr)
return result
# Adaptive Median Filter
###############################################################################
# INPUT
# img: Input image
# s: Start shape of filter (default is 3x3)
# s_max: Maximum shape of filter (default is 7x7)
###############################################################################
# OUTPUT
# result: Output image
###############################################################################
def adaptive_median_filter(img, s=3, s_max=7):
x, y = img.shape
# Initialize result image
result = np.zeros_like(img)
# Traverse through image
for i in range(0, x):
for j in range(0, y):
# Set current filter size to starting filter size
s_cur = s
# While current filter size is smaller or equal to maximum filter size
while s_cur <= s_max:
# Create new filter list
filtr = []
filter_edge = s_cur/2
# Traverse through filter
for u in range(s_cur):
for v in range(s_cur):
# Get current position
cur_x = (i + u - filter_edge)
cur_y = (j + v - filter_edge)
# Stay inside image boundaries
if((cur_x >= 0) and (cur_y >= 0) and (cur_x < x) and (cur_y < y)):
# Append value to filter list
filtr.append(img[cur_x, cur_y])
# Get value in center of filter region
if cur_x == i and cur_y == j:
z_xy = filtr[-1]
# Convert filter list to numpy array
filtr = np.asarray(filtr)
# Get minimum value in filter region
z_min = np.amin(filtr)
# Get maximum value in filter region
z_max = np.amax(filtr)
# Get median value in filter region
z_med = np.median(filtr)
# If z_med is not an impulse: check next case. else: increase window size
if z_min < z_med < z_max:
# If z_xy is not an impulse: output z_xy. else: output z_med
if z_min < z_xy < z_max:
result[i, j] = z_xy
else:
result[i, j] = z_med
# Break to exit while loop
break
else:
s_cur += 2
else:
result[i, j] = z_med # Output median value if maximum window size has been surpassed
return result
# Adaptive Local Noise Reduction Filter
###############################################################################
# INPUT
# img: Input image
# var_g: Estimate of overall noise variance in image
# s: Shape of filter (default is 3x3)
###############################################################################
# OUTPUT
# result: Output image
###############################################################################
def adaptive_lnr_filter(img, var_g, s=3):
x, y = img.shape
# Initialize result image
result = np.zeros_like(img)
filter_edge = s/2
# Traverse through image
for i in range(0,x):
for j in range(0,y):
# Create new filter list
filtr = []
# Traverse through filter
for u in range(s):
for v in range(s):
# Get current position
cur_x = (i + u - filter_edge)
cur_y = (j + v - filter_edge)
# Stay inside image boundaries
if((cur_x >= 0) and (cur_y >= 0) and (cur_x < x) and (cur_y < y)):
# Append value to filter list
filtr.append(img[cur_x, cur_y])
# Convert filter list to numpy array
filtr = np.array(filtr)
# Get local mean from filter
mean_l = np.mean(filtr)
# Get local variance from filter
var_l = np.var(filtr)
# If local variance is smaller than global variance, set ratio to 1
if var_g <= var_l:
r = var_g / var_l
else:
r = 1
# Get the output value and round off to nearest integer
result[i, j] = img[i, j] - (r * (img[i, j] - mean_l))
return result