-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
529 lines (416 loc) · 17.2 KB
/
main.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
import cv2
import os
import math
import json
import logging
import argparse
import scipy.signal
import scipy.ndimage
import numpy as np
from PIL import Image
from typing import List
import phasepack.phasecong as pc
from skimage.metrics import structural_similarity
try:
import rasterio
except ImportError:
rasterio = None
logger = logging.getLogger(__name__)
def _assert_image_shapes_equal(org_img: np.ndarray, pred_img: np.ndarray, metric: str):
# shape of the image should be like this (rows, cols, bands)
# Please note that: The interpretation of a 3-dimension array read from rasterio is: (bands, rows, columns) while
# image processing software like scikit-image, pillow and matplotlib are generally ordered: (rows, columns, bands)
# in order efficiently swap the axis order one can use reshape_as_raster, reshape_as_image from rasterio.plot
msg = (
f"Cannot calculate {metric}. Input shapes not identical. y_true shape ="
f"{str(org_img.shape)}, y_pred shape = {str(pred_img.shape)}"
)
assert org_img.shape == pred_img.shape, msg
def rmse(org_img: np.ndarray, pred_img: np.ndarray, max_p: int = 4095) -> float:
"""
Root Mean Squared Error
Calculated individually for all bands, then averaged
"""
_assert_image_shapes_equal(org_img, pred_img, "RMSE")
rmse_bands = []
for i in range(org_img.shape[2]):
dif = np.subtract(org_img[:, :, i], pred_img[:, :, i])
m = np.mean(np.square(dif / max_p))
s = np.sqrt(m)
rmse_bands.append(s)
return np.mean(rmse_bands)
def psnr(org_img: np.ndarray, pred_img: np.ndarray, max_p: int = 4095) -> float:
"""
Peek Signal to Noise Ratio, implemented as mean squared error converted to dB.
It can be calculated as
PSNR = 20 * log10(MAXp) - 10 * log10(MSE)
When using 12-bit imagery MaxP is 4095, for 8-bit imagery 255. For floating point imagery using values between
0 and 1 (e.g. unscaled reflectance) the first logarithmic term can be dropped as it becomes 0
"""
_assert_image_shapes_equal(org_img, pred_img, "PSNR")
mse_bands = []
for i in range(org_img.shape[2]):
mse_bands.append(np.mean(np.square(org_img[:, :, i] - pred_img[:, :, i])))
result = 20 * np.log10(max_p) - 10.0 * np.log10(np.mean(mse_bands))
if math.isinf(result):
return 0.0
else:
return result
def _similarity_measure(x: np.array, y: np.array, constant: float):
"""
Calculate feature similarity measurement between two images
"""
numerator = 2 * x * y + constant
denominator = x ** 2 + y ** 2 + constant
return numerator / denominator
def _gradient_magnitude(img: np.ndarray, img_depth: int):
"""
Calculate gradient magnitude based on Scharr operator.
"""
scharrx = cv2.Scharr(img, img_depth, 1, 0)
scharry = cv2.Scharr(img, img_depth, 0, 1)
return np.sqrt(scharrx ** 2 + scharry ** 2)
def fsim(
org_img: np.ndarray, pred_img: np.ndarray, T1: float = 0.85, T2: float = 160
) -> float:
"""
Feature-based similarity index, based on phase congruency (PC) and image gradient magnitude (GM)
There are different ways to implement PC, the authors of the original FSIM paper use the method
defined by Kovesi (1999). The Python phasepack project fortunately provides an implementation
of the approach.
There are also alternatives to implement GM, the FSIM authors suggest to use the Scharr
operation which is implemented in OpenCV.
Note that FSIM is defined in the original papers for grayscale as well as for RGB images. Our use cases
are mostly multi-band images e.g. RGB + NIR. To accommodate for this fact, we compute FSIM for each individual
band and then take the average.
Note also that T1 and T2 are constants depending on the dynamic range of PC/GM values. In theory this parameters
would benefit from fine-tuning based on the used data, we use the values found in the original paper as defaults.
Args:
org_img -- numpy array containing the original image
pred_img -- predicted image
T1 -- constant based on the dynamic range of PC values
T2 -- constant based on the dynamic range of GM values
"""
_assert_image_shapes_equal(org_img, pred_img, "FSIM")
alpha = (
beta
) = 1 # parameters used to adjust the relative importance of PC and GM features
fsim_list = []
for i in range(org_img.shape[2]):
# Calculate the PC for original and predicted images
pc1_2dim = pc(
org_img[:, :, i], nscale=4, minWaveLength=6, mult=2, sigmaOnf=0.5978
)
pc2_2dim = pc(
pred_img[:, :, i], nscale=4, minWaveLength=6, mult=2, sigmaOnf=0.5978
)
# pc1_2dim and pc2_2dim are tuples with the length 7, we only need the 4th element which is the PC.
# The PC itself is a list with the size of 6 (number of orientation). Therefore, we need to
# calculate the sum of all these 6 arrays.
pc1_2dim_sum = np.zeros((org_img.shape[0], org_img.shape[1]), dtype=np.float64)
pc2_2dim_sum = np.zeros(
(pred_img.shape[0], pred_img.shape[1]), dtype=np.float64
)
for orientation in range(6):
pc1_2dim_sum += pc1_2dim[4][orientation]
pc2_2dim_sum += pc2_2dim[4][orientation]
# Calculate GM for original and predicted images based on Scharr operator
gm1 = _gradient_magnitude(org_img[:, :, i], cv2.CV_16U)
gm2 = _gradient_magnitude(pred_img[:, :, i], cv2.CV_16U)
# Calculate similarity measure for PC1 and PC2
S_pc = _similarity_measure(pc1_2dim_sum, pc2_2dim_sum, T1)
# Calculate similarity measure for GM1 and GM2
S_g = _similarity_measure(gm1, gm2, T2)
S_l = (S_pc ** alpha) * (S_g ** beta)
numerator = np.sum(S_l * np.maximum(pc1_2dim_sum, pc2_2dim_sum))
denominator = np.sum(np.maximum(pc1_2dim_sum, pc2_2dim_sum))
fsim_list.append(numerator / denominator)
return np.mean(fsim_list)
def _ehs(x: np.ndarray, y: np.ndarray):
"""
Entropy-Histogram Similarity measure
"""
H = (np.histogram2d(x.flatten(), y.flatten()))[0]
return -np.sum(np.nan_to_num(H * np.log2(H)))
def _edge_c(x: np.ndarray, y: np.ndarray):
"""
Edge correlation coefficient based on Canny detector
"""
# Use 100 and 200 as thresholds, no indication in the paper what was used
g = cv2.Canny((x * 0.0625).astype(np.uint8), 100, 200)
h = cv2.Canny((y * 0.0625).astype(np.uint8), 100, 200)
g0 = np.mean(g)
h0 = np.mean(h)
numerator = np.sum((g - g0) * (h - h0))
denominator = np.sqrt(np.sum(np.square(g - g0)) * np.sum(np.square(h - h0)))
return numerator / denominator
def issm(org_img: np.ndarray, pred_img: np.ndarray) -> float:
"""
Information theoretic-based Statistic Similarity Measure
Note that the term e which is added to both the numerator as well as the denominator is not properly
introduced in the paper. We assume the authers refer to the Euler number.
"""
_assert_image_shapes_equal(org_img, pred_img, "ISSM")
# Variable names closely follow original paper for better readability
x = org_img
y = pred_img
A = 0.3
B = 0.5
C = 0.7
ehs_val = _ehs(x, y)
canny_val = _edge_c(x, y)
numerator = canny_val * ehs_val * (A + B) + math.e
denominator = A * canny_val * ehs_val + B * ehs_val + C * ssim(x, y) + math.e
return np.nan_to_num(numerator / denominator)
def vifp(ref, dist) -> float:
"""
VIF (sometimes called VIF-P or VIFP), Visual Information Fidelity
"""
_assert_image_shapes_equal(ref, dist, "vifp")
sigma_nsq = 2
eps = 1e-10
num = 0.0
den = 0.0
for scale in range(1, 5):
N = 2 ** (4 - scale + 1) + 1
sd = N / 5.0
if (scale > 1):
ref = scipy.ndimage.gaussian_filter(ref, sd)
dist = scipy.ndimage.gaussian_filter(dist, sd)
ref = ref[::2, ::2]
dist = dist[::2, ::2]
mu1 = scipy.ndimage.gaussian_filter(ref, sd)
mu2 = scipy.ndimage.gaussian_filter(dist, sd)
mu1_sq = mu1 * mu1
mu2_sq = mu2 * mu2
mu1_mu2 = mu1 * mu2
sigma1_sq = scipy.ndimage.gaussian_filter(ref * ref, sd) - mu1_sq
sigma2_sq = scipy.ndimage.gaussian_filter(dist * dist, sd) - mu2_sq
sigma12 = scipy.ndimage.gaussian_filter(ref * dist, sd) - mu1_mu2
sigma1_sq[sigma1_sq < 0] = 0
sigma2_sq[sigma2_sq < 0] = 0
g = sigma12 / (sigma1_sq + eps)
sv_sq = sigma2_sq - g * sigma12
g[sigma1_sq < eps] = 0
sv_sq[sigma1_sq < eps] = sigma2_sq[sigma1_sq < eps]
sigma1_sq[sigma1_sq < eps] = 0
g[sigma2_sq < eps] = 0
sv_sq[sigma2_sq < eps] = 0
sv_sq[g < 0] = sigma2_sq[g < 0]
g[g < 0] = 0
sv_sq[sv_sq <= eps] = eps
num += np.sum(np.log10(1 + g * g * sigma1_sq / (sv_sq + sigma_nsq)))
den += np.sum(np.log10(1 + sigma1_sq / sigma_nsq))
vifp = num / den
if np.isnan(vifp):
return 1.0
else:
return vifp
def ssim(org_img: np.ndarray, pred_img: np.ndarray, max_p: int = 4095) -> float:
"""
Structural Simularity Index
"""
_assert_image_shapes_equal(org_img, pred_img, "SSIM")
return structural_similarity(org_img, pred_img, data_range=max_p, channel_axis = -1)
def sliding_window(image: np.ndarray, stepSize: int, windowSize: int):
# slide a window across the image
for y in range(0, image.shape[0], stepSize):
for x in range(0, image.shape[1], stepSize):
# yield the current window
yield (x, y, image[y : y + windowSize[1], x : x + windowSize[0]])
def uiq(
org_img: np.ndarray, pred_img: np.ndarray, step_size: int = 1, window_size: int = 8
):
"""
Universal Image Quality index
"""
# TODO: Apply optimization, right now it is very slow
_assert_image_shapes_equal(org_img, pred_img, "UIQ")
org_img = org_img.astype(np.float32)
pred_img = pred_img.astype(np.float32)
q_all = []
for (x, y, window_org), (x, y, window_pred) in zip(
sliding_window(
org_img, stepSize=step_size, windowSize=(window_size, window_size)
),
sliding_window(
pred_img, stepSize=step_size, windowSize=(window_size, window_size)
),
):
# if the window does not meet our desired window size, ignore it
if window_org.shape[0] != window_size or window_org.shape[1] != window_size:
continue
for i in range(org_img.shape[2]):
org_band = window_org[:, :, i]
pred_band = window_pred[:, :, i]
org_band_mean = np.mean(org_band)
pred_band_mean = np.mean(pred_band)
org_band_variance = np.var(org_band)
pred_band_variance = np.var(pred_band)
org_pred_band_variance = np.mean(
(org_band - org_band_mean) * (pred_band - pred_band_mean)
)
numerator = 4 * org_pred_band_variance * org_band_mean * pred_band_mean
denominator = (org_band_variance + pred_band_variance) * (
org_band_mean ** 2 + pred_band_mean ** 2
)
if denominator != 0.0:
q = numerator / denominator
q_all.append(q)
if not np.any(q_all):
raise ValueError(
f"Window size ({window_size}) is too big for image with shape "
f"{org_img.shape[0:2]}, please use a smaller window size."
)
return np.mean(q_all)
def sam(org_img: np.ndarray, pred_img: np.ndarray, convert_to_degree: bool = True):
"""
Spectral Angle Mapper which defines the spectral similarity between two spectra
"""
_assert_image_shapes_equal(org_img, pred_img, "SAM")
# Spectral angles are first computed for each pair of pixels
numerator = np.sum(np.multiply(pred_img, org_img), axis=2)
denominator = np.linalg.norm(org_img, axis=2) * np.linalg.norm(pred_img, axis=2)
val = np.clip(numerator / denominator, -1, 1)
sam_angles = np.arccos(val)
if convert_to_degree:
sam_angles = sam_angles * 180.0 / np.pi
# The original paper states that SAM values are expressed as radians, while e.g. Lanares
# et al. (2018) use degrees. We therefore made this configurable, with degree the default
return np.mean(np.nan_to_num(sam_angles))
def sre(org_img: np.ndarray, pred_img: np.ndarray):
"""
Signal to Reconstruction Error Ratio
"""
_assert_image_shapes_equal(org_img, pred_img, "SRE")
org_img = org_img.astype(np.float32)
sre_final = []
for i in range(org_img.shape[2]):
numerator = np.square(np.mean(org_img[:, :, i]))
denominator = (np.linalg.norm(org_img[:, :, i] - pred_img[:, :, i])) / (
org_img.shape[0] * org_img.shape[1]
)
sre_final.append(numerator / denominator)
result = 10 * np.log10(np.mean(sre_final))
if math.isinf(result):
return 0.0
else:
return result
def diff_export(org_img_path: str, pred_img_path: str):
org_img = cv2.imread(org_img_path)
pred_img = cv2.imread(pred_img_path)
# Convert images to grayscale
before_gray = cv2.cvtColor(org_img, cv2.COLOR_BGR2GRAY)
after_gray = cv2.cvtColor(pred_img, cv2.COLOR_BGR2GRAY)
# Compute SSIM between two images
(score, diff) = structural_similarity(before_gray, after_gray, full=True)
diff = (diff * 255).astype("uint8")
# Threshold the difference image, followed by finding contours to
# obtain the regions of the two input images that differ
thresh = cv2.threshold(diff, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
contours = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours = contours[0] if len(contours) == 2 else contours[1]
mask = np.zeros(org_img.shape, dtype='uint8')
filled_after = pred_img.copy()
#
for c in contours:
area = cv2.contourArea(c)
if area > 40:
x, y, w, h = cv2.boundingRect(c)
cv2.rectangle(org_img, (x, y), (x + w, y + h), (36, 255, 12), 2)
cv2.rectangle(pred_img, (x, y), (x + w, y + h), (36, 255, 12), 2)
cv2.drawContours(mask, [c], 0, (0, 255, 0), -1)
cv2.drawContours(filled_after, [c], 0, (0, 255, 0), -1)
save_path = "Result"
# ordinal number
ordinal_num = "1"
before_Image = Image.fromarray(org_img)
before_Image.save(os.path.join(save_path, ordinal_num +"_before.png"))
after_Image = Image.fromarray(pred_img)
after_Image.save(os.path.join(save_path, ordinal_num + "_after.png"))
diff_Image = Image.fromarray(diff)
diff_Image.save(os.path.join(save_path, ordinal_num + "_diff.png"))
mask_Image = Image.fromarray(mask)
mask_Image.save(os.path.join(save_path,ordinal_num + "_mask.png"))
filled_afterImage = Image.fromarray(filled_after)
filled_afterImage.save(os.path.join(save_path,ordinal_num + "_filled_after.png"))
# cv2.imshow('before', org_img)
# cv2.imshow('after', pred_img)
# cv2.imshow('diff',diff)
# cv2.imshow('mask',mask)
# cv2.imshow('filled after',filled_after)
# cv2.waitKey(0)
metric_functions = {
"fsim": fsim,
"issm": issm,
"vifp": vifp,
"psnr": psnr,
"rmse": rmse,
"sam": sam,
"sre": sre,
"ssim": ssim,
"uiq": uiq,
}
def read_image(path: str):
logger.info(f"Reading image {os.path.basename(path)}")
if rasterio and (path.endswith(".tif") or path.endswith(".tiff")):
return np.rollaxis(rasterio.open(path).read(), 0, 3)
return cv2.imread(path)
def evaluation(org_img_path: str, pred_img_path: str, metrics: List[str]):
output_dict = {}
org_img = read_image(org_img_path)
pred_img = read_image(pred_img_path)
diff_export(org_img_path, pred_img_path)
for metric in metrics:
metric_func = metric_functions[metric]
out_value = float(metric_func(org_img, pred_img))
logger.info(f"{metric.upper()} value is: {out_value}")
output_dict[metric] = out_value
return output_dict
def main():
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
level=logging.INFO,
)
all_metrics = sorted(metric_functions.keys())
parser = argparse.ArgumentParser(
description="Evaluates an Image Super Resolution Model"
)
parser.add_argument(
"--org_img_path",
help="Path to original input image",
required=True,
metavar="FILE",
)
parser.add_argument(
"--pred_img_path",
help="Path to predicted image",
required=True,
metavar="FILE"
)
parser.add_argument(
"--metric",
dest="metrics",
action="append",
choices=all_metrics + ["all"],
metavar="METRIC",
help="select an evaluation metric (%(choices)s) (can be repeated)",
)
args = parser.parse_args()
if not args.metrics:
args.metrics = ["psnr"]
if "all" in args.metrics:
args.metrics = all_metrics
metric_values = evaluation(
org_img_path=args.org_img_path,
pred_img_path=args.pred_img_path,
metrics=args.metrics,
)
result_dict = {
"image1": args.org_img_path,
"image2": args.pred_img_path,
"metrics": metric_values,
}
print(json.dumps(result_dict, sort_keys=True))
if __name__ == "__main__":
main()