-
Notifications
You must be signed in to change notification settings - Fork 3
/
Chapter_10_Deblurring.asv
501 lines (444 loc) · 15.2 KB
/
Chapter_10_Deblurring.asv
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
% Figures - 10.8,
% =========================================
% This script runs the deblurring experiment. The details of the
% experiment are the following:
% 1. Measurement created by blurring 'cameraman' with 15*15
% blur kernel, 1/(1+i^2+j^2), and additive w.g.n. with
% sigma^2 = 2 or 8. This gives y.
% 2. We aim to deblur by minimizing 0.5||HAa-y||^2+lambda*rho(a)
% where lambda = 0.075,
% rho - the L1 function
% H - the blur matrix
% A - Unecimated Haar with 3 levels of resolution, 7:1
% redundancy The code here BUILDS A explicitly - this
% is not necesary in general, but we do so here for clarity
% 3. Methods used for minimizing this function are
% - SSF (plain)
% - SSF + Line Search (Newton algorithm)
% - PCD + Line Search (Newton algorithm)
% - SSF + SESOP-5 (Newton algorithm)
% - PCD + SESOP-5 (Newton algorithm)
% 4. Outputs we produce:
% - The function value f(a) as a function of the computations
% - The ISNR as a function of the computations
% - Output images with the PCD (10 iterations) for sigma=2 and
% sigma=8
% =========================================
% clear all; close all;
function []=Chapter_10_Deblurring()
%================================================
% O r g a n i z i n g t h e D a t a
%================================================
% --------------------------------- Part 1 - creation of data ------------------------------------
load Chapter_10_DataCameraman;
Haar=Generate_Haar_Matrix(256);
[n,m]=size(Haar);
Hy0=BlurOp(y0(:),blur);
noise=randn(n,1);
sigma=sqrt(2); % noise power
y=Hy0+sigma*noise; % add noise
% ------------------------------- Part 2 - various Preparations --------------------------------
% computing c for SSF (supposed to be 1)
% Note: A includes the dictionary and the blur together
iter=100;
temp=randn(m,1);
for k=1:1:iter,
temp=temp/norm(temp);
temp=BlurOp(Haar*temp,blur);
temp=Haar'*BlurOp(temp,blur);
disp([k,norm(temp)]);
end;
c=norm(temp);
c=1; % so the code above is not needed
% computing diag(A'*A) for PCD
% Note: A includes the dictionary and the blur together
iter=1000;
n=256^2; m=256^2*7;
ww=zeros(m,1);
h=waitbar(0,'Random calculations ...');
for k=1:1:iter,
waitbar(k/iter);
temp=randn(n,1);
temp=Haar'*BlurOp(temp,blur);
ww=ww+temp.^2;
figure(1); clf;
plot(sqrt(ww/k));
axis([1 m 0 0.2]); hold on;
drawnow;
end;
close(h);
W=ww/iter;
IW=1./W;
lambda = 0.075;
Iter = 100; % number of iterations in each of the algorithms
s = 0.01; % smoothing parameter, used within PhiFunc as well
LSiter = 10; % Number of iterations of the line-search
% Initalize the output will be organized into this structure: one only once
Results=struct('Name',[],'Function_Value',[],'ISNR',[]);
%================================================
% A L G O R I T H M S S I M U L A T I O N S
%================================================
% ------------------------------- Part 1 - Run plain SSF ----------------------------------------
x=zeros(m,1);
Ax=BlurOp(Haar*x,blur);
[res1,res2,res3]=RhoFunc(x,s);
f(1)=0.5*sum((Ax-y).^2)+lambda*res1;
ISNR(1)=10*log10((sum((y-y0(:)).^2)/sum((y0(:)-Haar*x).^2)));
fprintf('Iteration: %i : Value: %f ISNR: %f\n',0,f(1),ISNR(1));
for k=1:1:Iter,
err=y-Ax;
ATerr=Haar'*BlurOp(err,blur);
temp=(1/c)*ATerr+x;
x=Analytic_Shrinkage(temp,lambda/c,s);
Ax=BlurOp(Haar*x,blur);
[res1,res2,res3]=RhoFunc(x,s);
f(k+1)=0.5*sum((Ax-y).^2)+lambda*res1;
ISNR(k+1)=10*log10((sum((y-y0(:)).^2)/sum((y0(:)-Haar*x).^2)));
fprintf('Iteration: %i : Value: %f ISNR: %f Residual: %f\n'...
,k,f(k+1),ISNR(k+1),sqrt(mean((Ax-y).^2)));
end;
Results(1).Name='SSF';
Results(1).Function_Value=f;
Results(1).ISNR=ISNR;
% -------------------------- Part 2 - Run SSF + LS ----------------------------------
x=zeros(m,1);
Ax=BlurOp(Haar*x,blur);
[res1,res2,res3]=RhoFunc(x,s);
f(1)=0.5*sum((Ax-y).^2)+lambda*res1;
ISNR(1)=10*log10((sum((y-y0(:)).^2)/sum((y0(:)-Haar*x).^2)));
fprintf('Iteration: %i : Value: %f ISNR: %f\n',0,f(1),ISNR(1));
for k=1:1:Iter,
err=y-Ax;
ATerr=Haar'*BlurOp(err,blur);
temp=(1/c)*ATerr+x;
temp=Analytic_Shrinkage(temp,lambda/c,s);
Atemp=BlurOp(Haar*temp,blur);
[x,mu]=LineSearch(x,temp-x,err,Atemp-Ax,LSiter,s,lambda);
disp(mu);
Ax=(1-mu)*Ax+mu*Atemp; % instead of Ax=BlurOp(Haar*x,blur);
[res1,res2,res3]=RhoFunc(x,s);
f(k+1)=0.5*sum((Ax-y).^2)+lambda*res1;
ISNR(k+1)=10*log10((sum((y-y0(:)).^2)/sum((y0(:)-Haar*x).^2)));
fprintf('Iteration: %i : Value: %f ISNR: %f Residual: %f\n'...
,k,f(k+1),ISNR(k+1),sqrt(mean((Ax-y).^2)));
end;
Results(2).Name='SSF-LS';
Results(2).Function_Value=f;
Results(2).ISNR=ISNR;
% -------------------------- Part 3 - Run PCD ----------------------------------------
x=zeros(m,1);
Ax=BlurOp(Haar*x,blur);
[res1,res2,res3]=RhoFunc(x,s);
f(1)=0.5*sum((Ax-y).^2)+lambda*res1;
ISNR(1)=10*log10((sum((y-y0(:)).^2)/sum((y0(:)-Haar*x).^2)));
fprintf('Iteration: %i : Value: %f ISNR: %f\n',0,f(1),ISNR(1));
for k=1:1:Iter,
err=y-Ax;
ATerr=Haar'*BlurOp(err,blur);
temp=IW.*ATerr+x;
temp=Analytic_Shrinkage(temp,lambda*IW,s);
Atemp=BlurOp(Haar*temp,blur);
[x,mu]=LineSearch(x,temp-x,err,Atemp-Ax,LSiter,s,lambda);
Ax=(1-mu)*Ax+mu*Atemp; % instead of Ax=BlurOp(Haar*x,blur);
[res1,res2,res3]=RhoFunc(x,s);
f(k+1)=0.5*sum((Ax-y).^2)+lambda*res1;
ISNR(k+1)=10*log10((sum((y-y0(:)).^2)/sum((y0(:)-Haar*x).^2)));
fprintf('Iteration: %i : Value: %f ISNR: %f Residual: %f\n'...
,k,f(k+1),ISNR(k+1),sqrt(mean((Ax-y).^2)));
end;
Results(3).Name='PCD-LS';
Results(3).Function_Value=f;
Results(3).ISNR=ISNR;
% ----------------------- Part 4 - Run SSF + SESOP-5 ---------------------------
x=zeros(m,1);
Ax=BlurOp(Haar*x,blur);
[res1,res2,res3]=RhoFunc(x,s);
f(1)=0.5*sum((Ax-y).^2)+lambda*res1;
ISNR(1)=10*log10((sum((y-y0(:)).^2)/sum((y0(:)-Haar*x).^2)));
fprintf('Iteration: %i : Value: %f ISNR: %f\n',0,f(1),ISNR(1));
M=5; % order of the SESOP
SubS=zeros(m,M);
ASubS=zeros(n,M);
for k=1:1:Iter,
err=y-Ax;
ATerr=Haar'*BlurOp(err,blur);
temp=(1/c)*ATerr+x;
temp=Analytic_Shrinkage(temp,lambda/c,s);
SubS=[SubS(:,2:M),temp-x];
Atemp=BlurOp(Haar*temp,blur);
ASubS=[ASubS(:,2:M),Atemp-Ax];
[x,mu]=LineSearch(x,SubS,err,ASubS,LSiter,s,lambda);
disp(mu');
Ax=Ax+ASubS*mu;
[res1,res2,res3]=RhoFunc(x,s);
f(k+1)=0.5*sum((Ax-y).^2)+lambda*res1;
ISNR(k+1)=10*log10((sum((y-y0(:)).^2)/sum((y0(:)-Haar*x).^2)));
fprintf('Iteration: %i : Value: %f ISNR: %f Residual: %f\n'...
,k,f(k+1),ISNR(k+1),sqrt(mean((Ax-y).^2)));
end;
Results(4).Name='SSF-SESOP-5';
Results(4).Function_Value=f;
Results(4).ISNR=ISNR;
% ----------------------- Part 5 - Run PCD + SESOP-5 ---------------------------
x=zeros(m,1);
Ax=BlurOp(Haar*x,blur);
[res1,res2,res3]=RhoFunc(x,s);
f(1)=0.5*sum((Ax-y).^2)+lambda*res1;
ISNR(1)=10*log10((sum((y-y0(:)).^2)/sum((y0(:)-Haar*x).^2)));
fprintf('Iteration: %i : Value: %f ISNR: %f\n',0,f(1),ISNR(1));
M=5; % order of the SESOP
SubS=zeros(m,M);
ASubS=zeros(n,M);
for k=1:1:Iter,
err=y-Ax;
ATerr=Haar'*BlurOp(err,blur);
temp=IW.*ATerr+x;
temp=Analytic_Shrinkage(temp,lambda*IW,s);
SubS=[SubS(:,2:M),temp-x];
Atemp=BlurOp(Haar*temp,blur);
ASubS=[ASubS(:,2:M),Atemp-Ax];
[x,mu]=LineSearch(x,SubS,err,ASubS,LSiter,s,lambda);
disp(mu');
Ax=Ax+ASubS*mu;
[res1,res2,res3]=RhoFunc(x,s);
f(k+1)=0.5*sum((Ax-y).^2)+lambda*res1;
ISNR(k+1)=10*log10((sum((y-y0(:)).^2)/sum((y0(:)-Haar*x).^2)));
fprintf('Iteration: %i : Value: %f ISNR: %f Residual: %f\n'...
,k,f(k+1),ISNR(k+1),sqrt(mean((Ax-y).^2)));
end;
Results(5).Name='PCD-SESOP-5';
Results(5).Function_Value=f;
Results(5).ISNR=ISNR;
save Chapter_10_Deblurring_Results.mat
%==============================================%
% C R E A T I O N O F O U T P U T %
%==============================================%
% Show the blur
figure(1); clf;
subplot(1,2,1); h=mesh(blur);
set(h,'LineWidth',2);
set(gca,'Fontsize',14);
subplot(1,2,2);
imagesc(blur);
axis image
colormap(gray(256));
colorbar;
set(gca,'Fontsize',14);
% print -depsc2 Chapter_10_Blur.eps
% Show the function's value for the iterated shrinkage algorithms
MIN=min([Results(1).Function_Value, Results(2).Function_Value,...
Results(3).Function_Value,Results(4).Function_Value,...
Results(5).Function_Value])-0.0001;
figure(1); clf;
h=semilogy(0:1:100,Results(1).Function_Value-MIN,'k');
set(h,'LineWidth',2);
hold on;
h=semilogy(0:1:100,Results(2).Function_Value-MIN,'k--');
set(h,'LineWidth',2);
h=semilogy(0:1:100,Results(4).Function_Value-MIN,'k-.');
set(h,'LineWidth',2);
axis([0 100 1e2 1e7]);
h=xlabel('Iteration');
set(h,'Fontsize',14);
h=ylabel('f(x_k)-f_{min}');
set(h,'Fontsize',14);
legend({Results(1).Name,Results(2).Name,Results(4).Name});
set(gca,'Fontsize',14);
% print -depsc2 Chapter_10_Func1.eps
figure(2); clf;
h=semilogy(0:1:100,Results(3).Function_Value-MIN,'k');
set(h,'LineWidth',2);
hold on;
h=semilogy(0:1:100,Results(5).Function_Value-MIN,'k--');
set(h,'LineWidth',2);
h=semilogy(0:1:100,Results(1).Function_Value-MIN,'k:');
set(h,'LineWidth',2);
legend({Results(3).Name,Results(5).Name,'SSF results'});
h=semilogy(0:1:100,Results(2).Function_Value-MIN,'k:');
set(h,'LineWidth',2);
h=semilogy(0:1:100,Results(4).Function_Value-MIN,'k:');
set(h,'LineWidth',2);
axis([0 100 1e2 1e7]);
h=xlabel('Iteration');
set(h,'Fontsize',14);
h=ylabel('f(x_k)-f_{min}');
set(h,'Fontsize',14);
set(gca,'Fontsize',14);
% print -depsc2 Chapter_10_Func2.eps
% Show the ISNR of the Iterated-shrinkage algorithms
figure(1); clf;
h=plot(0:1:100,Results(1).ISNR,'k');
set(h,'LineWidth',2);
hold on;
h=semilogy(0:1:100,Results(2).ISNR,'k--');
set(h,'LineWidth',2);
h=semilogy(0:1:100,Results(4).ISNR,'k-.');
set(h,'LineWidth',2);
axis([0 100 0 7.5]);
h=xlabel('Iteration');
set(h,'Fontsize',14);
h=ylabel('ISNR(x_k)');
set(h,'Fontsize',14);
legend({Results(1).Name,Results(2).Name,Results(4).Name},4);
set(gca,'Fontsize',14);
% print -depsc2 Chapter_10_ISNR1.eps
figure(2); clf;
h=plot(0:1:100,Results(3).ISNR,'k');
set(h,'LineWidth',2);
hold on;
h=plot(0:1:100,Results(5).ISNR,'k--');
set(h,'LineWidth',2);
h=plot(0:1:100,Results(1).ISNR,'k:');
set(h,'LineWidth',2);
legend({Results(3).Name,Results(5).Name,'SSF results'},4);
h=semilogy(0:1:100,Results(2).ISNR,'k:');
set(h,'LineWidth',2);
h=semilogy(0:1:100,Results(4).ISNR,'k:');
set(h,'LineWidth',2);
axis([0 100 0 7.5]);
h=xlabel('Iteration');
set(h,'Fontsize',14);
h=ylabel('ISNR(x_k)');
set(h,'Fontsize',14);
set(gca,'Fontsize',14);
% print -depsc2 Chapter_10_ISNR2.eps
% Show output images for sigma^2=2
Hy0=BlurOp(y0(:),blur);
noise=randn(n,1);
sigma=sqrt(2);
y=Hy0+sigma*noise;
lambda = 0.075;
Iter=30;
x=zeros(m,1);
Ax=BlurOp(Haar*x,blur);
for k=1:1:Iter,
err=y-Ax;
ATerr=Haar'*BlurOp(err,blur);
temp=IW.*ATerr+x;
temp=Analytic_Shrinkage(temp,lambda*IW,s);
Atemp=BlurOp(Haar*temp,blur);
[x,mu]=LineSearch(x,temp-x,err,Atemp-Ax,LSiter,s,lambda);
Ax=(1-mu)*Ax+mu*Atemp; % instead of Ax=BlurOp(Haar*x,blur);
[res1,res2,res3]=RhoFunc(x,s);
disp(10*log10((sum((y-y0(:)).^2)/sum((y0(:)-Haar*x).^2))));
end;
figure(1); clf; imagesc(reshape(y0,[256,256]));
axis image; axis off; colormap(gray(256));
% print -depsc2 Chapter_10_Image_sigma_2_original.eps
figure(2); clf; imagesc(reshape(y,[256,256]));
axis image; axis off; colormap(gray(256));
% print -depsc2 Chapter_10_Image_sigma_2_noisy.eps
figure(3); clf; imagesc(reshape(Haar*x,[256,256]));
axis image; axis off; colormap(gray(256));
% print -depsc2 Chapter_10_Image_sigma_2_restored.eps
figure(4);
h=semilogy(sort(abs(x),'descend'),'k');
set(h,'LineWidth',2);
h=xlabel('index');
set(h,'Fontsize',14);
h=ylabel('Sorted absolute values');
set(h,'Fontsize',14);
set(gca,'Fontsize',14);
grid on;
axis([1 256^2*7 1e-10 1e3]);
% print -depsc2 Chapter_10_IsItSparse.eps
% Show output images for sigma^2=8
Hy0=BlurOp(y0(:),blur);
noise=randn(n,1);
sigma=sqrt(8);
y=Hy0+sigma*noise;
lambda = 0.15;
Iter=30;
x=zeros(m,1);
Ax=BlurOp(Haar*x,blur);
for k=1:1:Iter,
err=y-Ax;
ATerr=Haar'*BlurOp(err,blur);
temp=IW.*ATerr+x;
temp=Analytic_Shrinkage(temp,lambda*IW,s);
Atemp=BlurOp(Haar*temp,blur);
[x,mu]=LineSearch(x,temp-x,err,Atemp-Ax,LSiter,s,lambda);
Ax=(1-mu)*Ax+mu*Atemp; % instead of Ax=BlurOp(Haar*x,blur);
[res1,res2,res3]=RhoFunc(x,s);
disp(10*log10((sum((y-y0(:)).^2)/sum((y0(:)-Haar*x).^2))));
end;
figure(1); clf; imagesc(reshape(y0,[256,256]));
axis image; axis off; colormap(gray(256));
% print -depsc2 Chapter_10_Image_sigma_8_original.eps
figure(2); clf; imagesc(reshape(y,[256,256]));
axis image; axis off; colormap(gray(256));
print -depsc2 Chapter_10_Image_sigma_8_noisy.eps
figure(3); clf; imagesc(reshape(Haar*x,[256,256]));
axis image; axis off; colormap(gray(256));
% print -depsc2 Chapter_10_Image_sigma_8_restored.eps
return;
%================================================
%================================================
function [Haar]=Generate_Haar_Matrix(n)
D1=sparse(n,n);
v=sparse([1 zeros(1,n-2), -1]/2);
for k=1:1:n
D1(k,:)=v;
v=[v(end),v(1:end-1)];
end;
D2=sparse(n,n);
v=[1 1 zeros(1,n-4), -1 -1]/4;
for k=1:1:n
D2(k,:)=v;
v=[v(end),v(1:end-1)];
end;
S1=abs(D1);
S2=abs(D2);
Haar=[kron(S2,S2),kron(S2,D2),kron(D2,S2),kron(D2,D2),...
kron(S1,D1),kron(D1,S1),kron(D1,D1)];
return;
%================================================
%================================================
function [y]=BlurOp(x,blur)
% This function performs the blur operation on an input image x, assuming
% cyclic boundary condition
n=length(x);
x=reshape(x,[sqrt(n),sqrt(n)]);
d=size(blur,1);
d=(d-1)/2;
xe=[x(:,end-d+1:end), x, x(:,1:1:d)];
xe=[xe(end-d+1:end,:); xe; xe(1:1:d,:)];
y=conv2(xe,blur,'valid');
y=y(:);
return;
%================================================
%================================================
function [obj,grad,hess]=RhoFunc(x,s)
% This function returns the value of rho(x), its gradient and its
% Hessian. The function rho(x) is a smoothed version of L1
ax=abs(x);
obj=sum(ax - s*log(1+ax/s));
grad=x./(s+ax);
hess=s./(s+ax).^2;
return;
%================================================
%================================================
function Out=Analytic_Shrinkage(In,lambda,s)
% This function applies the LUT that minimizes the function
% g(x)=0.5*(x-In)^2+lambda*rho(x)
InM=abs(In);
Temp=InM-lambda-s;
Out=(Temp+sqrt(Temp.^2+4*s*InM))/2;
Out=sign(In).*Out;
return;
%================================================
%================================================
function [x,mu] = LineSearch(x,dd,err,Add,iter,s,lambda)
% In this function we search mu that minimizes the function
% g(mu)=0.5*||ee+mu*vv||^2+rho(x+mu*dd). Once mu is found,
% the output is aa+mu*dd
S=size(dd,2);
mu=[zeros(S-1,1); 1];
for k=1:1:iter,
[res1,res2,res3]=RhoFunc(x+dd*mu,s);
grad=Add'*(Add*mu-err)+lambda*dd'*res2;
hess=Add'*Add+lambda*dd'*spdiags(res3,0,length(res3),length(res3))*dd;
mu=mu-pinv(hess)*grad;
end;
x=x+dd*mu;
return;