-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
247 lines (200 loc) · 6.75 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
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
import numpy as np
import cv2
import os
import math
import scipy.io as sio
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
class AverageMeter(object):
def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
def ReadImg(filename):
img = cv2.imread(filename)
img = img[:,:,::-1] / 255.0
img = np.array(img).astype('float32')
return img
def hwc_to_chw(img):
return np.transpose(img, axes=[2, 0, 1])
def chw_to_hwc(img):
return np.transpose(img, axes=[1, 2, 0])
####################################################
#################### noise model ###################
####################################################
def func(x, a):
return np.power(x, a)
def CRF_curve_fit(I, B):
popt, pcov = curve_fit(func, I, B)
return popt
def CRF_function_transfer(x, y):
para = []
for crf in range(201):
temp_x = np.array(x[crf, :])
temp_y = np.array(y[crf, :])
para.append(CRF_curve_fit(temp_x, temp_y))
return para
def mosaic_bayer(rgb, pattern, noiselevel):
w, h, c = rgb.shape
if pattern == 1:
num = [1, 2, 0, 1]
elif pattern == 2:
num = [1, 0, 2, 1]
elif pattern == 3:
num = [2, 1, 1, 0]
elif pattern == 4:
num = [0, 1, 1, 2]
elif pattern == 5:
return rgb
mosaic = np.zeros((w, h, 3))
mask = np.zeros((w, h, 3))
B = np.zeros((w, h))
B[0:w:2, 0:h:2] = rgb[0:w:2, 0:h:2, num[0]]
B[0:w:2, 1:h:2] = rgb[0:w:2, 1:h:2, num[1]]
B[1:w:2, 0:h:2] = rgb[1:w:2, 0:h:2, num[2]]
B[1:w:2, 1:h:2] = rgb[1:w:2, 1:h:2, num[3]]
gauss = np.random.normal(0, noiselevel/255.,(w, h))
gauss = gauss.reshape(w, h)
B = B + gauss
return (B, mask, mosaic)
def ICRF_Map(Img, I, B):
w, h, c = Img.shape
output_Img = Img.copy()
prebin = I.shape[0]
tiny_bin = 9.7656e-04
min_tiny_bin = 0.0039
for i in range(w):
for j in range(h):
for k in range(c):
temp = output_Img[i, j, k]
start_bin = 0
if temp > min_tiny_bin:
start_bin = math.floor(temp/tiny_bin - 1) - 1
for b in range(start_bin, prebin):
tempB = B[b]
if tempB >= temp:
index = b
if index > 0:
comp1 = tempB - temp
comp2 = temp - B[index-1]
if comp2 < comp1:
index = index - 1
output_Img[i, j, k] = I[index]
break
return output_Img
def CRF_Map(Img, I, B):
w, h, c = Img.shape
output_Img = Img.copy()
prebin = I.shape[0]
tiny_bin = 9.7656e-04
min_tiny_bin = 0.0039
for i in range(w):
for j in range(h):
for k in range(c):
temp = output_Img[i, j, k]
if temp < 0:
temp = 0
Img[i, j, k] = 0
elif temp > 1:
temp = 1
Img[i, j, k] = 1
start_bin = 0
if temp > min_tiny_bin:
start_bin = math.floor(temp/tiny_bin - 1) - 1
for b in range(start_bin, prebin):
tempB = I[b]
if tempB >= temp:
index = b
if index > 0:
comp1 = tempB - temp
comp2 = temp - B[index-1]
if comp2 < comp1:
index = index - 1
output_Img[i, j, k] = B[index]
break
return output_Img
def CRF_Map_opt(Img, popt):
w, h, c = Img.shape
output_Img = Img.copy()
output_Img = func(output_Img, *popt)
return output_Img
def Demosaic(B_b, pattern):
B_b = B_b * 255
B_b = B_b.astype(np.uint16)
if pattern == 1:
lin_rgb = cv2.demosaicing(B_b, cv2.COLOR_BayerGB2BGR)
elif pattern == 2:
lin_rgb = cv2.demosaicing(B_b, cv2.COLOR_BayerGR2BGR)
elif pattern == 3:
lin_rgb = cv2.demosaicing(B_b, cv2.COLOR_BayerBG2BGR)
elif pattern == 4:
lin_rgb = cv2.demosaicing(B_b, cv2.COLOR_BayerRG2BGR)
elif pattern == 5:
lin_rgb = B_b
lin_rgb = lin_rgb[:,:,::-1] / 255.
return lin_rgb
def AddNoiseMosai(x, CRF_para, iCRF_para, I, B, Iinv, Binv, sigma_s, sigma_c, crf_index, pattern, opt = 1):
w, h, c = x.shape
temp_x = CRF_Map_opt(x, iCRF_para[crf_index] )
sigma_s = np.reshape(sigma_s, (1, 1, c))
noise_s_map = np.multiply(sigma_s, temp_x)
noise_s = np.random.randn(w, h, c) * noise_s_map
temp_x_n = temp_x + noise_s
noise_c = np.zeros((w, h, c))
for chn in range(3):
noise_c [:, :, chn] = np.random.normal(0, sigma_c[chn], (w, h))
temp_x_n = temp_x_n + noise_c
temp_x_n = np.clip(temp_x_n, 0.0, 1.0)
temp_x_n = CRF_Map_opt(temp_x_n, CRF_para[crf_index])
if opt == 1:
temp_x = CRF_Map_opt(temp_x, CRF_para[crf_index])
B_b_n = mosaic_bayer(temp_x_n[:,:,::-1], pattern, 0)[0]
lin_rgb_n = Demosaic(B_b_n, pattern)
result = lin_rgb_n
if opt == 1:
B_b = mosaic_bayer(temp_x[:,:,::-1], pattern, 0)[0]
lin_rgb = Demosaic(B_b, pattern)
diff = lin_rgb_n - lin_rgb
result = x + diff
return result
def AddRealNoise(image, CRF_para, iCRF_para, I_gl, B_gl, I_inv_gl, B_inv_gl):
sigma_s = np.random.uniform(0.0, 0.16, (3,))
sigma_c = np.random.uniform(0.0, 0.06, (3,))
CRF_index = np.random.choice(201)
pattern = np.random.choice(4) + 1
noise_img = AddNoiseMosai(image, CRF_para, iCRF_para, I_gl, B_gl, I_inv_gl, B_inv_gl, sigma_s, sigma_c, CRF_index, pattern, 0)
noise_level = sigma_s * np.power(image, 0.5) + sigma_c
return noise_img, noise_level
def Illumination(L,method):
if method == "max_c":
return np.max(L,axis=2)
elif method == "min_c":
return np.min(L,axis=2)
else:
print("输入模式有误请输入max_c or min_c")
def guideFilter(I, p, winSize, eps):
mean_I = cv2.blur(I, winSize)
mean_p = cv2.blur(p, winSize)
mean_II = cv2.blur(I * I, winSize)
mean_Ip = cv2.blur(I * p, winSize)
var_I = mean_II - mean_I * mean_I
cov_Ip = mean_Ip - mean_I * mean_p
a = cov_Ip / (var_I + eps)
b = mean_p - a * mean_I
mean_a = cv2.blur(a, winSize)
mean_b = cv2.blur(b, winSize)
q = mean_a * I + mean_b
return q
def Adapt_gamma(img,x):
mean = np.mean(img)
gamma = x*min(math.log10(0.5)/math.log10(mean),1)
out = img**gamma
return out