-
Notifications
You must be signed in to change notification settings - Fork 41
/
utils.py
218 lines (173 loc) · 5.46 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
import cv2
import numpy as np
import rawpy
import matplotlib.pyplot as plt
import imageio
def extract_bayer_channels(raw):
ch_B = raw[1::2, 1::2]
ch_Gb = raw[0::2, 1::2]
ch_R = raw[0::2, 0::2]
ch_Gr = raw[1::2, 0::2]
return ch_R, ch_Gr, ch_B, ch_Gb
def load_rawpy (raw_file):
'''
Load RAW images in .dng format using rawpy
'''
raw = rawpy.imread(raw_file)
raw_image = raw.raw_image
return raw_image
def load_img (filename, debug=False, norm=True, resize=None):
'''
Load RGB image
'''
img = cv2.imread(filename)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
if norm:
img = img / 255.
img = img.astype(np.float32)
if debug:
print (img.shape, img.dtype, img.min(), img.max())
if resize:
img = cv2.resize(img, (resize[0], resize[1]), interpolation = cv2.INTER_AREA)
return img
def save_rgb (img, filename):
'''Save RGB image <img> as 8bit 3-channel using the provided <filename>'''
if np.max(img) <= 1:
img = img * 255
img = img.astype(np.float32)
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
cv2.imwrite(filename, img)
def load_raw_png(raw, debug=False):
'''
Load RAW images from the ZurichRAW2RGB Dataset
Reference: https://github.com/aiff22/PyNET-PyTorch/blob/master/dng_to_png.py
by Andrey Ignatov.
inputs:
- raw: filename to the raw image saved as '.png'
returns:
- RAW_norm: normalized float32 4-channel raw image with bayer pattern RGGB.
'''
assert '.png' in raw
raw = np.asarray(imageio.imread((raw)))
ch_R, ch_Gr, ch_B, ch_Gb = extract_bayer_channels (raw)
RAW_combined = np.dstack((ch_R, ch_Gr, ch_Gb, ch_B))
RAW_norm = RAW_combined.astype(np.float32) / (4 * 255)
RAW_norm = np.clip(RAW_norm, 0, 1)
if debug:
print (RAW_norm.shape, RAW_norm.dtype, RAW_norm.min(), RAW_norm.max())
# raw as (h,w,1) in RGBG domain! do not use
raw_unpack = raw.astype(np.float32) / (4 * 255)
raw_unpack = np.expand_dims(raw_unpack, axis=-1)
return RAW_norm
def load_raw(raw_path, max_val=2**10):
'''
Loads RAW images saved as '.npy' files and type np.uint16
'''
raw = np.load (raw_path)/ max_val
raw = np.clip(raw, 0., 1.)
return raw.astype(np.float32)
########## RAW image manipulation
def unpack_raw(im):
"""
Unpack RAW image from (h,w,4) to (h*2 , w*2, 1)
"""
h,w,chan = im.shape
H, W = h*2, w*2
img2 = np.zeros((H,W))
img2[0:H:2,0:W:2]=im[:,:,0]
img2[0:H:2,1:W:2]=im[:,:,1]
img2[1:H:2,0:W:2]=im[:,:,2]
img2[1:H:2,1:W:2]=im[:,:,3]
img2 = np.squeeze(img2)
img2 = np.expand_dims(img2, axis=-1)
return img2
def pack_raw(im):
"""
Pack RAW image from (h,w,1) to (h/2 , w/2, 4)
"""
img_shape = im.shape
H = img_shape[0]
W = img_shape[1]
## R G G B
out = np.concatenate((im[0:H:2,0:W:2,:],
im[0:H:2,1:W:2,:],
im[1:H:2,0:W:2,:],
im[1:H:2,1:W:2,:]), axis=2)
return out
########## VISUALIZATION
def demosaic (raw):
"""Simple demosaicing to visualize RAW images
Inputs:
- raw: (h,w,4) RAW RGGB image normalized [0..1] as float32
Returns:
- Simple Avg. Green Demosaiced RAW image with shape (h*2, w*2, 3)
"""
assert raw.shape[-1] == 4
shape = raw.shape
red = raw[:,:,0]
green_red = raw[:,:,1]
green_blue = raw[:,:,2]
blue = raw[:,:,3]
avg_green = (green_red + green_blue) / 2
image = np.stack((red, avg_green, blue), axis=-1)
image = cv2.resize(image, (shape[1]*2, shape[0]*2))
return image
def mosaic(rgb):
"""Extracts RGGB Bayer planes from an RGB image."""
assert rgb.shape[-1] == 3
shape = rgb.shape
red = rgb[0::2, 0::2, 0]
green_red = rgb[0::2, 1::2, 1]
green_blue = rgb[1::2, 0::2, 1]
blue = rgb[1::2, 1::2, 2]
image = np.stack((red, green_red, green_blue, blue), axis=-1)
return image
def gamma_compression(image):
"""Converts from linear to gamma space."""
return np.maximum(image, 1e-8) ** (1.0 / 2.2)
def tonemap(image):
"""Simple S-curved global tonemap"""
return (3*(image**2)) - (2*(image**3))
def postprocess_raw(raw):
"""Simple post-processing to visualize demosaic RAW imgaes
Input: (h,w,3) RAW image normalized
Output: (h,w,3) post-processed RAW image
"""
raw = gamma_compression(raw)
raw = tonemap(raw)
raw = np.clip(raw, 0, 1)
return raw
def plot_pair (img1, img2, t1='RGB', t2='RAW', axis='off'):
'''
Plot pair of images
'''
fig = plt.figure(figsize=(12, 6), dpi=80)
plt.subplot(1,2,1)
plt.title(t1)
plt.axis(axis)
plt.imshow(img1)
plt.subplot(1,2,2)
plt.title(t2)
plt.axis(axis)
plt.imshow(img2)
plt.show()
def plot_all (images, figsize=(12, 6), axis='off', titles=None):
'''
Plots in a row the list of "images" provided.
'''
fig = plt.figure(figsize=figsize, dpi=80)
nplots = len(images)
for i in range(nplots):
plt.subplot(1,nplots,i+1)
plt.axis(axis)
plt.imshow(images[i])
if titles: plt.title(titles[i])
plt.show()
########## METRICS
def PSNR(y_true, y_pred):
mse = np.mean((y_true - y_pred) ** 2)
if(mse == 0):
return np.inf
max_pixel = np.max(y_true)
psnr = 20 * np.log10(max_pixel / np.sqrt(mse))
return psnr