-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathPRIMOR_CT.m
403 lines (344 loc) · 13.7 KB
/
PRIMOR_CT.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
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
function [u,errAll] = PRIMOR_CT(TParameters,G,f,R,N,uref,mu,lambda,gamma,alpha,beta,nBreg,varargin)
% u = PRIMOR_CT(TParameters,G,f,R,N,uref,mu,lambda,gamma,alpha,beta,nBreg)
% [u,errAll] = PRIMOR_CT(TParameters,G,f,R,N,uref,mu,lambda,gamma,alpha,beta,nBreg,varargin)
%
% Inputs:
%
% TParameters = result from the registration between gates (see
% ComputeSplineRegTwoDirectionsPoolDouble.m). It is a structure with fields
% TParameters.GridAll, TParameters.SpacingAll, TParameters.GridDAll,
% TParameters.SpacingDAll
% G = Projection operator where G' is the backprojection operator
% f = data, N x numPro x time
% R = undersampling pattern, same size as f. Entries are 1 when
% data is sampled and zero otherwise
% N = image size n_x x n_y x n_t (number of pixels in spatial and
% temporal dimensions)
% uref = prior image (usually the FBP of averaged data), size can be
% n_x x n_y x n_t or n_x x n_y (the latter will be replicated across time)
% mu = 1, weight of data constraint. Increase for faster
% convergence, decrease for noisy data (1 usually works fine)
% lambda = 1
% beta = 0.2, TV sparsity parameter
% alpha = 0.4, prior functional sparsity parameter,
% increase for imposing similarity to the prior image
% nBreg = Bregman iterations. This is the most critical parameter,
% chose regarding the noise in the data (lower number of iterations for
% noisier data). It can be selected by checking the convergence (errAll)
% varargin = {uTarget} = Gold standard to computer the error and assess
% convergence
%
% Outputs:
%
% u = reconstructed image of size n_x x n_y x n_t
% errAll = relative solution error norm at each iteration, size nBreg x
% number of gates
%
% Prior- and motion-based reconstruction (PRIMOR) method efficiently
% solved using the Split Bregman formulation. It assumes that a good
% quality (free of artefacts) prior image is available and that there is an
% estimate of the image registration between consecutive gates, which can
% be obtained from a previous reconstruction. PRIMOR provides additionally
% a-priori information with respect to previous methods methods, which can
% be seen by computing abs(OperatorL(uTarget)) and abs(uTarget(:,:,1)-uTarget(:,:,1))
% and abs(uTarget(:,:,1)-uref(:,:,1)). The linear system in the image
% reconstruciton step is solved using Gauss-Newton Krylov method (see the
% Krylov tolerance threshold below).
%
%
% Requirements:
%
% To run PRIMOR download the IRT code and FFD-based registration software.
% This demo makes use of parallel computation using parfor loops across
% different gates, but this is not a requirement (if you do not have the
% parallel computing toolbox, change parfor loops by for loops).
%
% % IRT package: Simulated data and forward and backprojection operators have
% been computed using IRT code (J A Fessler, Image reconstruction toolbox
% [IRT], 2011, retrieved from
% <http://www.eecs.umich.edu/~fessler/code/index.html>).
% Execute setup.m to add all directories to the path and execute
% mex_build.m to compile mex files for your system.
% Bear in mind that these forward and backprojection operations are the
% most computationally expensive parts of the algorithm, so providing
% faster implementations will speed up the algorithm considerably.
%
% Wavelab 850: Package for wavelet transform computation (Buckheit JB, Chen
% S, Donoho DL, Johnstone IM, Scargle JD. WaveLab. Reference Manual.
% ftp://playfair.stanford.edu/pub/wavelab/WaveLabRef.ps. 1995., retrieved
% from http://statweb.stanford.edu/~wavelab/). Execute startup.m to ad
% paths and compile mex files.
%
% FFD-based registration package: For the motion estimation step we used
% the FFD-based registration package available in MATLAB Central (Dirk-Jan
% Kroon; B-spline grid, image and point based registration; 2012,
% retrieved from
% http://www.mathworks.com/matlabcentral/fileexchange/20057-b-spline-grid-image-and-point-based-registration),
% Add all directories to the path and run compile_c_files.m for faster
% implementation.
%
%
% Faster implementations:
%
% Change forward operator, G, and backprojection
% operator, G', with your own GPU implementations to make the algorithm
% up to orders of magnitude faster.
%
% If you use this code, please reference the paper JFPJ Abascal et al. A
% novel prior- and motion-based compressed sensing method for small-animal
% respiratory gated CT. Plos One, 2016 (in press).
%
% Juan FPJ Abascal, Monica Abella
% Departamento de Bioingenieria e Ingenieria Aeroespacial
% Universidad Carlos III de Madrid, Madrid, Spain
h = figure;
hw = waitbar(0);
tolKrylov = 1e-2; % Krylov convergence criterion, the smaller the value
% the higher the precission for solving the linear system
% Wavelab WT parameters
L = 3;
qmf = MakeONFilter('Symmlet',8);
NPadMax = 2^ceil(log(N(1))/log(2));
NPad = round((NPadMax-N(1))/2);
indPad = NPad+1:NPadMax-NPad;
dimIm = N;
rows = N(1);
cols = N(2);
numTime = N(3);
Nf = size(f);
if size(uref,3) <= N(3)
uref = repmat(uref,[1 1 N(3)]);
end
R = reshape(R,Nf);
f = f.*R;
% Normalize data
normFactor = getNormalizationFactor(f,f);
f = normFactor*f;
uref = double(uref)*normFactor;
if nargin >= 13
uTarget = varargin{1};
errAll = zeros(nBreg,N(3));
uTarget = double(uTarget)*normFactor;
end % nargin
% Reserve memory for the auxillary variables
f0 = f;
v = zeros(rows,cols,numTime);
u = zeros(rows,cols,numTime);
x = zeros(rows,cols,numTime);
y = zeros(rows,cols,numTime);
bx = zeros(rows,cols,numTime);
by = zeros(rows,cols,numTime);
dx = zeros(rows,cols,numTime);
dy = zeros(rows,cols,numTime);
p = zeros(rows,cols,numTime);
bp = zeros(rows,cols,numTime);
vWT = zeros(NPadMax,NPadMax,numTime);
rhs_wt = zeros(rows,cols,numTime);
dv = zeros(NPadMax,NPadMax,numTime);
bv = zeros(NPadMax,NPadMax,numTime);
murf = zeros(rows,cols,numTime);
FFuref = zeros(rows,cols,numTime);
% Forward and backward spline-based transformations
% Forward (Grid,Spacing) must be the transformation of moving frame i to
% match the next frame, and backward (GridD,SpacingD) transform frame i to
% match the frame i-1
GridAll = TParameters.GridAll;
SpacingAll = TParameters.SpacingAll;
GridDAll = TParameters.GridDAll;
SpacingDAll = TParameters.SpacingDAll;
dpref = OperatorL(uref);
dxref = Dx(uref);
dyref = Dy(uref);
% Backprojection
parfor ip = 1:numTime
murf(:,:,ip) = double(mu*(G'*f(:,:,ip)));
FFuref(:,:,ip) = mu*(G'*(R(:,:,ip).*(G*uref(:,:,ip))));
end
% Do the reconstruction
for outer = 1:nBreg;
figure(hw); waitbar(outer/nBreg);
parfor iw = 1:numTime
rhs_wt_temp = IWT2_PO(dv(:,:,iw)-bv(:,:,iw),L,qmf);
rhs_wt(:,:,iw) = rhs_wt_temp(indPad,indPad);
end % iw
rhs_wt = lambda*rhs_wt;
rhs_p = lambda*OperatorLt(p-dpref-bp);
rhs_tv = lambda*(Dxt(x-dxref-bx)+Dyt(y-dyref-by));
rhs = murf-FFuref+rhs_wt+rhs_p+rhs_tv;
v = reshape(krylov(rhs(:)),N);
u = uref + v;
% Derivatives
dx = Dx(u);
dy = Dy(u);
dp = OperatorL(u);
% WT of image difference
parfor iw = 1:numTime
vWT(:,:,iw) = FWT2_PO(padarray(v(:,:,iw),[NPad NPad]),L,qmf);
end % iw
% update x, y, p
[x,y] = shrink2(dx+bx, dy+by,alpha/lambda);
dv = shrink1(vWT+bv, gamma/lambda);
p = shrink1(dp+bp, beta/lambda);
% update bregman parameters
bv = bv+vWT-dv;
bx = bx+dx-x;
by = by+dy-y;
bp = bp+dp-p;
% Bregman iteration for the data constraint
parfor iw = 1:numTime
fForw = double(G*u(:,:,iw)).*R(:,:,iw);
f(:,:,iw) = f(:,:,iw) + f0(:,:,iw)-fForw;
murf(:,:,iw) = double(mu*(G'*f(:,:,iw)));
end
% Solution error norm
if nargin >= 13
for iw = 1:numTime
errAll(outer,iw) = norm(reshape(uTarget(:,:,iw)-u(:,:,iw),[],1))...
/norm(reshape(uTarget(:,:,iw),[],1));
end
end
if any([outer ==1, outer == 5, rem(outer,10)==0])
% Display image and auxiliary variables for TV and prior terms. The
% number of nonzero coefficients on the respective transformed
% domains are given as a precentage
subplot(2,2,1);
imagesc(u(:,:,1)/(normFactor)); title(['Iter ' num2str(outer)]); colorbar; axis image;
subplot(2,2,2);
imagesc(x(:,:,1)); axis image; title(['xw, sparsity % ' num2str(100*nnz(x(:,:,1))/(prod(N(1:2))))]);
subplot(2,2,3);
tmp = dv(:,:,1);
tmp = IWT2_PO(tmp,L,qmf);
tmp = tmp(indPad,indPad);
imagesc(tmp); colorbar; axis image; title(['w, sparsity % ' num2str(100*nnz(dv(:,:,1))/(NPadMax*NPadMax))]);
if nargin >= 13
subplot(2,2,4); plot(errAll(1:outer,:)); axis tight; title(['Sol. error' ]);
end
colormap gray;
drawnow;
end % rem
end % outer
close(hw);
% undo the normalization so that results are scaled properly
u = u/normFactor;
% =====================================================================
function uL = OperatorL(uThis)
% Apply the transform operator Lu_ijk=u_ijk-u_i'j'k-1 that
% associates a pixel ij in the frame k with a unique pixel (using
% Nearest Neighbour interpolation) in the frame k, the
% interpolation matrix is obtained from the spline-registratio
% toolbox.
% NN(:,:,1) transforms a pixel ij in frame 1 to another i'j' in
% frame 2
%
% This is as Tu_k = u_k-T_{k-1}(u_{k-1})
% Each frame is substracted the forward tranformtation of the
% previous frame
uTAll = InterpolateBasedSplines(uThis,GridAll,SpacingAll);
uL = zeros(dimIm);
% Compare to previous frame
for it = 1:dimIm(3)
if it == 1
LuThis = uTAll(:,:,end);
else
LuThis = uTAll(:,:,it-1);
end
uL(:,:,it) = uThis(:,:,it)-LuThis;
end
end
function uLt = OperatorLt(uThis)
% % Apply the transpose of the transform operator
% % Lu_ijk=u_ijk-u_i'j'k-1
%
% This is as Ttu_k = u_k-Du_k+1
% Each frame is substracted the backward tranformtation of the next
% frame
uTAll = InterpolateBasedSplines(uThis,GridDAll,SpacingDAll);
uLt = zeros(dimIm);
% Compare to previous frame
for it = 1:dimIm(3)
if it == dimIm(3)
LuThis = uTAll(:,:,1);
else
LuThis = uTAll(:,:,it+1);
end
uLt(:,:,it) = uThis(:,:,it)-LuThis;
end
end
function uTAll = InterpolateBasedSplines(uThis,GridAll,SpacingAll)
% Transform each image corresponding to each frame, following the
% spline-based transformation given by GridAll and SpacingAll. This
% should transform each frame to be similar to the next one
uTAll = zeros(dimIm);
for ih = 1:numTime
% Tranform u:
IT = uThis(:,:,ih);
uTAll(:,:,ih) = bspline_transform(GridAll(:,:,:,ih),IT,SpacingAll(:,:,ih));
end % ih
end
function normFactor = getNormalizationFactor(R,f)
normFactor = 1/norm(f(:)/size(R==1,1));
end
function d = Dx(u)
[rows,cols,height] = size(u);
d = zeros(rows,cols,height);
d(:,2:cols,:) = u(:,2:cols,:)-u(:,1:cols-1,:);
d(:,1,:) = u(:,1,:)-u(:,cols,:);
end
function d = Dxt(u)
[rows,cols,height] = size(u);
d = zeros(rows,cols,height);
d(:,1:cols-1,:) = u(:,1:cols-1,:)-u(:,2:cols,:);
d(:,cols,:) = u(:,cols,:)-u(:,1,:);
end
function d = Dy(u)
[rows,cols,height] = size(u);
d = zeros(rows,cols,height);
d(2:rows,:,:) = u(2:rows,:,:)-u(1:rows-1,:,:);
d(1,:,:) = u(1,:,:)-u(rows,:,:);
end
function d = Dyt(u)
[rows,cols,height] = size(u);
d = zeros(rows,cols,height);
d(1:rows-1,:,:) = u(1:rows-1,:,:)-u(2:rows,:,:);
d(rows,:,:) = u(rows,:,:)-u(1,:,:);
end
function [xs,ys] = shrink2(x,y,lambda)
s = sqrt(x.*conj(x)+y.*conj(y));
ss = s-lambda;
ss = ss.*(ss>0);
s = s+(s<lambda);
ss = ss./s;
xs = ss.*x;
ys = ss.*y;
end
function xs = shrink1(x,lambda)
s = abs(x);
xs = sign(x).*max(s-lambda,0);
end
% Krylov solver subroutine to solve the linear system
% X = GMRES(A,B,RESTART,TOL,MAXIT,M)
% bicgstab(A,b,tol,maxit)
function dx = krylov(r)
%dx = gmres (@jtjx, r, 30, tolKrylov, 100);
[dx,flag] = bicgstab(@jtjx, r, tolKrylov, 100);
end
% Callback function for matrix-vector product (called by krylov)
function b = jtjx(sol)
solMat = reshape(sol,N);
% TV part
btv = lambda*(Dyt(Dy(solMat)) + Dxt(Dx(solMat)));
% Temporal operator part
bP = lambda*OperatorLt(OperatorL(solMat));
% Data constraint part
bF = zeros(N);
parfor iq = 1:N(3)
tmp = R(:,:,iq).*(G*solMat(:,:,iq));
bF(:,:,iq) = mu*(double(G'*tmp));
end
% Prior part
bwt = lambda*sol;
b = bwt(:) + btv(:) + bP(:) + bF(:);
end
end
%