-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfilters.py
366 lines (283 loc) · 10.9 KB
/
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
import imageio
import numpy as np
from scipy.signal import medfilt2d
from fuzzysets import small, medium, large
from utils import rmse, saveimg
import os
alpha = 100
def _filterA(img, i, j, k, mu, betaij):
l = len(img)
m = np.arange(k) - k//2
n = np.arange(k) - k//2
d2 = []
_mu = []
w = []
I = []
for p in m:
for q in n:
if 0 <= i+p < l and 0 <= j+q < l:
d2.append((img[i+p][j+q] - img[i][j])**2)
_mu.append(mu[i][j][p][q])
I.append(img[i+p][j+q])
minusd2bybetaij = d2 / (-1 * betaij)
weights = _mu * (1+minusd2bybetaij)
num, den = 0, 0
for i in range(len(weights)):
num += weights[i] * I[i]
den += weights[i]
return num / den
def filterA(img, k, mu, beta):
l = len(img)
result = np.zeros((l, l))
for i in range(len(img)):
for j in range(len(img)):
result[i][j] = _filterA(img, i, j, k, mu, beta[i][j])
return result
def filterB(img, k, beta):
""" Apply filter B on img given pre-computed mu and beta """
l = len(img)
m = np.arange(k) - k//2
n = np.arange(k) - k//2
result = np.zeros((l, l))
beta = 1 / beta
# Apply filter
for i in range(l):
for j in range(l):
num = 0
den = 0
for p in m:
for q in n:
if p == 0 and q == 0:
continue
if 0 <= i+p < l and 0 <= j+q < l:
num += beta[p+i][q+j] * img[p+i][q+j]
den += beta[p+i][q+j]
result[i][j] = num / den
return result
def filterC(img, k, mu, beta):
""" Apply filter C on img given pre-computed mu and beta """
l = len(img)
m = np.arange(k) - k//2
n = np.arange(k) - k//2
result = np.zeros((l, l))
for i in range(l):
for j in range(l):
num = 0
den = 0
for p in m:
for q in n:
if 0 <= i+p < l and 0 <= j+q < l:
_mu = mu[i+p][j+q][-p][-q]
weight = _mu / beta[i+p][j+q]
num += weight * img[i+p][j+q]
den += weight
result[i][j] = num / den
return result
def compute_mu_beta_w(img, k):
""" Computes mu beta and w. This is a useful pre-computation """
l = len(img)
mu = np.zeros((l, l, k, k))
w = np.zeros((l, l, k, k))
beta = np.zeros((l, l))
m = np.arange(k) - k//2
n = np.arange(k) - k//2
for i in range(l):
for j in range(l):
d2 = np.zeros((k, k))
num = 0
for p in m:
for q in n:
if 0 <= i+p < l and 0 <= j+q < l:
num += 1
d2[p][q] = (img[i+p][j+q] - img[i][j])**2
w[i][j][p][q] = np.exp(-1*(p**2 + q**2)/alpha)
beta[i][j] = np.sum(d2) / (num - 1)
mu[i][j] = w[i][j] * np.exp(d2 / (-1 * beta[i][j]))
return mu, beta, w
def TC(l, k, mu, w):
""" Computes total compatibility """
m = np.arange(k) - k//2
n = np.arange(k) - k//2
tc = np.zeros((l, l))
for i in range(l):
for j in range(l):
num, den = 0, 0
for p in m:
for q in n:
if p == 0 and q == 0:
continue
if 0 <= i+p < l and 0 <= j+q < l:
num += mu[i+p][j+q][-p][-q]
den += w[i+p][j+q][-p][-q]
tc[i][j] = num / den
return tc
def filterR1(img, k, tc, beta, img_A=None, img_B=None, img_C=None):
""" Apply filter R1 on img given pre-computed tc and beta """
l = len(img)
result = np.zeros((l, l))
if img_B is None:
img_B = filterB(img, k, beta)
for i in range(l):
for j in range(l):
c1 = small(tc[i][j])
c2 = 1 - c1
result[i][j] = (c1 * img_B[i][j] + c2 * img[i][j]) / (c1 + c2)
return result
def filterR2(img, k, tc, mu, beta, img_A=None, img_B=None, img_C=None):
""" Apply filter R2 on img given pre-computed tc, mu and beta """
l = len(img)
result = np.zeros((l, l))
if img_B is None:
img_B = filterB(img, k, beta)
if img_C is None:
img_C = filterC(img, k, mu, beta)
for i in range(l):
for j in range(l):
c1 = small(tc[i][j])
c2 = 1 - c1
result[i][j] = (c1 * img_B[i][j] + c2 * img_C[i][j]) / (c1 + c2)
return result
def filterR3(img, k, tc, mu, beta, img_A=None, img_B=None, img_C=None):
""" Apply filter R3 on img given pre-computed tc, mu and beta """
l = len(img)
result = np.zeros((l, l))
if img_A is None:
img_A = filterA(img, k, mu, beta)
if img_B is None:
img_B = filterB(img, k, beta)
if img_C is None:
img_C = filterC(img, k, mu, beta)
for i in range(l):
for j in range(l):
c1 = small(tc[i][j])
c2 = medium(tc[i][j])
c3 = large(tc[i][j])
result[i][j] = (c1 * img_A[i][j] + c2 * img_B[i]
[j] + c3 * img_C[i][j]) / (c1 + c2 + c3)
return result
def filterR3Crisp(img, k, tc, mu, beta, img_A=None, img_B=None, img_C=None):
""" Apply filter R3-Crsip on img given pre-computed tc, mu and beta """
l = len(img)
result = np.zeros((l, l))
if img_A is None:
img_A = filterA(img, k, mu, beta)
if img_B is None:
img_B = filterB(img, k, beta)
if img_C is None:
img_C = filterC(img, k, mu, beta)
for i in range(l):
for j in range(l):
c1 = small(tc[i][j])
c2 = medium(tc[i][j])
c3 = large(tc[i][j])
if c1 == max(c1, c2, c3):
result[i][j] = img_A[i][j]
elif c2 == max(c1, c2, c3):
result[i][j] = img_B[i][j]
else:
result[i][j] = img_C[i][j]
return result
def filterR4(img, k, tc, mu, beta, img_A=None, img_B=None, img_C=None):
""" Apply filter R4 on img given pre-computed tc, mu and beta """
l = len(img)
result = np.zeros((l, l))
if img_A is None:
img_A = filterA(img, k, mu, beta)
if img_B is None:
img_B = filterB(img, k, beta)
if img_C is None:
img_C = filterC(img, k, mu, beta)
for i in range(l):
for j in range(l):
c1 = small(tc[i][j])
c2 = medium(tc[i][j])
c3 = large(tc[i][j])
result[i][j] = (c1 * img_B[i][j] + c2 * img_A[i]
[j] + c3 * img_C[i][j]) / (c1 + c2 + c3)
return result
def filterR4Crisp(img, k, tc, mu, beta, img_A=None, img_B=None, img_C=None):
""" Apply filter R4-Crsip on img given pre-computed tc, mu and beta """
l = len(img)
result = np.zeros((l, l))
if img_A is None:
img_A = filterA(img, k, mu, beta)
if img_B is None:
img_B = filterB(img, k, beta)
if img_C is None:
img_C = filterC(img, k, mu, beta)
for i in range(l):
for j in range(l):
c1 = small(tc[i][j])
c2 = medium(tc[i][j])
c3 = large(tc[i][j])
if c1 == max(c1, c2, c3):
result[i][j] = img_B[i][j]
elif c2 == max(c1, c2, c3):
result[i][j] = img_A[i][j]
else:
result[i][j] = img_C[i][j]
return result
def allfilters(img, k, tc, mu, beta, imagename, original):
""" This function applies all the filters and saves the output images and logs the RMSE.
Filters applied: A, B, C, R1, R2, R3, R4, R3-Crisp, R4-Crisp and Median """
inp_img = img.copy()
img_A = filterA(img, k, mu, beta)
op_imagepath = os.path.join('images', 'enhanced', imagename+'_A.png')
img_A = saveimg(op_imagepath, img_A)
img_B = filterB(img, k, beta)
op_imagepath = os.path.join('images', 'enhanced', imagename+'_B.png')
img_B = saveimg(op_imagepath, img_B)
img_C = filterC(img, k, mu, beta)
op_imagepath = os.path.join('images', 'enhanced', imagename+'_C.png')
img_C = saveimg(op_imagepath, img_C)
img_Med = medfilt2d(img, k)
op_imagepath = os.path.join('images', 'enhanced', imagename+'_Med.png')
img_Med = saveimg(op_imagepath, img_Med)
img_R1 = filterR1(img, k, tc, beta, img_A=img_A, img_B=img_B, img_C=img_C)
op_imagepath = os.path.join('images', 'enhanced', imagename+'_R1.png')
img_R1 = saveimg(op_imagepath, img_R1)
img_R2 = filterR2(img, k, tc, mu, beta, img_A=img_A,
img_B=img_B, img_C=img_C)
op_imagepath = os.path.join('images', 'enhanced', imagename+'_R2.png')
img_R2 = saveimg(op_imagepath, img_R2)
img_R3 = filterR3(img, k, tc, mu, beta, img_A=img_A,
img_B=img_B, img_C=img_C)
op_imagepath = os.path.join('images', 'enhanced', imagename+'_R3.png')
img_R3 = saveimg(op_imagepath, img_R3)
img_R3Crisp = filterR3Crisp(
img, k, tc, mu, beta, img_A=img_A, img_B=img_B, img_C=img_C)
op_imagepath = os.path.join('images', 'enhanced', imagename+'_R3Crisp.png')
img_R3Crisp = saveimg(op_imagepath, img_R3Crisp)
img_R4 = filterR4(img, k, tc, mu, beta, img_A=img_A,
img_B=img_B, img_C=img_C)
op_imagepath = os.path.join('images', 'enhanced', imagename+'_R4.png')
img_R4 = saveimg(op_imagepath, img_R4)
img_R4Crisp = filterR4(img, k, tc, mu, beta,
img_A=img_A, img_B=img_B, img_C=img_C)
op_imagepath = os.path.join('images', 'enhanced', imagename+'_R4Crisp.png')
img_R4Crisp = saveimg(op_imagepath, img_R4Crisp)
if original is not None:
orig_imagepath = os.path.join('images', original)
orig_img = imageio.imread(orig_imagepath)
err = rmse(img_A, orig_img)
print('RMSE of filterA (against original image):{}'.format(err))
err = rmse(img_B, orig_img)
print('RMSE of filterB (against original image):{}'.format(err))
err = rmse(img_C, orig_img)
print('RMSE of filterC (against original image):{}'.format(err))
err = rmse(img_Med, orig_img)
print('RMSE of filterMed (against original image):{}'.format(err))
err = rmse(img_R1, orig_img)
print('RMSE of filterR1 (against original image):{}'.format(err))
err = rmse(img_R2, orig_img)
print('RMSE of filterR2 (against original image):{}'.format(err))
err = rmse(img_R3, orig_img)
print('RMSE of filterR3 (against original image):{}'.format(err))
err = rmse(img_R3Crisp, orig_img)
print('RMSE of filterR3Crisp (against original image):{}'.format(err))
err = rmse(img_R4, orig_img)
print('RMSE of filterR4 (against original image):{}'.format(err))
err = rmse(img_R4Crisp, orig_img)
print('RMSE of filterR3Crisp (against original image):{}'.format(err))
noisy_err = rmse(orig_img, inp_img)
print('RMSE of input image (against original image):{}'.format(noisy_err))