-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmetrics.py
172 lines (151 loc) · 4.85 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
import numpy as np
import math
from skimage.metrics import structural_similarity as SSIM
from skimage.metrics import peak_signal_noise_ratio as PSNR
def compare_ergas(x_true, x_pred, ratio):
'''
Calculate ERGAS, ERGAS offers a global indication of the quality of fused image.The ideal value is 0.
:param x_true:
:param x_pred:
:param ratio: 上采样系数
:return:
'''
x_true, x_pred = img_2d_mat(x_true=x_true, x_pred=x_pred)
sum_ergas = 0
for i in range(x_true.shape[0]):
vec_x = x_true[i]
vec_y = x_pred[i]
err = vec_x - vec_y
r_mse = np.mean(np.power(err, 2))
tmp = r_mse / (np.mean(vec_x)**2)
sum_ergas += tmp
return (100 / ratio) * np.sqrt(sum_ergas / x_true.shape[0])
def img_2d_mat(x_true, x_pred):
'''
# 将三维的多光谱图像转为2位矩阵
:param x_true: (H, W, C)
:param x_pred: (H, W, C)
:return: a matrix which shape is (C, H * W)
'''
h, w, c = x_true.shape
x_true, x_pred = x_true.astype(np.float32), x_pred.astype(np.float32)
x_mat = np.zeros((c, h * w), dtype=np.float32)
y_mat = np.zeros((c, h * w), dtype=np.float32)
for i in range(c):
x_mat[i] = x_true[:, :, i].reshape((1, -1))
y_mat[i] = x_pred[:, :, i].reshape((1, -1))
return x_mat, y_mat
'''
img1.shape: (512, 512, 31)
img2.shape: (512, 512, 31)
'''
def MPSNR(img1, img2, data_range):
ch = np.size(img1,2)
if ch == 1:
mse = np.mean((img1 - img2) ** 2)
if mse == 0:
return 100
PIXEL_MAX = data_range
s = 20 * math.log10(PIXEL_MAX / math.sqrt(mse))
return s
else:
sum = 0
for i in range(ch):
mse = np.mean((img1[:,:,i] - img2[:,:,i]) ** 2)
if mse == 0:
return 100
PIXEL_MAX = data_range
s = 20 * math.log10(PIXEL_MAX / math.sqrt(mse))
sum = sum + s
s = sum / ch
return s
def MPSNR1(img1, img2):
ch = np.size(img1,2)
if ch == 1:
# mse = np.mean((img1 - img2) ** 2)
# if mse == 0:
# return 100
# PIXEL_MAX = 255.0
s = PSNR(img1, img2)
return s
else:
sum = 0
for i in range(ch):
# mse = np.mean((img1[:,:,i] - img2[:,:,i]) ** 2)
# if mse == 0:
# return 100
# PIXEL_MAX = 1.0
s = PSNR(img1[:,:,i], img2[:,:,i])
sum = sum + s
s = sum / ch
return s
def MSSIM(img1, img2, data_range):
'''
:param img1: shape: (512, 512, 31)
:param img2: shape: (512, 512, 31)
:return:
'''
ch = np.size(img1, 2)
if ch == 1:
s = SSIM(img1, img2, data_range=data_range)
return s
else:
sum = 0
for i in range(ch):
s = SSIM(img1[:, :, i], img2[:, :, i], data_range=data_range)
sum = sum + s
s = sum / ch
return s
def Loss_SAM1(x_true, x_pred):
'''
:param x_true: 高光谱图像:格式:(H, W, C)
:param x_pred: 高光谱图像:格式:(H, W, C)
:return: 计算原始高光谱数据与重构高光谱数据的光谱角相似度
'''
num = 0
sum_sam = 0
x_true, x_pred = x_true.astype(np.float32), x_pred.astype(np.float32)
for x in range(x_true.shape[0]):
for y in range(x_true.shape[1]):
tmp_pred = x_pred[x, y].ravel()
tmp_true = x_true[x, y].ravel()
if np.linalg.norm(tmp_true) != 0 and np.linalg.norm(tmp_pred) != 0:
sum_sam += np.arccos(
np.minimum(1, np.inner(tmp_pred, tmp_true) / (np.linalg.norm(tmp_true) * np.linalg.norm(tmp_pred))))
num += 1
sam_deg = (sum_sam / num) * 180 / np.pi
return sam_deg
def MSAM(img1, img2):
'''
:param img1: shape: (512, 512, 31)
:param img2: shape: (512, 512, 31)
:return:
'''
return Loss_SAM1(img1, img2)
def RMSE(img1, img2):
assert img1.shape == img2.shape
img1, img2 = img1.astype(np.float32), img2.astype(np.float32)
return np.linalg.norm(img1 - img2) / (np.sqrt(img1.shape[0] * img1.shape[1] * img1.shape[2]))
def sum_dict(a, b):
temp = dict()
for key in a.keys()| b.keys():
temp[key] = sum([d.get(key, 0) for d in (a, b)])
return temp
def quality_assessment(x_true, x_pred, data_range, ratio, multi_dimension=False, block_size=8):
'''
:param multi_dimension:
:param ratio:
:param data_range:
:param x_true:
:param x_pred:
:param block_size
:return:
'''
result = {
'MPSNR': MPSNR(x_true, x_pred, data_range=data_range),
'MSSIM': MSSIM(x_true, x_pred, data_range=data_range),
'ERGAS': compare_ergas(x_true=x_true, x_pred=x_pred, ratio=ratio),
'SAM': MSAM(x_true, x_pred),
'RMSE': RMSE(x_true, x_pred),
}
return result