/
TVL2.py
199 lines (149 loc) · 4.98 KB
/
TVL2.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
import numpy as np
import matplotlib.pyplot as plt
def div(cx,cy):
#cy and cy are coordonates of a vector field.
#the function computes the discrete divergence of this vector field
nr,nc=cx.shape
ddx=np.zeros((nr,nc))
ddy=np.zeros((nr,nc))
ddx[:,1:-1]=cx[:,1:-1]-cx[:,0:-2]
ddx[:,0]=cx[:,0]
ddx[:,-1]=-cx[:,-2]
ddy[1:-1,:]=cy[1:-1,:]-cy[0:-2,:]
ddy[0,:]=cy[0,:]
ddy[-1,:]=-cy[-2,:]
d=ddx+ddy
return d
def grad(im):
#computes the gradient of the image 'im'
# image size
nr,nc=im.shape
gx = im[:,1:]-im[:,0:-1]
gx = np.block([gx,np.zeros((nr,1))])
gy =im[1:,:]-im[0:-1,:]
gy=np.block([[gy],[np.zeros((1,nc))]])
return gx,gy
def chambolle_pock_prox_TV(ub,lambd,niter):
# the function solves the problem
# argmin_u 1/2 \| u - ub\|^2 + \lambda TV(u)
# with TV(u) = \sum_i \|\nabla u (i) \|_2
# uses niter iterations of Chambolle-Pock
nr,nc = ub.shape
ut = np.copy(ub)
ubar = np.copy(ut)
p = np.zeros((nr,nc,2))
tau = 0.9/np.sqrt(8*lambd**2)
sigma = 0.9/np.sqrt(8*lambd**2)
theta = 1
for k in range(niter):
# calcul de proxF
ux,uy = grad(ubar)
p = p + sigma*lambd*np.stack((ux,uy),axis=2)
normep = np.sqrt(p[:,:,0]**2+p[:,:,1]**2)
normep = normep*(normep>1) + (normep<=1)
p[:,:,0] = p[:,:,0]/normep
p[:,:,1] = p[:,:,1]/normep
# calcul de proxG
d=div(p[:,:,0],p[:,:,1])
#TVL2
unew = 1/(1+tau)*(ut+tau*lambd*d+tau*ub)
#extragradient step
ubar = unew+theta*(unew-ut)
ut = unew
return ut
def chambolle_pock_prox_TV1(ub,lambd,niter):
# the function solves the problem
# argmin_u 1/2 \| u - ub\|^2 + \lambda TV(u)
# with TV(u) = \sum_i \|\nabla u (i) \|_1
# uses niter iterations of Chambolle-Pock
nr,nc = ub.shape
ut = np.copy(ub)
ubar = np.copy(ut)
p = np.zeros((nr,nc,2))
tau = 0.9/np.sqrt(8*lambd**2)
sigma = 0.9/np.sqrt(8*lambd**2)
theta = 1
for k in range(niter):
# calcul de proxF
ux,uy = grad(ubar)
p = p + sigma*lambd*np.stack((ux,uy),axis=2)
normep = np.abs(p[:,:,0])+np.abs(p[:,:,1])
normep = normep*(normep>1) + (normep<=1)
p[:,:,0] = p[:,:,0]/normep
p[:,:,1] = p[:,:,1]/normep
# calcul de proxG
d=div(p[:,:,0],p[:,:,1])
#TVL2
unew = 1/(1+tau)*(ut+tau*lambd*d+tau*ub)
#extragradient step
ubar = unew+theta*(unew-ut)
ut = unew
return ut
def convol(a,b):
return np.real(np.fft.ifft2(np.fft.fft2(a)*np.fft.fft2(b)))
def IdplustauATA_inv(x,tau,h):
return np.real(np.fft.ifft2(np.fft.fft2(x)/(1+tau*np.abs(np.fft.fft2(h))**2)))
def chambolle_pock_deblurring_TV(ub,h,lambd,niter):
# the function solves the problem
# argmin_u 1/2 \| Au - ub\|^2 + \lambda TV(u)
# with TV(u) = \sum_i \|\nabla u (i) \|_2
# and A = blur given by a kernel h
# uses niter iterations of Chambolle-Pock
nr,nc = ub.shape
ut = np.copy(ub)
p = np.zeros((nr,nc,2))
tau = 0.9/np.sqrt(8*lambd**2)
sigma = 0.9/np.sqrt(8*lambd**2)
theta = 1
ubar = np.copy(ut)
h_fft = np.fft.fft2(h)
hc_fft = np.conj(h_fft)
hc = np.fft.ifft2(hc_fft)
for k in range(niter):
# subgradient step on p
ux,uy = grad(ubar)
p = p + sigma*lambd*np.stack((ux,uy),axis=2)
normep = np.sqrt(p[:,:,0]**2+p[:,:,1]**2)
normep = normep*(normep>1) + (normep<=1)
p[:,:,0] = p[:,:,0]/normep
p[:,:,1] = p[:,:,1]/normep
# subgradient step on u
d=div(p[:,:,0],p[:,:,1])
unew = (ut+tau*lambd*d+tau*convol(ub, hc))
unew = IdplustauATA_inv(unew, tau,h)
#extragradient step on u
ubar = unew+theta*(unew-ut)
ut = unew
return ut
###################
## interpolation
##################
def chambolle_pock_interpolation_TV2(ub,mask,lambd,niter):
# the function solves the problem
# argmin_u 1/2 \| Au - ub\|^2 + \lambda TV(u)
# with TV(u) = \sum_i \|\nabla u (i) \|_2
# and A = diagonal matrix represented by the mask
# uses niter iterations of Chambolle-Pock
nr,nc = ub.shape
ut = np.copy(ub)
p = np.zeros((nr,nc,2))
tau = 0.9/np.sqrt(8*lambd**2)
sigma = 0.9/np.sqrt(8*lambd**2)
theta = 1
ubar = np.copy(ut)
for k in range(niter):
# subgradient step on p
ux,uy = grad(ubar)
p = p + sigma*lambd*np.stack((ux,uy),axis=2)
normep = np.sqrt(p[:,:,0]**2+p[:,:,1]**2)
normep = normep*(normep>1) + (normep<=1)
p[:,:,0] = p[:,:,0]/normep
p[:,:,1] = p[:,:,1]/normep
# subgradient step on u
d=div(p[:,:,0],p[:,:,1])
unew = (ut+tau*lambd*d+tau*mask*ub)
unew = unew/(1+tau*mask)
#extragradient step on u
ubar = unew+theta*(unew-ut)
ut = unew
return ut