-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmakeFigures.m
More file actions
295 lines (235 loc) · 9.14 KB
/
Copy pathmakeFigures.m
File metadata and controls
295 lines (235 loc) · 9.14 KB
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
function [] = makeFigures()
close all; clear
runPCandLADMM = 1;
plotLearningCurves = 1;
runFilteredBackProjection = 1;
runBlurryPhantom = 0;
runBlurryPhantomMetrics = 0;
runAnimation = 0;
% datacase = 'lena';
datacase = 'phantom';
%% PC, LADMM, and GD results
% no translation, with translation, with translation and rotation
close all;
% Reconstruction parameters
noiseLevel = 1e-3;
cy = 0; nRows=64;
cx = 0; nCols=64;
pixSize = 0.001; % meters / pixel
switch datacase
case 'phantom'
img = phantom();
case 'lena'
img = double( imread( 'lena.png' ) );
end
img = imresize( img, [nCols nRows], 'bilinear' );
figure; imshow( imresize(img,10,'nearest'), [] );
fileName = [char(pwd) '/figures/originalImage.eps'];
saveas(gcf,fileName,'epsc');
title('original');
detSize = 0.001;
dTheta = 1 * pi/180;
thetas = 0:dTheta:pi-dTheta;
nThetas = numel(thetas);
nDetectors = nCols*2;
maxShift = [0 0;
0.01 0.02;
0.01 0.02]; % [maxVertical maxHorizontal]
rotation = [0 0;
0 0;
-10 * pi/180 10 * pi/180]; % [maxRot minRot]
imageNames = {'NoTransNoRot','TransAndNoRot','TransAndRot'};
% method = {'PC','LADMM','GD'}; % Options: GD, PC, LADMM
method = {'PC','LADMM'}; % Options: GD, PC, LADMM
tic;
lw = 2;
fs = 18; % font size for labels
ts = 16; % font size for tick marks
for m = 1:numel(method)
for k = 1:numel(imageNames)
translations = zeros( nThetas, 2 );
translations(:,1) = linspace(0,maxShift(k,1),nThetas);
translations(:,2) = linspace(0,maxShift(k,2),nThetas);
rotations = linspace(rotation(k,1),rotation(k,2),nThetas);
im = padImgForRadon( img, maxShift(k,2), maxShift(k,1), ...
pixSize );
[nRows,nCols] = size(im);
sinogram = radonWithRotAndTrans( im, pixSize, nDetectors, detSize, ...
thetas, rotations, translations );
figure('name','no noise'); imshow( imresize(sinogram,10,'nearest'),[])
fileName = [char(pwd) '/figures/sino' char(imageNames(k)) ...
'.eps'];
saveas(gcf,fileName,'epsc');
noise = noiseLevel*randn(size(sinogram,1),size(sinogram,2));
sinogram = sinogram + noise;
figure('name','with noise'); imshow( imresize(sinogram,10,'nearest'),[])
fileName = [char(pwd) '/figures/noisySino' char(imageNames(k)) ...
'.eps'];
saveas(gcf,fileName,'epsc');
if runFilteredBackProjection == 1 && m == 1
recon = filteredBackProjection(sinogram, thetas, detSize, ...
nCols, nRows, pixSize);
if (maxShift(k,1) == 0) && (maxShift(k,2) == 0)
figure; imshow( imresize(recon,10,'nearest'), [] );
fileName = [char(pwd) '/figures/' 'imgFBP' ...
char(imageNames(k)) '.eps'];
saveas(gcf,fileName,'epsc');
title('Reconstructed image');
else
xShiftPix = ceil(abs( maxShift(k,2) / pixSize));
yShiftPix = ceil(abs(maxShift(k,1) / pixSize));
recon = recon(yShiftPix+1:end-yShiftPix,...
xShiftPix+1:end-xShiftPix);
figure; imshow( imresize(recon,10,'nearest'), [] );
fileName = [char(pwd) '/figures/' 'imgFBP' ...
char(imageNames(k)) '.eps'];
saveas(gcf,fileName,'epsc');
title('Reconstructed image');
end
end
if runPCandLADMM == 1;
if (rotation(k,1) == 0) && (rotation(k,2) == 0)
[recon,costs] = ctCorrectForTranslation( sinogram, nDetectors, ...
detSize, thetas, translations, nCols, nRows, pixSize, ...
'method', char(method(m)) );
else
[recon,costs] = ctCorrectForRotAndTrans( sinogram, nDetectors, ...
detSize, thetas, rotations, translations, nCols, nRows, pixSize, ...
'method', char(method(m)) );
end
fileName = [char(pwd) '/figures/' 'costData' char(method(m)) ...
char(imageNames(k)) '.mat'];
save(fileName, 'costs');
figure; imshow( imresize(recon,10,'nearest'), [] );
fileName = [char(pwd) '/figures/' 'img' char(method(m)) ...
char(imageNames(k)) '.eps'];
saveas(gcf,fileName,'epsc');
title('Reconstructed image');
figure; set(gca,'FontSize',ts)
plot( costs, 'LineWidth', lw );
xlabel('Iteration','FontSize',fs);
ylabel('Cost Function','FontSize',fs);
fileName = [char(pwd) '/figures/' 'costs' char(method(m)) ...
char(imageNames(k)) '.eps'];
saveas(gcf,fileName,'epsc');
end
end
end
timeTaken = toc;
display(['Time taken: ', num2str(timeTaken)])
%% Plot learning curves from LADMM and PC on same graph
if plotLearningCurves == 1
for k = 1:numel(imageNames)
fileName = [char(pwd) '/figures/costDataLADMM' char(imageNames(k)) '.mat'];
load (fileName)
costsLADMM = costs;
fileName = [char(pwd) '/figures/costDataPC' char(imageNames(k)) '.mat'];
load (fileName)
costsPC = costs;
figure; set(gca,'FontSize',ts)
semilogy( costsPC, 'b', 'LineWidth', lw );
hold on;
semilogy( costsLADMM, 'r', 'LineWidth', lw);
axis([0 1000 1e-4 1])
xlabel('Iteration','FontSize',fs);
ylabel('Cost Function','FontSize',fs);
legPos = [0.67, 0.77, 0.01, 0.001];
leg1 = legend('Pock-Chambolle','Linearized ADMM');
set(leg1,'Position',legPos,'FontSize',fs)
fileName = [char(pwd) '/figures/' 'costsPCandLADMM' ...
char(imageNames(k)) '.eps'];
saveas(gcf,fileName,'epsc');
end
end
%% cartoon explaining what modified radon transform is
if runAnimation == 1;
close all;
img = phantom();
translations = [0.5,0]; %meters
% translations = [0,0]; %meters
pixsize = 0.01;
translations = translations/pixsize; %pixels
rotations = -120; %degrees
% rotations = 0; %degrees
img = imrotate(img,-rotations/2,'crop');
img = padImgForRadon(img,50,50,1);
% figure;
% imshow(img,[])
numFrames = 4;
images = makeImagesForReport(img,rotations,translations,numFrames);
for k = 1:numFrames
thisImage = images(:,:,k);
figure; imshow(thisImage, [])
fileName = [char(pwd) '/figures/' 'phantomRollingImageTransAndRot' ...
num2str(k) '.eps'];
saveas(gcf,fileName,'epsc');
end
end
%% make blurry phantom to demonstrate image metrics
if runBlurryPhantom == 1;
close all;
horizontaltrans = [5 2 0];
images = makeBlurryPhantoms(horizontaltrans);
for k = 1:numel(horizontaltrans)
thisImage = images(:,:,k);
figure; imshow(thisImage, [])
fileName = [char(pwd) '/figures/' 'blurryPhantomWithTrans' ...
num2str(horizontaltrans(k)) '.eps'];
saveas(gcf,fileName,'epsc');
end
end
%% plot image metrics for blurry phantom
% plot image metrics
if runBlurryPhantomMetrics == 1
[x, metric_normgrad, metric_laplacian, metric_histogram, ...
metric_variance] = makeMetricImages();
lw = 2;
fs = 18; % font size for labels
ts = 16; % font size for tick marks
ys = 6; % number of y tick marks to show
figure;
set(gca,'FontSize',ts)
plot(x,metric_normgrad,'LineWidth',lw);
xlabel('Image Shift (pixels)','FontSize',fs)
ylabel('Normalized Gradient Squared','FontSize',fs)
set(gca,'XTick',linspace(-5,5,3))
L = get(gca,'YLim');
set(gca,'YTick',linspace(L(1),L(2),ys))
set(gca,'XMinorTick','on','YMinorTick','on')
fileName = [char(pwd) '/figures/' 'metric_normgrad.eps'];
saveas(gcf,fileName,'epsc');
figure;
set(gca,'FontSize',ts)
plot(x,metric_laplacian,'LineWidth',lw);
xlabel('Image Shift (pixels)','FontSize',fs)
ylabel('Laplacian','FontSize',fs)
set(gca,'XTick',linspace(-5,5,3))
L = get(gca,'YLim');
set(gca,'YTick',linspace(L(1),L(2),ys))
set(gca,'XMinorTick','on','YMinorTick','on')
fileName = [char(pwd) '/figures/' 'metric_laplacian.eps'];
saveas(gcf,fileName,'epsc');
figure;
set(gca,'FontSize',ts)
plot(x,metric_histogram,'LineWidth',lw);
xlabel('Image Shift (pixels)','FontSize',fs)
ylabel('Histogram Energy','FontSize',fs)
set(gca,'XTick',linspace(-5,5,3))
L = get(gca,'YLim');
set(gca,'YTick',linspace(L(1),L(2),ys))
set(gca,'XMinorTick','on','YMinorTick','on')
fileName = [char(pwd) '/figures/' 'metric_histogram.eps'];
saveas(gcf,fileName,'epsc');
figure;
set(gca,'FontSize',ts)
plot(x,metric_variance,'LineWidth',lw);
xlabel('Image Shift (pixels)','FontSize',fs)
ylabel('Variance','FontSize',fs)
set(gca,'XTick',linspace(-5,5,3))
L = get(gca,'YLim');
set(gca,'YTick',linspace(L(1),L(2),ys))
set(gca,'XMinorTick','on','YMinorTick','on')
fileName = [char(pwd) '/figures/' 'metric_variance.eps'];
saveas(gcf,fileName,'epsc');
end
end