-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
187 lines (151 loc) · 5.54 KB
/
utils.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
import numpy as np
from readrsimage import readrsimage_with_geoinfo,readrsimage
import cv2 as cv
from cutwithgeoinfo import CutWithGeoInfo
import os
def make_test_image(pan_image_path, mul_image_path):
pan_image, pan_prj, pan_geo = readrsimage_with_geoinfo(pan_image_path)
mul_image, mul_prj, mul_geo = readrsimage_with_geoinfo(mul_image_path)
pan_resample_image = cv.resize(pan_image, (128, 128), interpolation=cv.INTER_LINEAR)
mul_resample_image = cv.resize(mul_image, (64, 64), interpolation = cv.INTER_LINEAR)
new_pan_geo = (pan_geo[0], 30, pan_geo[2], pan_geo[3], pan_geo[4], -30)
new_mul_geo = (mul_geo[0], 60, mul_geo[2], mul_geo[3], mul_geo[4], -60)
if os.path.exists("./testimages") == False:
os.mkdir("./testimages")
pan_resample_image = np.reshape(pan_resample_image, (128, 128, 1))
mul_resample_image = np.reshape(mul_resample_image, (64, 64, 7))
CutWithGeoInfo.write_with_geo("./testimages/pan11.tif", pan_resample_image, pan_prj, new_pan_geo)
CutWithGeoInfo.write_with_geo("./testimages/mul11.tif", mul_resample_image, mul_prj, new_mul_geo)
CutWithGeoInfo.write_with_geo("./testimages/label11.tif", mul_image, mul_prj, mul_geo)
def mul_psnr(fake_hr, real_hr):
"""
Only for 8 bit images.
"""
# print(fake_hr.shape)
channels = fake_hr.shape[2]
fake_hr = fake_hr.astype(np.float32)
real_hr = real_hr.astype(np.float32)
def single_band_psnr(img1, img2):
diff = img1 - img2
mse = np.mean(np.square(diff))
psnr = 10 * np.log10(255 * 255 / mse)
return psnr
psnr_sum = 0
for band in range(channels):
fake_band_img = fake_hr[:, :, band]
real_band_img = real_hr[:, :, band]
psnr_sum += single_band_psnr(fake_band_img, real_band_img)
psnr = round(psnr_sum/channels, 2)
return psnr
def rmse(fake_hr, real_hr):
"""
Only for 8 bit images.
"""
# print(fake_hr.shape)
if len(fake_hr.shape) == 3:
channels = fake_hr.shape[2]
else:
channels = 1
fake_hr = np.reshape(fake_hr, (fake_hr.shape[0], fake_hr.shape[1], 1))
real_hr = np.reshape(real_hr, (real_hr.shape[0], real_hr.shape[1], 1))
fake_hr = fake_hr.astype(np.float32)
real_hr = real_hr.astype(np.float32)
def single_mse(img1, img2):
diff = img1 - img2
mse = np.mean(np.square(diff))
return mse
mse_sum = 0
for band in range(channels):
fake_band_img = fake_hr[:, :, band]
real_band_img = real_hr[:, :, band]
mse_sum += single_mse(fake_band_img, real_band_img)
rmse = np.sqrt(mse_sum)
rmse = round(rmse/channels, 2)
return rmse
def MG(image):
height = image.shape[0]
width = image.shape[1]
channels = image.shape[2]
MG_sum = 0
for channel in range(channels):
channel_mg_sum = 0
for row in range(1, height):
for column in range(1, width):
dy = image[row, column, channel] - image[row-1, column, channel]
dx = image[row, column, channel] - image[row, column - 1, channel]
channel_mg_sum += np.sqrt((dx**2 + dy**2)/2)
channels_mg = channel_mg_sum/((height-1)*(width-1))
MG_sum += channels_mg
MG = round(MG_sum/channels, 2)
return MG
def ERGAS(hr_mul, label, lr_mul):
"""
calc ergas.
"""
h = 30
l = 60
channels = hr_mul.shape[2]
inner_sum = 0
for channel in range(channels):
band_img1 = hr_mul[:, :, channel]
band_img2 = label[:, :, channel]
band_img3 = lr_mul[:, :, channel]
rmse_value = rmse(band_img1, band_img2)
m = np.mean(band_img3)
inner_sum += np.power((rmse_value/m), 2)
mean_sum = inner_sum/channels
ergas = 100*(h/l)*np.sqrt(mean_sum)
return ergas
def SAM(image1, image2):
"""
Calculate SAM(spectral angle mapper).
"""
if image1.shape != image2.shape:
raise Exception("shape is not the same.")
height = image1.shape[0]
width = image1.shape[1]
channels = image1.shape[2]
def vector_norm(vector):
length = len(vector)
square_sum = 0
for i in range(length):
value = np.power(vector[i], 2)
square_sum += value
vector_norm = np.sqrt(square_sum)
return vector_norm
def vector_inner_poduct(vector1, vector2):
dot_sum = 0
length = len(vector1)
for i in range(length):
value = vector1[i] * vector2[i]
dot_sum += value
return dot_sum
pixel_num = height * width
total_sam = 0
for row in range(height):
for col in range(width):
vector1 = image1[row, col, :]
vector2 = image2[row, col, :]
u_value = vector_inner_poduct(vector1, vector2)
d_value = vector_norm(vector1)*vector_norm(vector2)
single_pixel_sam = np.arccos(u_value/d_value)
total_sam += single_pixel_sam
mean_sam = total_sam/pixel_num
return mean_sam
def eachbandrmse(image1, image2):
channels = image1.shape[2]
rmse_values = np.zeros(channels, np.float32)
for i in range(channels):
rmse_values[i] = rmse(image1[:, :, i],image2[:, :, i])
return rmse_values
def norm(image):
max_value = np.max(image)
min_value = np.min(image)
image = 255 * (image - min_value)/float(max_value - min_value)
return image
def main():
# make_test_image("./testimages/4656_p.tif", "./testimages/4656_m.tif")
image1 = readrsimage()
image2 = readrsimage()
if __name__ == "__main__":
main()