forked from bbncWLG/LightField-deconvolution
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deconv_phase_space.m
152 lines (142 loc) · 5.72 KB
/
deconv_phase_space.m
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
function Output=deconv_phase_space(LF)
% Deconvolution the light field data in phase space
%% Input:
% @LF: input single snapshot image by Light field camera
%
% Ouput: the super resolution image by deconvolution
%
% The Code is created based on the method described in the following paper
% ZHI LU, JIAMIN WU, HUI QIAO and YOU ZHOU .etc,
% Phase-space deconvolution for light field microscopy.
% Author: ZHI LU ([email protected])
% Date : 05/21/2019
%% maximum iteration
maxIter=1;
%% Pre-processing
load('PSF/LF_psf.mat');
Nnum=size(H,3);
% Realignment H to phase_space PSF
IMGsize=size(H,1)-mod((size(H,1)-Nnum),2*Nnum);
psf =zeros(IMGsize,IMGsize,Nnum,Nnum,size(H,5));
for z=1:size(H,5)
LFtmp=zeros(IMGsize,IMGsize,Nnum,Nnum);
index1=round(size(H,1)/2)-fix(size(LFtmp,1)/2);
index2=round(size(H,1)/2)+fix(size(LFtmp,1)/2);
for ii=1:size(H,3)
for jj=1:size(H,4)
LFtmp(:,:,ii,jj)=im_shift3(squeeze(H(index1:index2,index1:index2,ii,jj,z)),ii-((Nnum+1)/2), jj-(Nnum+1)/2);
end
end
multiWDF=zeros(Nnum,Nnum,size(LFtmp,1)/size(H,3),size(LFtmp,2)/size(H,4),Nnum,Nnum);
for i=1:size(H,3)
for j=1:size(H,4)
for a=1:size(LFtmp,1)/size(H,3)
for b=1:size(LFtmp,2)/size(H,4)
multiWDF(i,j,a,b,:,:)=squeeze( LFtmp( (a-1)*Nnum+i,(b-1)*Nnum+j,:,: ) );
end
end
end
end
WDF=zeros( size(LFtmp,1),size(LFtmp,2),Nnum,Nnum );
for a=1:size(LFtmp,1)/size(H,3)
for c=1:Nnum
x=Nnum*a+1-c;
for b=1:size(LFtmp,2)/size(H,4)
for d=1:Nnum
y=Nnum*b+1-d;
WDF(x,y,:,:)=squeeze(multiWDF(:,:,a,b,c,d));
end
end
end
end
psf(:,:,:,:,z)=WDF;
end
psf_t=zeros(size(psf));
for ii=1:Nnum
for jj=1:Nnum
for cc=1:size(psf,5)
psf_t(:,:,ii,jj,cc)=rot90(squeeze(psf(:,:,ii,jj,cc)),2 );
end
end
end
% Realignment the LF image into phase space
LR_phase_space=zeros(size(LF,1)/Nnum,size(LF,2)/Nnum,Nnum,Nnum);
for i=1:Nnum
for j=1:Nnum
LR_phase_space(:,:,i,j)=LF(i:Nnum:end,j:Nnum:end);
end
end
phase_space = zeros( size(LF,1),size(LF,2),Nnum,Nnum );
for i = 1:Nnum
for j = 1:Nnum
phase_space(:,:,i,j) = imresize(LR_phase_space(:,:,i,j),[size(LF,1),size(LF,2)],'cubic');
end
end
phase_space(phase_space<0)=0;
% Filter
x=-fix(size(phase_space,1)/2):fix(size(phase_space,1)/2);
[xx,yy]=meshgrid(x,x);
cut_off_freq=10;
sub_aperture_filter=zeros(size(phase_space,1),size(phase_space,2));
sub_aperture_filter(xx.^2+yy.^2 <= cut_off_freq^2)=1;
for i=1:Nnum
for j=1:Nnum
phase_space_f_with_filter=fftshift(fft2(squeeze(phase_space(:,:,i,j)))).*sub_aperture_filter;
phase_space_with_filter(:,:,i,j)=abs( ifft2(ifftshift(phase_space_f_with_filter)) );
end
end
phase_space=phase_space_with_filter;
phase_space(phase_space<0)=0;
% Weights for every iteration
weight=squeeze(sum(sum(sum(psf,1),2),5));
weight(find(isnan(weight))) = 0;
weight=weight./sum(weight(:));
weight=weight-min(weight(:));
weight(weight<0.0035)=0;
weight=weight.*80;
%% Deconvolution
% Initialization
Xguess=ones(size(LF,1),size(LF,2),size(psf,5));
Xguess=Xguess./sum(Xguess(:)).*sum(LF(:));
Htf=zeros( size(LF,1),size(LF,2) , size(psf,5) , Nnum ,Nnum );
for u=1:Nnum
for v=1:Nnum
if weight(u,v)==0
continue;
else
Htf(:,:,:,u,v)= backwardProject_phase_space(squeeze(psf_t(:,:,u,v,:)), ones( size(LF) ) );
end
end
end
% Ptychographic order
index1=[1,1,1,1,1,1,1,1,1,1,1,1,1,2,3,4,5,6,7,8,9,10,11,12,13,13,13,13,13,13,13,13,13,13,13,13,13,12,11,10,9,8,7,6,5,4,3,2,2,2,2,2,2,2,2,2,2,2,2,3,4,5,6,7,8,9,10,11,12,12,12,12,12,12,12,12,12,12,12,11,10,9,8,7,6,5,4,3,3,3,3,3,3,3,3,3,3,4,5,6,7,8,9,10,11,11,11,11,11,11,11,11,11,10,9,8,7,6,5,4,4,4,4,4,4,4,4,5,6,7,8,9,10,10,10,10,10,10,10,9,8,7,6,5,5,5,5,5,5,6,7,8,9,9,9,9,9,8,7,6,6,6,6,7,8,8,8,7,7];
index2=[1,2,3,4,5,6,7,8,9,10,11,12,13,13,13,13,13,13,13,13,13,13,13,13,13,12,11,10,9,8,7,6,5,4,3,2,1,1,1,1,1,1,1,1,1,1,1,1,2,3,4,5,6,7,8,9,10,11,12,12,12,12,12,12,12,12,12,12,12,11,10,9,8,7,6,5,4,3,2,2,2,2,2,2,2,2,2,2,3,4,5,6,7,8,9,10,11,11,11,11,11,11,11,11,11,10,9,8,7,6,5,4,3,3,3,3,3,3,3,3,4,5,6,7,8,9,10,10,10,10,10,10,10,9,8,7,6,5,4,4,4,4,4,4,5,6,7,8,9,9,9,9,9,8,7,6,5,5,5,5,6,7,8,8,8,7,6,6,7];
% iterative ptychographic volume update
for i=1:maxIter
tic;
for u_2=1:13
for v_2=1:13
u=index1((u_2-1)*13+v_2);
v=index2((u_2-1)*13+v_2);
if weight(u,v)==0
continue;
else
XguessBefore=Xguess;
HXguess=forwardProject_phase_space(squeeze(psf(:,:,u,v,:)), Xguess);
errorEM=squeeze(phase_space(:,:,u,v))./HXguess;
errorEM(~isfinite(errorEM))=0;
XguessCor = backwardProject_phase_space(squeeze(psf_t(:,:,u,v,:)),errorEM) ;
Xguess_add=XguessBefore.*XguessCor./squeeze(Htf(:,:,:,u,v));
Xguess_add(find(isnan(Xguess_add))) = 0;
Xguess_add(find(isinf(Xguess_add))) = 0;
Xguess_add(Xguess_add<0 ) = 0;
Xguess=Xguess_add.*weight(u,v)+(1-weight(u,v)).*XguessBefore;
Xguess(Xguess<0) = 0;
end
end
end
ttime1=toc;
disp(['iter ',num2str(i),' | ',num2str(maxIter),', phase-space deconvolution took ',num2str(ttime1),' secs']);
end
%% Post-processing
Output=Xguess(Nnum*2+1:end-Nnum*2,Nnum*2+1:end-Nnum*2,6:end-5);