-
Notifications
You must be signed in to change notification settings - Fork 1
/
functions.py
286 lines (251 loc) · 6.69 KB
/
functions.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
import random
import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import interp1d
import scipy.io as sio
import cv2
import scipy
import os
# this is a tool part serving for other codes
def Fill_B(B, h, w):
'''
change the size of Blur matrix B to meet the blur operation
since the size of estimate B is 10*10
:param B:
:param h:
:param w:
:return:
'''
tB = np.zeros([h, w], dtype=np.float32)
tB[-4:, -4:] = B[-4:, -4:]
tB[:6, -4:] = B[:6, -4:]
tB[-4:, :6] = B[-4:, :6]
tB[:6, :6] = B[:6, :6]
return tB
def generateRandomList(numlist: list, maxNum, count):
'''
produce needed random list
:param numlist: random list
:param maxNum: the max number
:param count: the count
:return:
'''
i = 0
while i < count:
num = random.randint(1, maxNum)
if num not in numlist:
numlist.append(num)
i += 1
def unfold(x, dim):
'''
Matrix shift by dimension
:param x:
:param dim:
:return:
'''
new_first_dim = x.shape[dim]
x = np.swapaxes(x, 0, dim)
return np.reshape(x, [new_first_dim, -1])
def getDenoiseV(X, k):
'''
get needed Vector by SVD for denoising
:param X:
:param k:
:return:
'''
h, w, c = X.shape
# _, c = X.shape
X = np.reshape(X, [h * w, -1], order='F').T
_, s, V = scipy.linalg.svd(X.T, full_matrices=False)
return V.T[:, :k]
def denoise(X, V):
'''
denoising
:param X:
:param V:
:return:
'''
h, w, c = X.shape
X_t = unfold(X, 2)
X_t_denoise = np.dot(np.dot(V, V.T), X_t)
X = np.reshape(X_t_denoise.T, [h, w, -1], order='F')
X[X < 0] = 0.0
X[X > 1] = 1.0
return X
def getFalseColorImage(X2, r=4, g=2, b=1):
'''
get falseColor image
:param X2:
:param r:
:param g:
:param b:
:return:
'''
h, w, _ = X2.shape
xin_rgb = np.zeros([h, w, 3], dtype=np.float32)
xin_rgb[:, :, 0] = X2[:, :, r]
xin_rgb[:, :, 1] = X2[:, :, g]
xin_rgb[:, :, 2] = X2[:, :, b]
return xin_rgb
def matchSensorWithWaveRange(psf, minWL, maxWL):
'''
When generating R, the band range of the sensor and image is pre-matched
and the spectral response function of the specified range is returned
:param psf:
:param minWL:
:param maxWL:
:return:
'''
r, c = psf.shape
count = 0
for i in range(r):
if psf[i, 0] >= minWL and psf[i, 0] <= maxWL:
count += 1
if count == 1:
valid_spf = psf[i, :]
else:
valid_spf = np.vstack((valid_spf, psf[i, :]))
if psf[i, 0] > maxWL:
break
return valid_spf
def sumtoOne(R):
'''
R needs to be normalized, which is divided by the sum of the rows (the number of bands in hrhs)
:param R:
:return:
'''
div = np.sum(R, axis=1)
div = np.expand_dims(div, axis=-1)
R = R / div
return R
def Fill_B(B, kernel_size, h, w):
'''
change the size of Blur matrix B to meet the blur operation
since the size of estimate B is r*r
:param B:
:param h:
:param w:
:return:
'''
tB = np.zeros([h, w], dtype=np.complex)
tB[:kernel_size, :kernel_size] = B
# Cyclic shift, so that the Gaussian core is located at the four corners
tB = np.roll(np.roll(tB, -kernel_size // 2, axis=0), -kernel_size // 2, axis=1)
return tB
def interplotedPointsToPSF(psf, bands, msbands):
'''
Cubic spline interpolation to obtain the required R (in the case of insufficient points)
:param psf:
:param bands:
:param msbands:
:return:
'''
psf_L = psf.shape[0]
x = np.linspace(0, psf_L, psf_L)
# print(x)
xx = np.linspace(0, psf_L, bands)
R = np.zeros([msbands, bands], dtype=np.float32)
for i in range(msbands):
# R[i, :] = make_interp_spline(x, psf[:, i + 1], xx)
f = interp1d(x, psf[:, i + 1], kind='cubic')
R[i, :] = f(xx)
return sumtoOne(R)
def IKONOS_PSF():
'''
this is an example
using IKONOS to get R of Pavia university
:return:
'''
spf = sio.loadmat(r"f:/Fairy/hehe/ikonos_spec_resp.mat")['ikonos_sp']
r, c = spf.shape
count = 0
for i in range(r):
if spf[i, 0] >= 430 and spf[i, 0] <= 860:
count += 1
if count == 1:
valid_spf = spf[i, :]
else:
valid_spf = np.vstack((valid_spf, spf[i, :]))
if spf[i, 0] > 860:
break
# print(valid_spf)
no_wa = valid_spf.shape[0]
print(no_wa)
# x = np.array([i for i in range(0, no_wa)])
x = np.linspace(0, no_wa, no_wa)
# print(x)
xx = np.linspace(0, no_wa, 103)
L = 103
R = np.zeros([5, 103], dtype=np.float32)
for i in range(5):
# R[i, :] = spline(x, valid_spf[:, i + 1], xx)
f = interp1d(x, valid_spf[:, i + 1], kind='cubic')
# print(f)
R[i, :] = f(xx)
plt.plot(xx, R[2, :], color='b', label='b')
plt.plot(xx, R[3, :], color='g', label='g')
plt.plot(xx, R[4, :], color='r', label='r')
plt.plot(x, valid_spf[:, 5], color='c', label='c')
plt.plot(x, valid_spf[:, 4], color='m', label='m')
plt.plot(x, valid_spf[:, 3], color='y', label='y')
plt.legend()
plt.show()
def calculateEpoch():
'''
calculate the epochs of TONWMD on CAVE data
:return:
'''
start_lr = 2e-3
end_lr = 1e-7
lr = start_lr
batch_size = 64
total_num = 15376
epochs = 0
current_epoch = 0
step = 0
epochs_decay = 10
while lr > end_lr:
step += 1
if batch_size * step >= total_num:
current_epoch += 1
epochs += 1
if current_epoch >= epochs_decay:
current_epoch = 0
lr *= 0.5
return epochs
def standard(X):
'''
Standardization
universal
:param X:
:return:
'''
X = (X - np.min(X)) / (np.max(X) - np.min(X))
return np.float32(X)
def checkFile(path):
'''
if filepath not exist make it
:param path:
:return:
'''
if not os.path.exists(path):
os.makedirs(path)
def standard_by_norminf(X):
'''
Standardized by channel
:param X:
:return:
'''
_, _, c = X.shape
for i in range(c):
X[:, :, i] = standard(X[:, :, i])
return np.float32(X)
def roundNum(X):
'''
rounding
:param X:
:return:
'''
return int(X + 0.5)
if __name__ == '__main__':
pass