-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetrics.py
1263 lines (1004 loc) · 42.9 KB
/
metrics.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
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import cv2
from scipy import ndimage
from scipy.ndimage import sobel
import scipy.ndimage as ft
from skimage.transform.integral import integral_image as integral
from math import ceil, floor, log2
import numpy as np
from skimage import transform
import math
import torch
from torch import nn
import spectral_tools as ut
def sam(img1, img2):
"""SAM for 3D image, shape (H, W, C); uint or float[0, 1]"""
if not img1.shape == img2.shape:
raise ValueError('Input images must have the same dimensions.')
assert img1.ndim == 3 and img1.shape[2] > 1, "image n_channels should be greater than 1"
img1_ = img1.astype(np.float64)
img2_ = img2.astype(np.float64)
inner_product = (img1_ * img2_).sum(axis=2)
img1_spectral_norm = np.sqrt((img1_**2).sum(axis=2))
img2_spectral_norm = np.sqrt((img2_**2).sum(axis=2))
# numerical stability
cos_theta = (inner_product / (img1_spectral_norm * img2_spectral_norm + np.finfo(np.float64).eps)).clip(min=0, max=1)
return np.mean(np.arccos(cos_theta))
def sam2(ms,ps,degs = True):
result = np.double(ps)
target = np.double(ms)
if result.shape != target.shape:
raise ValueError('Result and target arrays must have the same shape!')
bands = target.shape[2]
rnorm = np.sqrt((result ** 2).sum(axis=2))
tnorm = np.sqrt((target ** 2).sum(axis=2))
dotprod = (result * target).sum(axis=2)
cosines = (dotprod / (rnorm * tnorm))
sam2d = np.arccos(cosines)
sam2d[np.invert(np.isfinite(sam2d))] = 0. # arccos(1.) -> NaN
if degs:
sam2d = np.rad2deg(sam2d)
return sam2d[np.isfinite(sam2d)].mean()
def ergas(img_fake, img_real, scale=4):
"""ERGAS for 2D (H, W) or 3D (H, W, C) image; uint or float [0, 1].
scale = spatial resolution of PAN / spatial resolution of MUL, default 4."""
if not img_fake.shape == img_real.shape:
raise ValueError('Input images must have the same dimensions.')
img_fake_ = img_fake.astype(np.float64)
img_real_ = img_real.astype(np.float64)
if img_fake_.ndim == 2:
mean_real = img_real_.mean()
mse = np.mean((img_fake_ - img_real_)**2)
return 100 / scale * np.sqrt(mse / (mean_real**2 + np.finfo(np.float64).eps))
elif img_fake_.ndim == 3:
means_real = img_real_.reshape(-1, img_real_.shape[2]).mean(axis=0)
mses = ((img_fake_ - img_real_)**2).reshape(-1, img_fake_.shape[2]).mean(axis=0)
return 100 / scale * np.sqrt((mses / (means_real**2 + np.finfo(np.float64).eps)).mean())
else:
raise ValueError('Wrong input image dimensions.')
# def sam2(ms,ps,degs = True):
# result = np.double(ps)
# target = np.double(ms)
# if result.shape != target.shape:
# raise ValueError('Result and target arrays must have the same shape!')
#
# bands = target.shape[2]
# rnorm = np.sqrt((result ** 2).sum(axis=2))
# tnorm = np.sqrt((target ** 2).sum(axis=2))
# dotprod = (result * target).sum(axis=2)
# cosines = (dotprod / (rnorm * tnorm))
# sam2d = np.arccos(cosines)
# sam2d[np.invert(np.isfinite(sam2d))] = 0. # arccos(1.) -> NaN
# if degs:
# sam2d = np.rad2deg(sam2d)
# return sam2d[np.isfinite(sam2d)].mean()
def RMSE(result, target):
result = np.double(result)
target = np.double(target)
if result.shape != target.shape:
raise ValueError('result and target arrays must have the same shape!')
return ((result - target) ** 2).mean() ** 0.5
def ERGAS(result, target, pixratio=0.5):
result = np.double(result)
target = np.double(target)
if result.shape != target.shape:
raise ValueError('result and target arrays must have the same shape!')
bands = target.shape[2]
addends = np.zeros(bands)
for band in range(bands):
addends[band] = ((RMSE(result[:, :, band], target[:, :, band])) / (target[:, :, band].mean())) ** 2
ergas = 100 * pixratio * ((1.0 / bands) * addends.sum()) ** 0.5
return ergas
def QAVE(result, target):
result = np.double(result)
target = np.double(target)
if result.shape != target.shape:
raise ValueError('result and target arrays must have the same shape!')
rmean = result.mean(axis=2)
tmean = target.mean(axis=2)
rmean1 = result[:, :, 0] - rmean
rmean2 = result[:, :, 1] - rmean
rmean3 = result[:, :, 2] - rmean
rmean4 = result[:, :, 3] - rmean
tmean1 = target[:, :, 0] - tmean
tmean2 = target[:, :, 1] - tmean
tmean3 = target[:, :, 2] - tmean
tmean4 = target[:, :, 3] - tmean
QR = (1 / result.shape[2] - 1) * (rmean1 ** 2 + rmean2 ** 2 + rmean3 ** 2 + rmean4 ** 2)
QT = (1 / result.shape[2] - 1) * (tmean1 ** 2 + tmean2 ** 2 + tmean3 ** 2 + tmean4 ** 2)
QRT = (1 / result.shape[2] - 1) * (rmean1 * tmean1 + rmean2 * tmean2 + rmean3 * tmean3 + rmean4 * tmean4)
QAVE = result.shape[2] * ((QRT * rmean) * tmean) / ((QR + QT) * ((rmean ** 2) + (tmean ** 2)))
m, n = QAVE.shape
Q = (1 / (m * n)) * np.sum(np.sum(QAVE))
return Q
def sCC(ms, ps):
ps_sobel = sobel(ps, mode='constant')
ms_sobel = sobel(ms, mode='constant')
return (np.sum(ps_sobel*ms_sobel)/np.sqrt(np.sum(ps_sobel*ps_sobel))/np.sqrt(np.sum(ms_sobel*ms_sobel)))
def scc(img1, img2):
"""SCC for 2D (H, W)or 3D (H, W, C) image; uint or float[0, 1]"""
if not img1.shape == img2.shape:
raise ValueError('Input images must have the same dimensions.')
img1_ = img1.astype(np.float64)
img2_ = img2.astype(np.float64)
if img1_.ndim == 2:
return np.corrcoef(img1_.reshape(1, -1), img2_.rehshape(1, -1))[0, 1]
elif img1_.ndim == 3:
#print(img1_[..., i].reshape[1, -1].shape)
#test = np.corrcoef(img1_[..., i].reshape[1, -1], img2_[..., i].rehshape(1, -1))
#print(type(test))
ccs = [np.corrcoef(img1_[..., i].reshape(1, -1), img2_[..., i].reshape(1, -1))[0, 1]
for i in range(img1_.shape[2])]
return np.mean(ccs)
else:
raise ValueError('Wrong input image dimensions.')
def PSNR(H_fuse, H_ref):
#Compute number of spectral bands
N_spectral = H_fuse.shape[1]
# Reshaping images
H_fuse_reshaped = H_fuse.view(N_spectral, -1)
H_ref_reshaped = H_ref.view(N_spectral, -1)
# Calculating RMSE of each band
rmse = torch.sqrt(torch.sum((H_ref_reshaped-H_fuse_reshaped)**2, dim=1)/H_fuse_reshaped.shape[1])
# Calculating max of H_ref for each band
max_H_ref, _ = torch.max(H_ref_reshaped, dim=1)
# Calculating PSNR
PSNR = torch.nansum(10*torch.log10(torch.div(max_H_ref, rmse)**2))/N_spectral
return PSNR
def cross_correlation(H_fuse, H_ref):
N_spectral = H_fuse.shape[1]
# Rehsaping fused and reference data
H_fuse_reshaped = H_fuse.view(N_spectral, -1)
H_ref_reshaped = H_ref.view(N_spectral, -1)
# Calculating mean value
mean_fuse = torch.mean(H_fuse_reshaped, 1).unsqueeze(1)
mean_ref = torch.mean(H_ref_reshaped, 1).unsqueeze(1)
CC = torch.sum((H_fuse_reshaped- mean_fuse)*(H_ref_reshaped-mean_ref), 1)/torch.sqrt(torch.sum((H_fuse_reshaped- mean_fuse)**2, 1)*torch.sum((H_ref_reshaped-mean_ref)**2, 1))
CC = torch.mean(CC)
return CC
def local_cross_correlation(img_1, img_2, half_width):
"""
Cross-Correlation Field computation.
Parameters
----------
img_1 : Numpy Array
First image on which calculate the cross-correlation. Dimensions: H, W
img_2 : Numpy Array
Second image on which calculate the cross-correlation. Dimensions: H, W
half_width : int
The semi-size of the window on which calculate the cross-correlation
Return
------
L : Numpy array
The cross-correlation map between img_1 and img_2
"""
w = int(half_width)
ep = 1e-20
if (len(img_1.shape)) != 3:
img_1 = np.expand_dims(img_1, axis=-1)
if (len(img_2.shape)) != 3:
img_2 = np.expand_dims(img_2, axis=-1)
img_1 = np.pad(img_1.astype(np.float64), ((w, w), (w, w), (0, 0)))
img_2 = np.pad(img_2.astype(np.float64), ((w, w), (w, w), (0, 0)))
img_1_cum = np.zeros(img_1.shape)
img_2_cum = np.zeros(img_2.shape)
for i in range(img_1.shape[-1]):
img_1_cum[:, :, i] = integral(img_1[:, :, i]).astype(np.float64)
for i in range(img_2.shape[-1]):
img_2_cum[:, :, i] = integral(img_2[:, :, i]).astype(np.float64)
img_1_mu = (img_1_cum[2 * w:, 2 * w:, :] - img_1_cum[:-2 * w, 2 * w:, :] - img_1_cum[2 * w:, :-2 * w, :]
+ img_1_cum[:-2 * w, :-2 * w, :]) / (4 * w ** 2)
img_2_mu = (img_2_cum[2 * w:, 2 * w:, :] - img_2_cum[:-2 * w, 2 * w:, :] - img_2_cum[2 * w:, :-2 * w, :]
+ img_2_cum[:-2 * w, :-2 * w, :]) / (4 * w ** 2)
img_1 = img_1[w:-w, w:-w, :] - img_1_mu
img_2 = img_2[w:-w, w:-w, :] - img_2_mu
img_1 = np.pad(img_1.astype(np.float64), ((w, w), (w, w), (0, 0)))
img_2 = np.pad(img_2.astype(np.float64), ((w, w), (w, w), (0, 0)))
i2 = img_1 ** 2
j2 = img_2 ** 2
ij = img_1 * img_2
i2_cum = np.zeros(i2.shape)
j2_cum = np.zeros(j2.shape)
ij_cum = np.zeros(ij.shape)
for i in range(i2_cum.shape[-1]):
i2_cum[:, :, i] = integral(i2[:, :, i]).astype(np.float64)
for i in range(j2_cum.shape[-1]):
j2_cum[:, :, i] = integral(j2[:, :, i]).astype(np.float64)
for i in range(ij_cum.shape[-1]):
ij_cum[:, :, i] = integral(ij[:, :, i]).astype(np.float64)
sig2_ij_tot = (ij_cum[2 * w:, 2 * w:, :] - ij_cum[:-2 * w, 2 * w:, :] - ij_cum[2 * w:, :-2 * w, :]
+ ij_cum[:-2 * w, :-2 * w, :])
sig2_ii_tot = (i2_cum[2 * w:, 2 * w:, :] - i2_cum[:-2 * w, 2 * w:, :] - i2_cum[2 * w:, :-2 * w, :]
+ i2_cum[:-2 * w, :-2 * w, :])
sig2_jj_tot = (j2_cum[2 * w:, 2 * w:, :] - j2_cum[:-2 * w, 2 * w:, :] - j2_cum[2 * w:, :-2 * w, :]
+ j2_cum[:-2 * w, :-2 * w, :])
sig2_ij_tot = np.clip(sig2_ij_tot, ep, sig2_ij_tot.max())
sig2_ii_tot = np.clip(sig2_ii_tot, ep, sig2_ii_tot.max())
sig2_jj_tot = np.clip(sig2_jj_tot, ep, sig2_jj_tot.max())
xcorr = sig2_ij_tot / ((sig2_ii_tot * sig2_jj_tot) ** 0.5 + ep)
return xcorr
def normalize_block(im):
"""
Auxiliary Function for Q2n computation.
Parameters
----------
im : Numpy Array
Image on which calculate the statistics. Dimensions: H, W
Return
------
y : Numpy array
The normalized version of im
m : float
The mean of im
s : float
The standard deviation of im
"""
m = np.mean(im)
s = np.std(im, ddof=1)
if s == 0:
s = 1e-10
y = ((im - m) / s) + 1
return y, m, s
def cayley_dickson_property_1d(onion1, onion2):
"""
Cayley-Dickson construction for 1-D arrays.
Auxiliary function for Q2n calculation.
Parameters
----------
onion1 : Numpy Array
First 1-D array
onion2 : Numpy Array
Second 1-D array
Return
------
ris : Numpy array
The result of Cayley-Dickson construction on the two arrays.
"""
n = onion1.__len__()
if n > 1:
half_pos = int(n / 2)
a = onion1[:half_pos]
b = onion1[half_pos:]
neg = np.ones(b.shape)
neg[1:] = -1
b = b * neg
c = onion2[:half_pos]
d = onion2[half_pos:]
d = d * neg
if n == 2:
ris = np.concatenate([(a * c) - (d * b), (a * d) + (c * b)])
else:
ris1 = cayley_dickson_property_1d(a, c)
ris2 = cayley_dickson_property_1d(d, b * neg)
ris3 = cayley_dickson_property_1d(a * neg, d)
ris4 = cayley_dickson_property_1d(c, b)
aux1 = ris1 - ris2
aux2 = ris3 + ris4
ris = np.concatenate([aux1, aux2])
else:
ris = onion1 * onion2
return ris
def cayley_dickson_property_2d(onion1, onion2):
"""
Cayley-Dickson construction for 2-D arrays.
Auxiliary function for Q2n calculation.
Parameters
----------
onion1 : Numpy Array
First MultiSpectral img. Dimensions: H, W, Bands
onion2 : Numpy Array
Second MultiSpectral img. Dimensions: H, W, Bands
Return
------
ris : Numpy array
The result of Cayley-Dickson construction on the two arrays.
"""
dim3 = onion1.shape[-1]
if dim3 > 1:
half_pos = int(dim3 / 2)
a = onion1[:, :, :half_pos]
b = onion1[:, :, half_pos:]
b = np.concatenate([np.expand_dims(b[:, :, 0], -1), -b[:, :, 1:]], axis=-1)
c = onion2[:, :, :half_pos]
d = onion2[:, :, half_pos:]
d = np.concatenate([np.expand_dims(d[:, :, 0], -1), -d[:, :, 1:]], axis=-1)
if dim3 == 2:
ris = np.concatenate([(a * c) - (d * b), (a * d) + (c * b)], axis=-1)
else:
ris1 = cayley_dickson_property_2d(a, c)
ris2 = cayley_dickson_property_2d(d,
np.concatenate([np.expand_dims(b[:, :, 0], -1), -b[:, :, 1:]], axis=-1))
ris3 = cayley_dickson_property_2d(np.concatenate([np.expand_dims(a[:, :, 0], -1), -a[:, :, 1:]], axis=-1),
d)
ris4 = cayley_dickson_property_2d(c, b)
aux1 = ris1 - ris2
aux2 = ris3 + ris4
ris = np.concatenate([aux1, aux2], axis=-1)
else:
ris = onion1 * onion2
return ris
def q_index_metric(im1, im2, size):
"""
Q2n calculation on a window of dimension (size, size).
Auxiliary function for Q2n calculation.
Parameters
----------
im1 : Numpy Array
First MultiSpectral img. Dimensions: H, W, Bands
im2 : Numpy Array
Second MultiSpectral img. Dimensions: H, W, Bands
size : int
The size of the squared windows on which calculate the UQI index
Return
------
q : Numpy array
The Q2n calculated on a window of dimension (size,size).
"""
im1 = im1.astype(np.double)
im2 = im2.astype(np.double)
im2 = np.concatenate([np.expand_dims(im2[:, :, 0], -1), -im2[:, :, 1:]], axis=-1)
depth = im1.shape[-1]
for i in range(depth):
im1[:, :, i], m, s = normalize_block(im1[:, :, i])
if m == 0:
if i == 0:
im2[:, :, i] = im2[:, :, i] - m + 1
else:
im2[:, :, i] = -(-im2[:, :, i] - m + 1)
else:
if i == 0:
im2[:, :, i] = ((im2[:, :, i] - m) / s) + 1
else:
im2[:, :, i] = -(((-im2[:, :, i] - m) / s) + 1)
m1 = np.mean(im1, axis=(0, 1))
m2 = np.mean(im2, axis=(0, 1))
mod_q1m = np.sqrt(np.sum(m1 ** 2))
mod_q2m = np.sqrt(np.sum(m2 ** 2))
mod_q1 = np.sqrt(np.sum(im1 ** 2, axis=-1))
mod_q2 = np.sqrt(np.sum(im2 ** 2, axis=-1))
term2 = mod_q1m * mod_q2m
term4 = mod_q1m ** 2 + mod_q2m ** 2
temp = (size ** 2) / (size ** 2 - 1)
int1 = temp * np.mean(mod_q1 ** 2)
int2 = temp * np.mean(mod_q2 ** 2)
int3 = temp * (mod_q1m ** 2 + mod_q2m ** 2)
term3 = int1 + int2 - int3
mean_bias = 2 * term2 / term4
if term3 == 0:
q = np.zeros((1, 1, depth), dtype='float64')
q[:, :, -1] = mean_bias
else:
cbm = 2 / term3
qu = cayley_dickson_property_2d(im1, im2)
qm = cayley_dickson_property_1d(m1, m2)
qv = temp * np.mean(qu, axis=(0, 1))
q = qv - temp * qm
q = q * mean_bias * cbm
return q
def Q2n(outputs, labels, q_block_size=32, q_shift=32):
"""
Q2n calculation on a window of dimension (size, size).
Auxiliary function for Q2n calculation.
[Scarpa21] Scarpa, Giuseppe, and Matteo Ciotola. "Full-resolution quality assessment for pansharpening.",
arXiv preprint arXiv:2108.06144
[Garzelli09] A. Garzelli and F. Nencini, "Hypercomplex quality assessment of multi/hyper-spectral images,"
IEEE Geoscience and Remote Sensing Letters, vol. 6, no. 4, pp. 662-665, October 2009.
[Vivone20] G. Vivone, M. Dalla Mura, A. Garzelli, R. Restaino, G. Scarpa, M.O. Ulfarsson, L. Alparone, and J. Chanussot, "A New Benchmark Based on Recent Advances in Multispectral Pansharpening: Revisiting pansharpening with classical and emerging pansharpening methods",
IEEE Geoscience and Remote Sensing Magazine, doi: 10.1109/MGRS.2020.3019315.
Parameters
----------
outputs : Numpy Array
The Fused image. Dimensions: H, W, Bands
labels : Numpy Array
The reference image. Dimensions: H, W, Bands
q_block_size : int
The windows size on which calculate the Q2n index
q_shift : int
The stride for Q2n index calculation
Return
------
q2n_index : float
The Q2n index.
q2n_index_map : Numpy Array
The Q2n map, on a support of (q_block_size, q_block_size)
"""
height, width, depth = labels.shape
stepx = ceil(height / q_shift)
stepy = ceil(width / q_shift)
if stepy <= 0:
stepx = 1
stepy = 1
est1 = (stepx - 1) * q_shift + q_block_size - height
est2 = (stepy - 1) * q_shift + q_block_size - width
if (est1 != 0) and (est2 != 0):
labels = np.pad(labels, ((0, est1), (0, est2), (0, 0)), mode='reflect')
outputs = np.pad(outputs, ((0, est1), (0, est2), (0, 0)), mode='reflect')
outputs = outputs.astype(np.int16)
labels = labels.astype(np.int16)
height, width, depth = labels.shape
if ceil(log2(depth)) - log2(depth) != 0:
exp_difference = 2 ** (ceil(log2(depth))) - depth
diff_zeros = np.zeros((height, width, exp_difference), dtype="float64")
labels = np.concatenate([labels, diff_zeros], axis=-1)
outputs = np.concatenate([outputs, diff_zeros], axis=-1)
height, width, depth = labels.shape
values = np.zeros((stepx, stepy, depth))
for j in range(stepx):
for i in range(stepy):
values[j, i, :] = q_index_metric(
labels[j * q_shift:j * q_shift + q_block_size, i * q_shift: i * q_shift + q_block_size, :],
outputs[j * q_shift:j * q_shift + q_block_size, i * q_shift: i * q_shift + q_block_size, :],
q_block_size
)
q2n_index_map = np.sqrt(np.sum(values ** 2, axis=-1))
q2n_index = np.mean(q2n_index_map)
return q2n_index.item()
# def ERGAS(outputs, labels, ratio):
# """
# Erreur Relative Globale Adimensionnelle de Synthèse (ERGAS).
#
#
# [Scarpa21] Scarpa, Giuseppe, and Matteo Ciotola. "Full-resolution quality assessment for pansharpening.",
# arXiv preprint arXiv:2108.06144
# [Ranchin00] T. Ranchin and L. Wald, "Fusion of high spatial and spectral resolution images: the ARSIS concept and its implementation,"
# Photogrammetric Engineering and Remote Sensing, vol. 66, no. 1, pp. 4961, January 2000.
# [Vivone20] G. Vivone, M. Dalla Mura, A. Garzelli, R. Restaino, G. Scarpa, M.O. Ulfarsson, L. Alparone, and J. Chanussot, "A New Benchmark Based on Recent Advances in Multispectral Pansharpening: Revisiting pansharpening with classical and emerging pansharpening methods",
# IEEE Geoscience and Remote Sensing Magazine, doi: 10.1109/MGRS.2020.3019315.
#
# Parameters
# ----------
# outputs : Numpy Array
# The Fused image. Dimensions: H, W, Bands
# labels : Numpy Array
# The reference image. Dimensions: H, W, Bands
# ratio : int
# PAN-MS resolution ratio
#
# Return
# ------
# ergas_index : float
# The ERGAS index.
#
# """
#
# mu = np.mean(labels, axis=(0, 1)) ** 2
# nbands = labels.shape[-1]
# error = np.mean((outputs - labels) ** 2, axis=(0, 1))
# ergas_index = 100 / ratio * np.sqrt(np.sum(error / mu) / nbands)
#
# return np.mean(ergas_index).item()
def SAM(outputs, labels):
"""
Spectral Angle Mapper (SAM).
[Scarpa21] Scarpa, Giuseppe, and Matteo Ciotola. "Full-resolution quality assessment for pansharpening.",
arXiv preprint arXiv:2108.06144
[Yuhas92] R. H. Yuhas, A. F. H. Goetz, and J. W. Boardman, "Discrimination among semi-arid landscape endmembers using the Spectral Angle Mapper (SAM) algorithm,"
in Proceeding Summaries 3rd Annual JPL Airborne Geoscience Workshop, 1992, pp. 147-149.
[Vivone20] G. Vivone, M. Dalla Mura, A. Garzelli, R. Restaino, G. Scarpa, M.O. Ulfarsson, L. Alparone, and J. Chanussot, "A New Benchmark Based on Recent Advances in Multispectral Pansharpening: Revisiting pansharpening with classical and emerging pansharpening methods",
IEEE Geoscience and Remote Sensing Magazine, doi: 10.1109/MGRS.2020.3019315.
Parameters
----------
outputs : Numpy Array
The Fused image. Dimensions: H, W, Bands
labels : Numpy Array
The reference image. Dimensions: H, W, Bands
Return
------
angle : float
The SAM index in degree.
"""
norm_outputs = np.sum(outputs ** 2, axis=-1)
norm_labels = np.sum(labels ** 2, axis=-1)
scalar_product = np.sum(outputs * labels, axis=-1)
norm_product = np.sqrt(norm_outputs * norm_labels)
scalar_product[norm_product == 0] = np.nan
norm_product[norm_product == 0] = np.nan
scalar_product = scalar_product.flatten()
norm_product = norm_product.flatten()
angle = np.sum(np.arccos(np.clip(scalar_product / norm_product, a_min=-1, a_max=1)), axis=-1) / norm_product.shape[0]
angle = angle * 180 / np.pi
return angle
def Q(outputs, labels, block_size=32):
"""
Universal Quality Index (UQI).
[Scarpa21] Scarpa, Giuseppe, and Matteo Ciotola. "Full-resolution quality assessment for pansharpening.",
arXiv preprint arXiv:2108.06144
[Wang02] Z. Wang and A. C. Bovik, "A universal image quality index,"
IEEE Signal Processing Letters, vol. 9, no. 3, pp. 81-84, March 2002.
[Vivone20] G. Vivone, M. Dalla Mura, A. Garzelli, R. Restaino, G. Scarpa, M.O. Ulfarsson, L. Alparone, and J. Chanussot, "A New Benchmark Based on Recent Advances in Multispectral Pansharpening: Revisiting pansharpening with classical and emerging pansharpening methods",
IEEE Geoscience and Remote Sensing Magazine, doi: 10.1109/MGRS.2020.3019315.
Parameters
----------
outputs : Numpy Array
The Fused image. Dimensions: H, W, Bands
labels : Numpy Array
The reference image. Dimensions: H, W, Bands
block_size : int
The windows size on which calculate the Q2n index
Return
------
quality : float
The UQI index.
"""
N = block_size ** 2
nbands = labels.shape[-1]
kernel = np.ones((block_size, block_size))
pad_size = floor((kernel.shape[0] - 1) / 2)
outputs_sq = outputs ** 2
labels_sq = labels ** 2
outputs_labels = outputs * labels
quality = np.zeros(nbands)
for i in range(nbands):
outputs_sum = ft.convolve(outputs[:, :, i], kernel)
labels_sum = ft.convolve(labels[:, :, i], kernel)
outputs_sq_sum = ft.convolve(outputs_sq[:, :, i], kernel)
labels_sq_sum = ft.convolve(labels_sq[:, :, i], kernel)
outputs_labels_sum = ft.convolve(outputs_labels[:, :, i], kernel)
outputs_sum = outputs_sum[pad_size:-pad_size, pad_size:-pad_size]
labels_sum = labels_sum[pad_size:-pad_size, pad_size:-pad_size]
outputs_sq_sum = outputs_sq_sum[pad_size:-pad_size, pad_size:-pad_size]
labels_sq_sum = labels_sq_sum[pad_size:-pad_size, pad_size:-pad_size]
outputs_labels_sum = outputs_labels_sum[pad_size:-pad_size, pad_size:-pad_size]
outputs_labels_sum_mul = outputs_sum * labels_sum
outputs_labels_sum_mul_sq = outputs_sum ** 2 + labels_sum ** 2
numerator = 4 * (N * outputs_labels_sum - outputs_labels_sum_mul) * outputs_labels_sum_mul
denominator_temp = N * (outputs_sq_sum + labels_sq_sum) - outputs_labels_sum_mul_sq
denominator = denominator_temp * outputs_labels_sum_mul_sq
index = (denominator_temp == 0) & (outputs_labels_sum_mul_sq != 0)
quality_map = np.ones(denominator.shape)
quality_map[index] = 2 * outputs_labels_sum_mul[index] / outputs_labels_sum_mul_sq[index]
index = denominator != 0
quality_map[index] = numerator[index] / denominator[index]
quality[i] = np.mean(quality_map)
return np.mean(quality).item()
def coregistration(ms, pan, kernel, ratio=4, search_win=4):
"""
Coregitration function for MS-PAN pair.
[Scarpa21] Scarpa, Giuseppe, and Matteo Ciotola. "Full-resolution quality assessment for pansharpening.",
arXiv preprint arXiv:2108.06144
Parameters
----------
ms : Numpy Array
The Multi-Spectral image. Dimensions: H, W, Bands
pan : Numpy Array
The PAN image. Dimensions: H, W
kernel : Numpy Array
The filter array.
ratio : int
PAN-MS resolution ratio
search_win : int
The windows in which search the optimal value for the coregistration step
Return
------
r : Numpy Array
The optimal raw values.
c : Numpy Array
The optimal column values.
"""
nbands = ms.shape[-1]
p = ft.convolve(pan, kernel, mode='nearest')
rho = np.zeros((search_win, search_win, nbands))
r = np.zeros(nbands)
c = np.copy(r)
for i in range(search_win):
for j in range(search_win):
rho[i, j, :] = np.mean(
local_cross_correlation(ms, np.expand_dims(p[i::ratio, j::ratio], -1), floor(ratio / 2)), axis=(0, 1))
max_value = np.amax(rho, axis=(0, 1))
for b in range(nbands):
x = rho[:, :, b]
max_value = x.max()
pos = np.where(x == max_value)
if len(pos[0]) != 1:
pos = (pos[0][0], pos[1][0])
pos = tuple(map(int, pos))
r[b] = pos[0]
c[b] = pos[1]
r = np.squeeze(r).astype(np.uint8)
c = np.squeeze(c).astype(np.uint8)
return r, c
def resize_with_mtf(outputs, ms, pan, sensor, ratio=4, dim_cut=21):
"""
Resize of Fused Image to MS scale, in according to the coregistration with the PAN.
If dim_cut is different by zero a cut is made on both outputs and ms, to discard possibly values affected by paddings.
[Scarpa21] Scarpa, Giuseppe, and Matteo Ciotola. "Full-resolution quality assessment for pansharpening.",
arXiv preprint arXiv:2108.06144
Parameters
----------
outputs : Numpy Array
Fused MultiSpectral img. Dimensions: H, W, Bands
ms : Numpy Array
MultiSpectral img. Dimensions: H, W, Bands
pan : Numpy Array
Panchromatic img. Dimensions: H, W, Bands
sensor : str
The name of the satellites which has provided the images.
ratio : int
Resolution scale which elapses between MS and PAN.
dim_cut : int
Cutting dimension for obtaining "valid" image to which apply the metrics
Return
------
x : NumPy array
Fused MultiSpectral image, coregistered with the PAN, low-pass filtered and decimated. If dim_cut is different
by zero it is also cut
ms : NumPy array
MultiSpectral img. If dim_cut is different by zero it is cut.
"""
from spectral_tools import gen_mtf
kernel = gen_mtf(ratio, sensor)
kernel = kernel.astype(np.float32)
nbands = kernel.shape[-1]
pad_size = floor((kernel.shape[0] - 1) / 2)
r, c = coregistration(ms, pan, kernel[:, :, 0], ratio)
kernel = np.moveaxis(kernel, -1, 0)
kernel = np.expand_dims(kernel, axis=1)
kernel = torch.from_numpy(kernel).type(torch.float32)
depthconv = nn.Conv2d(in_channels=nbands,
out_channels=nbands,
groups=nbands,
kernel_size=kernel.shape,
bias=False)
depthconv.weight.data = kernel
depthconv.weight.requires_grad = False
pad = nn.ReplicationPad2d(pad_size)
x = np.zeros(ms.shape, dtype=np.float32)
outputs = np.expand_dims(np.moveaxis(outputs, -1, 0), 0)
outputs = torch.from_numpy(outputs)
outputs = pad(outputs)
outputs = depthconv(outputs)
outputs = outputs.detach().cpu().numpy()
outputs = np.moveaxis(np.squeeze(outputs, 0), 0, -1)
for b in range(nbands):
x[:, :, b] = outputs[r[b]::ratio, c[b]::ratio, b]
if dim_cut != 0:
x = x[dim_cut:-dim_cut, dim_cut:-dim_cut, :]
ms = ms[dim_cut:-dim_cut, dim_cut:-dim_cut, :]
return x, ms
def ReproERGAS(outputs, ms, pan, sensor, ratio=4, dim_cut=0):
"""
Reprojected Erreur Relative Globale Adimensionnelle de Synthèse (ERGAS).
[Scarpa21] Scarpa, Giuseppe, and Matteo Ciotola. "Full-resolution quality assessment for pansharpening.",
arXiv preprint arXiv:2108.06144
Parameters
----------
outputs : Numpy Array
Fused MultiSpectral img. Dimensions: H, W, Bands
ms : Numpy Array
MultiSpectral img. Dimensions: H, W, Bands
pan : Numpy Array
Panchromatic img. Dimensions: H, W, Bands
sensor : str
The name of the satellites which has provided the images.
ratio : int
Resolution scale which elapses between MS and PAN.
dim_cut : int
Cutting dimension for obtaining "valid" image to which apply the metrics
Return
------
R-ERGAS : float
The R-ERGAS index
"""
outputs, ms = resize_with_mtf(outputs, ms, pan, sensor, ratio, dim_cut)
return ERGAS(outputs, ms, ratio)
def ReproSAM(outputs, ms, pan, sensor, ratio=4, dim_cut=0):
"""
Reprojected Spectral Angle Mapper (SAM).
[Scarpa21] Scarpa, Giuseppe, and Matteo Ciotola. "Full-resolution quality assessment for pansharpening.",
arXiv preprint arXiv:2108.06144
Parameters
----------
outputs : Numpy Array
Fused MultiSpectral img. Dimensions: H, W, Bands
ms : Numpy Array
MultiSpectral img. Dimensions: H, W, Bands
pan : Numpy Array
Panchromatic img. Dimensions: H, W, Bands
sensor : str
The name of the satellites which has provided the images.
ratio : int
Resolution scale which elapses between MS and PAN.
dim_cut : int
Cutting dimension for obtaining "valid" image to which apply the metrics
Return
------
R-SAM : float
The R-SAM index
"""
outputs, ms = resize_with_mtf(outputs, ms, pan, sensor, ratio, dim_cut)
return SAM(outputs, ms)
def ReproQ2n(outputs, ms, pan, sensor, ratio=4, q_block_size=32, q_shift=32, dim_cut=0):
"""
Reprojected Q2n.
[Scarpa21] Scarpa, Giuseppe, and Matteo Ciotola. "Full-resolution quality assessment for pansharpening.",
arXiv preprint arXiv:2108.06144
Parameters
----------
outputs : Numpy Array
Fused MultiSpectral img. Dimensions: H, W, Bands
ms : Numpy Array
MultiSpectral img. Dimensions: H, W, Bands
pan : Numpy Array
Panchromatic img. Dimensions: H, W, Bands
sensor : str
The name of the satellites which has provided the images.
ratio : int
Resolution scale which elapses between MS and PAN.
q_block_size : int
The windows size on which calculate the Q2n index
q_shift : int
The stride for Q2n index calculation
dim_cut : int
Cutting dimension for obtaining "valid" image to which apply the metrics
Return
------
r_q2n : float
The R-Q2n index
"""
outputs, ms = resize_with_mtf(outputs, ms, pan, sensor, ratio, dim_cut)
r_q2n, _ = Q2n(outputs, ms, q_block_size, q_shift)
return r_q2n
def ReproQ(outputs, ms, pan, sensor, ratio=4, q_block_size=32, dim_cut=0):
"""
Reprojected Q.
[Scarpa21] Scarpa, Giuseppe, and Matteo Ciotola. "Full-resolution quality assessment for pansharpening.",
arXiv preprint arXiv:2108.06144
Parameters
----------
outputs : Numpy Array
Fused MultiSpectral img. Dimensions: H, W, Bands
ms : Numpy Array
MultiSpectral img. Dimensions: H, W, Bands
pan : Numpy Array
Panchromatic img. Dimensions: H, W, Bands
sensor : str
The name of the satellites which has provided the images.
ratio : int
Resolution scale which elapses between MS and PAN.
q_block_size : int
The windows size on which calculate the Q index
dim_cut : int
Cutting dimension for obtaining "valid" image to which apply the metrics
Return
------
r_q : float
The R-Q index
"""
outputs, ms = resize_with_mtf(outputs, ms, pan, sensor, ratio, dim_cut)
r_q = Q(outputs, ms, q_block_size)
return r_q
def ReproMetrics(outputs, ms, pan, sensor, ratio=4, q_block_size=32, q_shift=32, dim_cut=0):
"""
Computation of all reprojected metrics.
[Scarpa21] Scarpa, Giuseppe, and Matteo Ciotola. "Full-resolution quality assessment for pansharpening.",
arXiv preprint arXiv:2108.06144
Parameters
----------
outputs : Numpy Array
Fused MultiSpectral img. Dimensions: H, W, Bands
ms : Numpy Array
MultiSpectral img. Dimensions: H, W, Bands
pan : Numpy Array
Panchromatic img. Dimensions: H, W, Bands
sensor : str
The name of the satellites which has provided the images.
ratio : int
Resolution scale which elapses between MS and PAN.
q_block_size : int
The windows size on which calculate the Q2n and Q index
q_shift : int
The stride for Q2n index calculation
dim_cut : int