-
Notifications
You must be signed in to change notification settings - Fork 13
/
utils.py
126 lines (115 loc) · 3.95 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
''' Utilities '''
import math
import numpy as np
import scipy.io as sio
import cv2
def TV_denoiser(x, _lambda, n_iter_max):
dt = 0.25
N = x.shape
idx = np.arange(1,N[0]+1)
idx[-1] = N[0]-1
iux = np.arange(-1,N[0]-1)
iux[0] = 0
ir = np.arange(1,N[1]+1)
ir[-1] = N[1]-1
il = np.arange(-1,N[1]-1)
il[0] = 0
p1 = np.zeros_like(x)
p2 = np.zeros_like(x)
divp = np.zeros_like(x)
for i in range(n_iter_max):
z = divp - x*_lambda
z1 = z[:,ir,:] - z
z2 = z[idx,:,:] - z
denom_2d = 1 + dt*np.sqrt(np.sum(z1**2 + z2**2, 2))
denom_3d = np.tile(denom_2d[:,:,np.newaxis], (1,1,N[2]))
p1 = (p1+dt*z1)/denom_3d
p2 = (p2+dt*z2)/denom_3d
divp = p1-p1[:,il,:] + p2 - p2[iux,:,:]
u = x - divp/_lambda;
return u
def A(x, Phi):
'''
Forward model of snapshot compressive imaging (SCI), where multiple coded
frames are collapsed into a snapshot measurement.
'''
return np.sum(x*Phi, axis=2) # element-wise product
def At(y, Phi):
'''
Tanspose of the forward model.
'''
# (nrow, ncol, nmask) = Phi.shape
# x = np.zeros((nrow, ncol, nmask))
# for nt in range(nmask):
# x[:,:,nt] = np.multiply(y, Phi[:,:,nt])
# return x
return np.multiply(np.repeat(y[:,:,np.newaxis],Phi.shape[2],axis=2), Phi)
def psnr(ref, img):
'''
Peak signal-to-noise ratio (PSNR).
'''
mse = np.mean( (ref - img) ** 2 )
if mse == 0:
return 100
PIXEL_MAX = 1.
#PIXEL_MAX = ref.max()
return 20 * math.log10(PIXEL_MAX / math.sqrt(mse))
def shift_back(inputs,step):
[row,col,nC] = inputs.shape
for i in range(nC):
inputs[:,:,i] = np.roll(inputs[:,:,i],(-1)*step*i,axis=1)
output = inputs[:,0:col-step*(nC-1),:]
return output
def shift(inputs,step):
[row,col,nC] = inputs.shape
output = np.zeros((row, col+(nC-1)*step, nC))
for i in range(nC):
output[:,i*step:i*step+row,i] = inputs[:,:,i]
return output
def calculate_ssim(img1, img2, border=0):
'''calculate SSIM
the same outputs as MATLAB's
img1, img2: [0, 255]
'''
# img1 = img1.squeeze()
# img2 = img2.squeeze()
if not img1.shape == img2.shape:
raise ValueError('Input images must have the same dimensions.')
h, w = img1.shape[:2]
img1 = img1[border:h - border, border:w - border]
img2 = img2[border:h - border, border:w - border]
if img1.ndim == 2:
return ssim(img1, img2)
elif img1.ndim == 3:
if img1.shape[2] == 3:
ssims = []
for i in range(3):
ssims.append(ssim(img1[:, :, i], img2[:, :, i]))
return np.array(ssims).mean()
elif img1.shape[2] == 1:
return ssim(np.squeeze(img1), np.squeeze(img2))
else:
ssims = []
for i in range(img1.shape[2]):
ssims.append(ssim(img1[:, :, i], img2[:, :, i]))
return np.array(ssims).mean()
else:
raise ValueError('Wrong input image dimensions.')
def ssim(img1, img2):
C1 = (0.01 * 1) ** 2
C2 = (0.03 * 1) ** 2
img1 = img1.astype(np.float64)
img2 = img2.astype(np.float64)
kernel = cv2.getGaussianKernel(11, 1.5)
window = np.outer(kernel, kernel.transpose())
mu1 = cv2.filter2D(img1, -1, window)[5:-5, 5:-5] # valid
mu2 = cv2.filter2D(img2, -1, window)[5:-5, 5:-5]
mu1_sq = mu1 ** 2
mu2_sq = mu2 ** 2
mu1_mu2 = mu1 * mu2
sigma1_sq = cv2.filter2D(img1 ** 2, -1, window)[5:-5, 5:-5] - mu1_sq
sigma2_sq = cv2.filter2D(img2 ** 2, -1, window)[5:-5, 5:-5] - mu2_sq
sigma12 = cv2.filter2D(img1 * img2, -1, window)[5:-5, 5:-5] - mu1_mu2
ssim_map = ((2 * mu1_mu2 + C1) * (2 * sigma12 + C2)) / ((mu1_sq + mu2_sq + C1) *
(sigma1_sq + sigma2_sq + C2))
return ssim_map.mean()