-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_tomo_res.m
156 lines (120 loc) · 4.45 KB
/
test_tomo_res.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
% Tomography test
%
% 2D Tomgoraphy example
% min_{x} \| A*x - b \|^2 subject to x \in \{-1, 1\}^n
%
% b - projected data
% A - tomography matrix
% x - (binary) image
%
% Created by:
% - Ajinkya Kadu, Utrecht University
% Feb 18, 2020
clc; clearvars; close all;
s = RandStream('mt19937ar','Seed',12);
RandStream.setGlobalStream(s);
addpath(genpath([pwd '/bin']));
%% generate true image, projection matrix and data
fprintf('------------- Setting up ---------------- \n')
imName = 'butterfly';
I = imread([pwd '/images/' imName '.png']);
I = double(I); % convert image to double
I = I/max(I(:)); % rescale
k = 4; % sampling
I = I(1:k:end,1:k:end);
% convert image to pixel values of -1 and 1
xt = I;
xt(xt<0.5) = -1;
xt(xt>0.5) = 1;
n = size(xt,1); % size of image
u = unique(xt(:)); % unique greylevels
% generate a tomography matrix
theta = round(linspace(0,120,6)); % angles (in degrees)
A = fancurvedtomo(n,theta); % parallel-beam geometry
A = A/normest(A); % rescale matrix
bt = A*xt(:); % generate (true) data
% add noise to data (additive white Gaussian noise)
noiseLevel = 0.0;
noiseB = randn(size(bt));
noiseB = noiseLevel*(noiseB/norm(noiseB)*norm(bt));
b = bt + noiseB;
sigma = 0.5*norm(A*xt(:)-b)^2; % measure the noise level
fprintf('matrix A and data b generated \n');
fprintf('matrix A: m: %d, n: %d \n',size(A));
fprintf(['angles = ' num2str(theta) '\n']);
fprintf(['The noise is ' num2str(sigma) '\n']);
s1 = sprintf('matrix A: %d x %d',size(A));
s2 = sprintf(['angles = ' num2str(theta)]);
s3 = sprintf(['The noise is ' num2str(sigma)]);
%% LSQR solution
%
fprintf('----------- LSQR solution --------------- \n');
xP = lsqr(A,b,1e-6,1e4);
% threshold
xPt = 0*xP;
xPt(xP<0)= -1;
xPt(xP>0)= 1;
xP = reshape(xP,n,n);
xPt = reshape(xPt,n,n);
% performance measures
misfitP = 0.5*norm(A*xPt(:)-b)^2;
jacIdP = nnz(xPt(:)==xt(:))/nnz(xt(:));
incIdP = nnz(min(xPt(:).*xt(:),0));
fprintf('misfit = %.4f \n',misfitP);
fprintf('jaccard index = %.4f \n',jacIdP);
fprintf('Incorrect pixels = %d \n',incIdP);
sP1 = sprintf('misfit = %.4f',misfitP);
sP2 = sprintf('jaccard index = %.4f',jacIdP);
sP3 = sprintf('Incorrect pixels = %d',incIdP);
%% dual - (first-order optimization method)
%
% solve: min_{p} |A'*p|_1 + 0.5*|p - b|_2^2
%
% We use first-order method: primal-dual method
fprintf('------------- Dual solution ------------- \n')
options.maxIter = 1e6;
options.optTol = 1e-6;
options.progTol = 1e-10;
options.savehist= 0;
[xD,hist] = solveBT(A,b,options);
% threshold
xDt = xD;
xDt(abs(xD) < 0.999) = 0;
xDt = sign(xDt);
xDt = reshape(xDt,n,n);
% performance measures
misfitD = 0.5*norm(A*xDt(:)-b)^2;
jacIdD = nnz(xDt(:)==xt(:))/nnz(xt(:));
incIdD = nnz(min(xDt(:).*xt(:),0));
undetD = nnz(xDt(:)==0);
sD1 = sprintf('misfit = %.4f',misfitD);
sD2 = sprintf('jaccard index = %.4f',jacIdD);
sD3 = sprintf('Incorrect pixels = %d',incIdD);
sD4 = sprintf('Undetermined pixels = %d',undetD);
fprintf('misfit = %.4f \n',misfitD);
fprintf('jaccard index = %.4f \n',jacIdD);
fprintf('Incorrect pixels = %d \n',incIdD);
fprintf('Undetermined pixels = %d \n',undetD);
%% compare
figure;semilogy(hist.opt); hold on;semilogy(hist.er); hold off;
xlabel('iterate');legend('optimality','progress');
fig1 = figure;
set(fig1, 'Position', [ 557, 170, 1212, 682])
subplot(2,4,1); imagesc(xt,[-1 1]);axis image;
axis off; colormap gray; title('true');
subplot(2,4,5); text(0,0.75,sprintf('%s\n%s\n%s',s1,s2,s3));axis off;
subplot(3,5,3); imagesc(xP,[-1 1]);axis image;
axis off; colormap gray; title('LSQR');
subplot(3,5,4); imagesc(xPt,[-1 1]);axis image;
axis off; colormap gray; title('(LSQR)_\tau');
subplot(3,5,5); imagesc(xDt,[-1 1]);axis image;
axis off; colormap gray; title('Dual');
subplot(3,5,8); imagesc(xP-xt,[-1 1]);axis image;
axis off; colormap gray; title('difference');
subplot(3,5,9); imagesc(xPt-xt,[-1 1]);axis image;
axis off; colormap gray; title('incorrect pixels');
subplot(3,5,10); imagesc(abs(xDt).*(xDt-xt),[-1 1]);axis image;
axis off; colormap gray; title('incorrect pixels');
subplot(3,5,14); text(0,1,sprintf('%s\n%s\n%s',sP1,sP2,sP3));axis off;
subplot(3,5,15); text(0,1,sprintf('%s\n%s\n%s\n%s',sD1,sD2,sD3,sD4));axis off;
% saveas(fig1,[pwd '/results/' imName],'png');