This repository has been archived by the owner on Sep 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
image-quality-assessment.py
322 lines (247 loc) · 11.5 KB
/
image-quality-assessment.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
# import specific necessary packages
from skimage.metrics import structural_similarity as ssim
from matplotlib import pyplot as plt
import cv2
import numpy as np
import math
import os
# define function for Peak signal-to-noise ratio (PSNR)
def psnr(target, ref):
# assume RGB Image
target_data = target.astype(float)
ref_data = ref.astype(float)
diff = ref_data - target_data
diff = diff.flatten('C')
rmse = math.sqrt(np.mean(diff ** 2.))
return 20 * math.log10(255. / rmse)
# define function for mean square error (MSE)
def mse(target, ref):
# MSE is the sum of the squared difference between the two images
err = np.sum((target.astype('float') - ref.astype('float')) ** 2)
err /= float(target.shape[0] * target.shape[1])
return err
# define function that combined all three image quality metrics
def compare_images(target, ref):
scores = []
scores.append(psnr(target, ref))
scores.append(mse(target, ref))
scores.append(ssim(target, ref, multichannel = True))
return scores
def iqa_enhancement():
try:
# for image enhancement only
TARGET = 'output/1-enhanced.png'
REFERENCE = 'input/1.png'
# open target and reference images
target = cv2.imread(TARGET)
ref = cv2.imread(REFERENCE)
# calculate score
scores = compare_images(target, ref)
# print image quality assessment
assessment = "> Image quality assessment (IQA) for enhancement only" + '\n' + \
"Target : " + TARGET + '\n' + \
"Reference : " + REFERENCE + '\n' + \
"PSNR (Peak signal-to-noise ratio) : " + str(scores[0]) + '\n' + \
"MSE (Mean squared error) : " + str(scores[1]) + '\n' + \
"SSIM (Structural similarity) : " + str(scores[2]) + '\n'
print(assessment)
# display images as subplots
fig, axs = plt.subplots(1, 2, figsize=(16, 8))
axs[0].imshow(cv2.cvtColor(ref, cv2.COLOR_BGR2RGB))
axs[0].set_title('Original')
axs[1].imshow(cv2.cvtColor(target, cv2.COLOR_BGR2RGB))
axs[1].set_title('Enhanced')
axs[1].set(xlabel = "PSNR (Peak signal-to-noise ratio) : " + str(scores[0]) + '\n' + \
"MSE (Mean squared error) : " + str(scores[1]) + '\n' + \
"SSIM (Structural similarity) : " + str(scores[2]) + '\n')
# remove the x and y ticks
# for ax in axs:
# ax.set_xticks([])
# ax.set_yticks([])
# save in image result
print('Saving result for IQA for enhancement only in image...' + '\n')
fig.savefig('output/IQA-enhancement.png')
plt.close()
print('Saving Done.' + '\n')
except Exception as e:
print("Skipping test.")
print("Error! " + str(e) + '\n')
def iqa_enlargement():
try:
# for image enlargement and enhancement
TARGET = 'output/1-bicubic-enhanced.png'
REFERENCE = 'output/1-bicubic.png'
# open target and reference images
target = cv2.imread(TARGET)
ref = cv2.imread(REFERENCE)
# calculate score
scores = compare_images(target, ref)
# print image quality assessment
assessment = "> Image quality assessment (IQA) for enlargement and enhancement" + '\n' + \
"Target : " + TARGET + '\n' + \
"Reference : " + REFERENCE + '\n' + \
"PSNR (Peak signal-to-noise ratio) : " + str(scores[0]) + '\n' + \
"MSE (Mean squared error) : " + str(scores[1]) + '\n' + \
"SSIM (Structural similarity) : " + str(scores[2]) + '\n'
print(assessment)
# display images as subplots
fig, axs = plt.subplots(1, 2, figsize=(16, 8))
axs[0].imshow(cv2.cvtColor(ref, cv2.COLOR_BGR2RGB))
axs[0].set_title('Bicubic')
axs[1].imshow(cv2.cvtColor(target, cv2.COLOR_BGR2RGB))
axs[1].set_title('Bicubic Enhanced')
axs[1].set(xlabel = "PSNR (Peak signal-to-noise ratio) : " + str(scores[0]) + '\n' + \
"MSE (Mean squared error) : " + str(scores[1]) + '\n' + \
"SSIM (Structural similarity) : " + str(scores[2]) + '\n')
# remove the x and y ticks
# for ax in axs:
# ax.set_xticks([])
# ax.set_yticks([])
# save in image result
print('Saving result for IQA for enlargement and enhancement in image...' + '\n')
fig.savefig('output/IQA-enlargement-enhancement.png')
plt.close()
print('Saving Done.' + '\n')
except Exception as e:
print("Skipping test.")
print("Error! " + str(e) + '\n')
def iqa_original_compare():
try:
# for degraded vs fsrcnn
REFERENCE = 'input/1-ref.png'
DEGRADED = 'input/1.png'
FSRCNN = 'output/1-enhanced.png'
# open target and reference images
fsrcnn = cv2.imread(FSRCNN)
degraded = cv2.imread(DEGRADED)
ref = cv2.imread(REFERENCE)
# calculate score
scores = []
scores.append(compare_images(degraded, ref))
scores.append(compare_images(fsrcnn, ref))
# print image quality assessment
assessment_degraded = "> Image quality assessment (IQA) for degraded and FSRCNN" + '\n' + \
"For Original (Degraded) image vs Reference ---" + '\n' + \
"Target : " + DEGRADED + '\n' + \
"Reference : " + REFERENCE + '\n' + \
"PSNR (Peak signal-to-noise ratio) : " + str(scores[0][0]) + '\n' + \
"MSE (Mean squared error) : " + str(scores[0][1]) + '\n' + \
"SSIM (Structural similarity) : " + str(scores[0][2]) + '\n'
assessment_fsrcnn = '\n' + \
"For FSRCNN vs Reference ---" + '\n' + \
"Target : " + FSRCNN + '\n' + \
"Reference : " + REFERENCE + '\n' + \
"PSNR (Peak signal-to-noise ratio) : " + str(scores[1][0]) + '\n' + \
"MSE (Mean squared error) : " + str(scores[1][1]) + '\n' + \
"SSIM (Structural similarity) : " + str(scores[1][2]) + '\n'
print(assessment_degraded)
print(assessment_fsrcnn)
# display images as subplots
fig, axs = plt.subplots(1, 3, figsize=(24, 8))
axs[0].imshow(cv2.cvtColor(ref, cv2.COLOR_BGR2RGB))
axs[0].set_title('Reference')
#axs[0].set_title('Reference - Bicubic 2x')
axs[1].imshow(cv2.cvtColor(degraded, cv2.COLOR_BGR2RGB))
axs[1].set_title('Original (Degraded)')
#axs[1].set_title('1 Pass')
axs[1].set(xlabel = "PSNR (Peak signal-to-noise ratio) : " + str(scores[0][0]) + '\n' + \
"MSE (Mean squared error) : " + str(scores[0][1]) + '\n' + \
"SSIM (Structural similarity) : " + str(scores[0][2]) + '\n')
axs[2].imshow(cv2.cvtColor(fsrcnn, cv2.COLOR_BGR2RGB))
axs[2].set_title('FSRCNN')
#axs[2].set_title('2 Passes')
axs[2].set(xlabel = "PSNR (Peak signal-to-noise ratio) : " + str(scores[1][0]) + '\n' + \
"MSE (Mean squared error) : " + str(scores[1][1]) + '\n' + \
"SSIM (Structural similarity) : " + str(scores[1][2]) + '\n')
# remove the x and y ticks
# for ax in axs:
# ax.set_xticks([])
# ax.set_yticks([])
# save in image result
print('Saving result for IQA for Original (Degraded) vs FSRCNN in image...' + '\n')
fig.savefig('output/IQA-original-compare.png')
plt.close()
print('Saving Done.' + '\n')
except Exception as e:
print("No reference file!, Skipping reference test.")
print("Error! " + str(e) + '\n')
def iqa_waifu2x_compare():
try:
# for waifu2x vs fsrcnn
REFERENCE = 'input/1-ref.png'
DEGRADED = 'input/1.png'
FSRCNN = 'output/1-enhanced.png'
WAIFU2X = 'input/1-waifu2x.png'
# open target and reference images
fsrcnn = cv2.imread(FSRCNN)
degraded = cv2.imread(DEGRADED)
ref = cv2.imread(REFERENCE)
waifu2x = cv2.imread(WAIFU2X)
# calculate score
scores = []
scores.append(compare_images(degraded, ref))
scores.append(compare_images(fsrcnn, ref))
scores.append(compare_images(waifu2x, ref))
# print image quality assessment
assessment_degraded = "> Image quality assessment (IQA) for degraded, FSRCNN and waifu2x" + '\n' + \
"For Original (Degraded) image vs Reference ---" + '\n' + \
"Target : " + DEGRADED + '\n' + \
"Reference : " + REFERENCE + '\n' + \
"PSNR (Peak signal-to-noise ratio) : " + str(scores[0][0]) + '\n' + \
"MSE (Mean squared error) : " + str(scores[0][1]) + '\n' + \
"SSIM (Structural similarity) : " + str(scores[0][2]) + '\n'
assessment_fsrcnn = '\n' + \
"For FSRCNN vs Reference ---" + '\n' + \
"Target : " + FSRCNN + '\n' + \
"Reference : " + REFERENCE + '\n' + \
"PSNR (Peak signal-to-noise ratio) : " + str(scores[1][0]) + '\n' + \
"MSE (Mean squared error) : " + str(scores[1][1]) + '\n' + \
"SSIM (Structural similarity) : " + str(scores[1][2]) + '\n'
assessment_waifu2x = '\n' + \
"For waifu2x vs Reference ---" + '\n' + \
"Target : " + WAIFU2X + '\n' + \
"Reference : " + REFERENCE + '\n' + \
"PSNR (Peak signal-to-noise ratio) : " + str(scores[2][0]) + '\n' + \
"MSE (Mean squared error) : " + str(scores[2][1]) + '\n' + \
"SSIM (Structural similarity) : " + str(scores[2][2]) + '\n'
print(assessment_degraded)
print(assessment_fsrcnn)
print(assessment_waifu2x)
# display images as subplots
fig, axs = plt.subplots(1, 4, figsize=(32, 8))
axs[0].imshow(cv2.cvtColor(ref, cv2.COLOR_BGR2RGB))
axs[0].set_title('Reference')
axs[1].imshow(cv2.cvtColor(degraded, cv2.COLOR_BGR2RGB))
axs[1].set_title('Original (Degraded)')
axs[1].set(xlabel = "PSNR (Peak signal-to-noise ratio) : " + str(scores[0][0]) + '\n' + \
"MSE (Mean squared error) : " + str(scores[0][1]) + '\n' + \
"SSIM (Structural similarity) : " + str(scores[0][2]) + '\n')
axs[2].imshow(cv2.cvtColor(fsrcnn, cv2.COLOR_BGR2RGB))
axs[2].set_title('FSRCNN')
axs[2].set(xlabel = "PSNR (Peak signal-to-noise ratio) : " + str(scores[1][0]) + '\n' + \
"MSE (Mean squared error) : " + str(scores[1][1]) + '\n' + \
"SSIM (Structural similarity) : " + str(scores[1][2]) + '\n')
axs[3].imshow(cv2.cvtColor(waifu2x, cv2.COLOR_BGR2RGB))
axs[3].set_title('waifu2x')
axs[3].set(xlabel = "PSNR (Peak signal-to-noise ratio) : " + str(scores[2][0]) + '\n' + \
"MSE (Mean squared error) : " + str(scores[2][1]) + '\n' + \
"SSIM (Structural similarity) : " + str(scores[2][2]) + '\n')
# remove the x and y ticks
# for ax in axs:
# ax.set_xticks([])
# ax.set_yticks([])
# save in image result
print('Saving result for IQA for Original (Degraded) vs FSRCNN vs waifu2x in image...' + '\n')
fig.savefig('output/IQA-waifu2x-compare.png')
plt.close()
print('Saving Done.' + '\n')
except Exception as e:
print("No waifu2x reference file!, Skipping waifu2x reference test.")
print("Error! " + str(e) + '\n')
if __name__ == "__main__":
iqa_enhancement()
iqa_enlargement()
# for having reference
iqa_original_compare()
# for having reference and waifu2x
iqa_waifu2x_compare()