summaryrefslogtreecommitdiffstats
path: root/matlab/algorithms/DART/DARTalgorithm.m
blob: ef5d880845e32f1e03c7a4c1eadb27e8264b9361 (plain)
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
%--------------------------------------------------------------------------
% This file is part of the ASTRA Toolbox
%
% Copyright: 2010-2018, imec Vision Lab, University of Antwerp
%            2014-2018, CWI, Amsterdam
% License: Open Source under GPLv3
% Contact: astra@astra-toolbox.com
% Website: http://www.astra-toolbox.com/
%--------------------------------------------------------------------------

classdef DARTalgorithm < matlab.mixin.Copyable
	% Algorithm class for Discrete Algebraic Reconstruction Technique (DART).
	
	%----------------------------------------------------------------------
	properties (GetAccess=public, SetAccess=public)
	
		tomography		= IterativeTomography();	% POLICY: Tomography object. 
		segmentation	= SegmentationDefault();	% POLICY: Segmentation object. 
		smoothing		= SmoothingDefault();		% POLICY: Smoothing object.
		masking			= MaskingDefault();			% POLICY: Masking object.
		output			= OutputDefault();			% POLICY: Output object.
		statistics		= StatisticsDefault();		% POLICY: Statistics object.

		base			= struct();					% DATA(set): base structure, should contain: 'sinogram', 'proj_geom', 'phantom' (optional).
		
		memory			= 'no';						% SETTING: reduce memory usage? (disables some features)
	
		implementation  = 'linear';					% SETTING: which type of projector is used ('linear', 'nonlinear')
		t				= 5;						% SETTING: # ARMiterations, each DART iteration.
		t0				= 100;						% SETTING: # ARM iterations at DART initialization.
	end
	%----------------------------------------------------------------------
	properties (GetAccess=public, SetAccess=private)
		V0					= [];			% DATA(get): Initial reconstruction.
		V					= [];			% DATA(get): Reconstruction.
		S					= [];			% DATA(get): Segmentation.
		R					= [];			% DATA(get): Residual projection data.
		Mask				= [];			% DATA(get): Reconstruction Mask.
		stats				= struct();		% Structure containing various statistics.
		iterationcount		= 0;			% Number of performed iterations.	
		start_tic			= 0;		  
		initialized			= 0;			% Is initialized?
	end
	%----------------------------------------------------------------------
	properties (Access=private)		
		adaptparam_name		= {};
		adaptparam_values	= {};
		adaptparam_iters	= {};
	end
	
	%----------------------------------------------------------------------
	methods
		
		%------------------------------------------------------------------
		function this = DARTalgorithm(varargin)
			% Constructor
			% >> D = DARTalgorithm(base);  [base is a matlab struct that
			%                               should contain 'sinogram' and
			%                               'proj_geom']
			% >> D = DARTalgorithm('base_path'); [path to base struct file]
			% >> D = DARTalgorithm(sinogram, proj_geom)
			% 
			narginchk(1, 2)
			if nargin == 1 && ischar(varargin{1})
				this.base = load(varargin{1});
			elseif nargin == 1 && isstruct(varargin{1})
				this.base = varargin{1};
			elseif nargin == 2
				this.base = struct();
				this.base.sinogram = varargin{1};
				this.base.proj_geom = varargin{2};
			else
				error('invalid arguments')
			end
		end	
		
		%------------------------------------------------------------------
		function D = deepcopy(this)
			% Create a deep copy of this object.
			% >> D2 = D.deepcopy();
			D = copy(this);
			props = properties(this);
			for i = 1:length(props)
				if isa(this.(props{i}), 'handle')
					D.(props{i}) = copy(this.(props{i}));
				end
			end			
		end
		
		%------------------------------------------------------------------
		function this = initialize(this)
			% Initializes this object.
			% >> D.initialize();
			
			% Initialize tomography part
			if ~this.tomography.initialized
				this.tomography.proj_geom = this.base.proj_geom;	
				this.tomography.initialize();
			end

			% Create an Initial Reconstruction
			if isfield(this.base, 'V0')
				this.V0 = this.base.V0;
			else
				this.output.pre_initial_iteration(this);
				this.V0 = this.tomography.reconstruct(this.base.sinogram, this.t0);
				this.output.post_initial_iteration(this);
			end
			this.V = this.V0;
			if strcmp(this.memory,'yes')
				this.base.V0 = [];
				this.V0 = [];
			end
			this.initialized = 1;
		end		
		
		%------------------------------------------------------------------
		% iterate
		function this = iterate(this, iters)
			% Perform several iterations of the DART algorithm.
			% >> D.iterate(iterations);		
			if strcmp(this.implementation,'linear')
				this.iterate_linear(iters);
			elseif strcmp(this.implementation,'nonlinear')
				this.iterate_nonlinear(iters);
			end
		end
		
		
		%------------------------------------------------------------------
		% iterate - linear projector implementation
		function this = iterate_linear(this, iters)
	
			this.start_tic = tic;
			
			for iteration = 1:iters
				this.iterationcount = this.iterationcount + 1;				

				% initial output
				this.output.pre_iteration(this);

				% update adaptive parameters
				this.update_adaptiveparameter(this.iterationcount);

				% segmentation
				this.segmentation.estimate_grey_levels(this, this.V);
				this.S = this.segmentation.apply(this, this.V);

				% select update and fixed pixels
				this.Mask = this.masking.apply(this, this.S);
				this.V = (this.V .* this.Mask) + (this.S .* (1 - this.Mask));
				F = this.V;
				F(this.Mask == 1) = 0;

				% compute residual projection difference
				this.R = this.base.sinogram - this.tomography.project(F);
				
				% ART update part
				this.V = this.tomography.reconstruct_mask(this.R, this.V, this.Mask, this.t);

				% blur
				this.V = this.smoothing.apply(this, this.V);

				%calculate statistics
				this.stats = this.statistics.apply(this);
			
				% output
				this.output.post_iteration(this);
			end
		
		end				

		%------------------------------------------------------------------
		% iterate - nonlinear projector implementation
		function this = iterate_nonlinear(this, iters)
			
			this.start_tic = tic;
			
			for iteration = 1:iters
				this.iterationcount = this.iterationcount + 1;				
		
				% Output
				this.output.pre_iteration(this);

				% update adaptive parameters
				this.update_adaptiveparameter(this.iterationcount)

				% Segmentation
				this.segmentation.estimate_grey_levels(this, this.V);
				this.S = this.segmentation.apply(this, this.V);

				% Select Update and Fixed Pixels
				this.Mask = this.masking.apply(this, this.S);
				this.V = (this.V .* this.Mask) + (this.S .* (1 - this.Mask));

				% ART update part
				this.V = this.tomography.reconstruct2_mask(this.base.sinogram, this.V, this.Mask, this.t);
				
				% blur
				this.V = this.smoothing.apply(this, this.V);
				
				% calculate statistics
				this.stats = this.statistics.apply(this);

				% output
				this.output.post_iteration(this);
			
			end
		
		end						
		
		
		%------------------------------------------------------------------
		% get data		
		function data = getdata(this, string)
			if numel(this.(string)) == 1
				data = astra_mex_data2d('get',this.(string));
			else
				data = this.(string);
			end
		end
		
		%------------------------------------------------------------------
		% add adaptive parameter
		function this = adaptiveparameter(this, name, values, iterations)
			this.adaptparam_name{end+1} = name;
			this.adaptparam_values{end+1} = values;
			this.adaptparam_iters{end+1} = iterations;
		end
		
		%------------------------------------------------------------------
		% update adaptive parameter
		function this = update_adaptiveparameter(this, iteration)
			for i = 1:numel(this.adaptparam_name)
				for j = 1:numel(this.adaptparam_iters{i})
					if iteration == this.adaptparam_iters{i}(j)
						new_value = this.adaptparam_values{i}(j);
						eval(['this.' this.adaptparam_name{i} ' = ' num2str(new_value) ';']);
					end						
				end
			end		
		end
			
		%------------------------------------------------------------------
		function settings = getsettings(this)
			% Returns a structure containing all settings of this object.
			% >> settings = tomography.getsettings();		
			
			settings.tomography = this.tomography.getsettings();
			settings.smoothing = this.smoothing.getsettings();
			settings.masking = this.masking.getsettings();
			settings.segmentation = this.segmentation.getsettings();
		end		
		%------------------------------------------------------------------
		
	end % methods
		
end % class