-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_tomo_TV.m
168 lines (129 loc) · 4.52 KB
/
test_tomo_TV.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
% Tomography test with total-variation regularization
% works for medium-scale problems
%
% 2D Tomgoraphy example
% min_{x} |A*x - b|_2^2 + lambda*|D*x|_1 subject to x \in {-1, 1}^n
%
% b - projected data
% A - tomography matrix
% x - (binary) image
% D - 2D gradient operator/matrix
% lambda - Total-variation regularization parameter
%
% Created by:
% - Ajinkya Kadu, Utrecht University
% Feb 18, 2020
clc; clearvars; close all;
s = RandStream('mt19937ar','Seed',1);
RandStream.setGlobalStream(s);
addpath(genpath([pwd '/bin']));
%% generate true image, projection matrix and data
fprintf('------------- Setting up ---------------- \n')
I = imread([pwd '/images/spring.png']);
I = double(I); % convert image to double
I = I/max(I(:)); % rescale
k = 8; % 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,150,10));% angles (in degrees)
A = fancurvedtomo(n,theta); % fan-beam geometry
A = A/normest(A); % rescale matrix
bt = A*xt(:); % generate (true) data
% add noise to data (additive white Gaussian noise)
noiseLevel = 0.05;
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']);
%% TV setup
D = finiteDiff(n);
D = D/normest(D);
lambda = 5e-3;
trueTV = norm(D*xt(:),1);
fprintf('true total-variation = %.4f \n',trueTV);
%% TV solution
% solve:
% min_{x} |b - A*x|_2^2 + lambda * |D*x|_1
%
% D is a finite difference matrix approximating the gradient
% lambda is a regularization parameter
fprintf('------------ TV solution ---------------- \n');
options.maxIter = 1e5;
options.optTol = 1e-6;
options.progTol = 1e-6;
options.saveHist= 0;
[xTV,histTV] = solveTV(A,b,D,lambda,options);
% threshold
xTVt = xTV;
xTVt(xTV<0) = -1;
xTVt(xTV>0) = 1;
xTV = reshape(xTV,n,n);
xTVt = reshape(xTVt,n,n);
% performance measures
misfitP = 0.5*norm(A*xTVt(:)-b)^2;
tvP = norm(D*xTVt(:),1);
jacIdP = nnz(xTVt(:)==xt(:))/nnz(xt(:));
incIdP = nnz(min(xTVt(:).*xt(:),0));
fprintf('misfit = %.4f \n',misfitP);
fprintf('total-variation = %.4f \n',tvP);
fprintf('jaccard index = %.4f \n',jacIdP);
fprintf('Incorrect pixels = %d \n',incIdP);
%% dualTV - (using first-order optimization method)
%
% solve: min_{p} |A'*p+D'*q|_1 + 0.5*|p - b|_2^2
% subject to |q|_inf <= lambda
%
% The solution is retrieved using
% x = -sign(A'*p + D'*q)
%
% We use first-order method: primal-dual algorithm
fprintf('----------- Dual-TV solution ------------ \n')
options.maxIter = 1e6;
options.optTol = 1e-6;
options.progTol = 1e-6;
options.savehist= 0;
[xD,hist] = solveTVBT(A,b,D,lambda,options);
% threshold
xDt = xD;
xDt(abs(xD) < 0.99) = 0;
xDt = sign(xDt);
xDt = reshape(xDt,n,n);
% performance measures
misfitD = 0.5*norm(A*xDt(:)-b)^2;
tvD = norm(D*xDt(:),1);
jacIdD = nnz(xDt(:)==xt(:))/nnz(xt(:));
incIdD = nnz(min(xDt(:).*xt(:),0));
undetD = nnz(xDt(:)==0);
fprintf('misfit = %.4f \n',misfitD);
fprintf('total-variation = %.4f \n',tvD);
fprintf('jaccard index = %.4f \n',jacIdD);
fprintf('Incorrect pixels = %d \n',incIdD);
fprintf('Undetermined pixels = %d \n',undetD);
%% compare
figure; subplot(1,2,1); semilogy(histTV.opt); hold on; semilogy(histTV.er);
legend('optimality','progress'); title('TV');
subplot(1,2,2); semilogy(hist.opt); hold on; semilogy(hist.er);
legend('optimality','progress'); title('TVDual');
figure;
subplot(1,4,1); imagesc(xt,[-1 1]);axis image;
axis off; colormap gray; title('true');
subplot(2,4,2); imagesc(xTV,[-1 1]);axis image;
axis off; colormap gray; title('TV');
subplot(2,4,3); imagesc(xTVt,[-1 1]);axis image;
axis off; colormap gray; title('(TV)_\tau');
subplot(2,4,4); imagesc(xDt,[-1 1]);axis image;
axis off; colormap gray; title('Dual-TV');
subplot(2,4,7); imagesc(xTVt-xt,[-1 1]);axis image;
axis off; colormap gray; title('incorrect pixels');
subplot(2,4,8); imagesc(abs(xDt).*(xDt-xt),[-1 1]);axis image;
axis off; colormap gray; title('incorrect pixels');