-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
210 lines (159 loc) · 5.31 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
# Autores: Manya V Afonso, original MATLAB version by: Manya V Afonso, José M Bioucas Dias, Mário A T Figueiredo
# Instituto de Telecomunicações, Instituto Superior Técnico, Lisboa, Portugal
import numpy as np
import pywt
def myNorm(x):
return np.sqrt( (x**2).sum() )
def myNorm_l1(x):
return np.sum( np.abs(x) )
def addGaussianNoise(x, SNRdB):
Ps = (x**2).sum()/x.size
sigma = np.sqrt( ((x-x.mean())**2).sum()/( x.size*(10**(SNRdB/10)) ) )
y = x + sigma * np.random.normal(size=x.shape)
return y
def myISNR(x0,y,x):
ISNR = 10*np.log10( ((x0-y)**2).sum()/((x0-x)**2).sum() )
return ISNR
def dnorm(x, mu, sd):
return 1 / (np.sqrt(2 * np.pi) * sd) * np.e ** (-np.power((x - mu) / sd, 2) / 2)
def gaussian_kernel(size, sigma=1, verbose=False):
kernel_1D = np.linspace(-(size // 2), size // 2, size)
for i in range(size):
kernel_1D[i] = dnorm(kernel_1D[i], 0, sigma)
kernel_1D = kernel_1D/kernel_1D.sum()
kernel_2D = np.outer(kernel_1D.T, kernel_1D.T)
kernel_2D *= 1.0 / kernel_2D.max()
if verbose:
plt.imshow(kernel_2D, interpolation='none',cmap='gray')
plt.title("Kernel")
plt.show()
return kernel_2D
def hard(x,T):
return np.multiply(x, np.abs(x)>=T )
def soft(x,T):
if np.sum(np.abs(T)) ==0:
y = x
else:
z = np.abs(x) - T
y = np.multiply( z, z>0 )
y = np.multiply( np.divide( y, (y+T) ) , x)
return (y)
def TVdiffs(u):
tmp = np.row_stack( (u[-1,:],u[:-1,:]) )
dux = u - tmp
tmp = np.column_stack( (u[:,-1],u[:,:-1]) )
duy = u - tmp
return dux, duy
def myNeighbors(u,i,j):
nb = np.array([ u[i-1,j], u[i,j-1], u[i+1,j], u[i,j+1] ])
return nb
def myNeighborsTensor(u):
tmp1 = np.row_stack( (u[-1,:],u[:-1,:]) )
tmp2 = np.column_stack( (u[:,-1],u[:,:-1]) )
tmp3 = np.row_stack( (u[1:,:],u[0,:]) )
tmp4 = np.column_stack( (u[:,1:],u[:,0]) )
nbMat = np.array( (tmp1, tmp2, tmp3, tmp4) )
return nbMat
def DivergenceIm(p1,p2):
z = p2[:,1:-1] - p2[:,:-2]
v = np.column_stack((p2[:,0],z,-p2[:,-1]))
z = p1[1:-1,:] - p1[:-2,:]
u = np.row_stack((p1[0,:],z,-p1[-1,:]))
divp = v + u
return divp
def GradientIm(u):
z = u[1:, :] - u[:-1,:]
dux = np.row_stack((z,np.zeros(z.shape[1])))
z = u[:, 1:] - u[:,:-1]
duy = np.column_stack((z,np.zeros(z.shape[0])))
return dux, duy
def chambolle_prox_TV_stop(g,alpha,MaxIter=10,tol=1e-2,px=np.array([0]),py=np.array([0])):
if (px.shape != g.shape)or(py.shape != g.shape):
px=np.zeros(g.shape)
py=np.zeros(g.shape)
tau = 0.249
cont = True
k = 0
while cont:
k = k + 1
divp = DivergenceIm(px,py)
u = divp - g/alpha
upx,upy = GradientIm(u)
tmp = np.sqrt(upx**2 + upy**2)
err = np.sqrt( ((tmp*px-upx)**2 + (tmp*py-upy)**2).sum() )
px = (px + tau * upx)/(1 + tau * tmp)
py = (py + tau * upy)/(1 + tau * tmp)
cont = (k<MaxIter) and (err>tol)
f = g - alpha*DivergenceIm(px,py)
return f,px,py
def denoiseTV(g,alpha,MaxIter=10,tol=1e-2,px=np.array([0]),py=np.array([0])):
f,_,_ = chambolle_prox_TV_stop(g,alpha,MaxIter,tol,px,py)
return f
def TVnorm(x):
dh, dv = GradientIm(x)
J = (np.sqrt(dh**2 + dv**2)).sum()
return J
def TVnorm_ani(x):
dh, dv = GradientIm(x)
J = (np.sqrt(dh**2 + dv**2)).sum()
return J
def cshift(x,L):
N = len(x)
y = np.zeros(x.shape)
if (L == 0):
return x
if (L>0):
y[L:] = x[:N-L]
y[:L] = x[N-L:N]
else:
L = -L
y[:N-L] = x[L:]
y[N-L:N] = x[:L]
return y
# transformada de wavelet, para trabalhar com matrizes.
def wrapper_swt2(img, wavelet_str, num_levels):
coeffs = pywt.swt2(img, wavelet_str, num_levels)
list_mat = []
for ind_level in range(0,num_levels):
list_mat.append( coeffs[ind_level][0] )
for ch in range(0,3):
list_mat.append( coeffs[ind_level][1][ch] )
X = np.hstack([ list_mat ] )
X = X.reshape(X.shape[0]*X.shape[1],X.shape[2])
return X
def wrapper_iswt2(X, wavelet_str, num_levels):
rows = int( X.shape[0]/(4*num_levels) )
cols = X.shape[1]
coeffs = [ () for _ in range(num_levels) ]
for level in range(0, num_levels):
cl = X[ 4*rows*level:4*rows*(level+1), : ]
current_tuple = ( cl[:rows,:], ( cl[rows:2*rows,:], cl[2*rows:3*rows,:], cl[3*rows:4*rows,:] ) )
coeffs[level] = current_tuple
y = pywt.iswt2(coeffs, wavelet_str)
return y
def cshift(x,L):
N = len(x)
y = np.zeros(x.shape)
if (L == 0):
return x
if (L>0):
y[L:] = x[:N-L]
y[:L] = x[N-L:N]#(N-L+1:N);
else:
L = -L
y[:N-L] = x[L:]
y[N-L:N] = x[:L]
return y
def make_blur_kernel(kernel_length, img_size, blur_type=0):
h_1d = np.zeros(img_size,)
if blur_type == 0:
# uniforme
h_1d[:kernel_length] = 1
else:
# gaussiana
h_1d[:kernel_length] = np.divide( np.ones(kernel_length,),\
(np.array( range(0,kernel_length) )-((kernel_length-1)/2.0) )**2+1 )
h_1d = h_1d/h_1d.sum()
h_1d = cshift(h_1d,int(-(kernel_length-1)/2) )
h_blur = np.outer(h_1d.transpose(), h_1d)
return h_blur