-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathrun_non_rigid_registration.m
187 lines (155 loc) · 6.65 KB
/
run_non_rigid_registration.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
function [deformed, displacementField, displacementFieldInverse] = ...
run_non_rigid_registration(...
moving, fixed, method, accumulationMethod, ...
startScale, stopScale, numberOfIterationsPerScale, ...
fluidRegularization, fluidRegularizationData, ...
elasticRegularization, elasticRegularizationData, ...
varargin)
% RUN_NON_RIGID_REGISTRATION Run non-rigid registration on provided data
%
% run_non_rigid_registration(moving, fixed, method, accumulationMethod, ...
% startScale, stopScale, numberOfIterationsPerScale, ...
% fluidRegularization, fluidRegularizationData, ...
% elasticRegularization, elasticRegularizationData)
%
% INPUT ARGUMENTS
% moving - Moving image
% fixed - Fixed image
% method - 'morphon' or 'demons'
% accumulationMethod - 'add'/'compositive'/'diffeomorphic'
% startScale - Start scale for registration
% stopScale - Stop scale for registration
% numberOfIterationsPerScale - Number of iterations per scale
% fluidRegularization - Regularization of update displacement field
% fluidRegularizationData - Sigma for regularization
% elasticRegularization - Regularization of accumulated displacement field
% elasticRegularizationData - Sigma for regularization
%
% OPTIONAL INPUT ARGUMENTS
% 'symmetric' - true/false
% 'inverse' - true/false
% 'GPU' - true/false
%
% OUTPUT ARGUMENTS
% deformed - Deformed moving image
% displacementField - Estimated displacement field
% displacementFieldInverse - Inverse displacement field
% Copyright (c) 2015 Daniel Forsberg
% danne.forsberg@outlook.com
%
% This program is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program. If not, see <http://www.gnu.org/licenses/>.
%% Default parameters
symmetric = false;
inverse = false;
GPU = true;
% Overwrites default parameters
for k=1:2:length(varargin)
eval([varargin{k},'=varargin{',int2str(k+1),'};']);
end
%% Registration
if GPU
%% Set GPU version
clear global registrationCUDA
if ~exist('check_registrationCUDA_affine.m','file') || ...
~exist('CUDA_non_rigid_registration3d.m','file')
error(['Files needed for GPU-based image registration are unavailable. ',...
'Either they are not on the path or the actual toolboxes for this are missing. ',...
'The toolboxes can be downloaded at https://bitbucket.org/dforsberg/matlab_cuda ',...
'and https://bitbucket.org/dforsberg/cuda'])
end
if ndims(moving) ~= 3 || ndims(fixed) ~= 3
error('GPU-based image registration is only supported for 3D data.')
end
global registrationCUDA;
registrationCUDA.moving = moving;
registrationCUDA.fixed = fixed;
registrationCUDA.dims = ndims(registrationCUDA.fixed);
registrationCUDA.method = method;
% add, compositive, diffeomorphic
registrationCUDA.accumulation = accumulationMethod;
% Number of scales
registrationCUDA.startScale = startScale;
registrationCUDA.stopScale = stopScale;
% Iterations per scale
registrationCUDA.numberOfIterationsPerScale = numberOfIterationsPerScale;
% Symmetric
registrationCUDA.symmetric = symmetric;
% Inverse
registrationCUDA.inverse = inverse;
% Regularization parameters
registrationCUDA.fluidRegularization = fluidRegularization;
registrationCUDA.fluidRegularizationData = fluidRegularizationData;
registrationCUDA.elasticRegularization = elasticRegularization;
registrationCUDA.elasticRegularizationData = elasticRegularizationData;
% Morphon specific parameters
load quadratureFiltersForMorphonRegistration3D
filterDirection = cell2mat(filterDirection);
registrationCUDA.filterDirectionX = filterDirection(2:3:end);
registrationCUDA.filterDirectionY = filterDirection(1:3:end);
registrationCUDA.filterDirectionZ = filterDirection(3:3:end);
registrationCUDA.quadFilters = f;
check_registrationCUDA();
%% Run non-rigid GPU registration
displacementField = cell(3,1);
displacementFieldInverse = cell(3,1);
if inverse
[deformed, displacementField{1}, displacementField{2}, displacementField{3}, ...
displacementFieldInverse{1}, displacementFieldInverse{2}, displacementFieldInverse{3}] = ...
CUDA_non_rigid_registration3d(registrationCUDA);
else
[deformed, displacementField{1}, displacementField{2}, displacementField{3}] = ...
CUDA_non_rigid_registration3d(registrationCUDA);
end
else
%% Set CPU version
clear global registration
global registration;
% Set data
registration.moving = moving;
registration.fixed = fixed;
registration.dims = ndims(registration.fixed);
% Set registration parameters
if strcmp(method,'morphon')
registration.method = 'phase';
elseif strcmp(method,'demons')
registration.method = 'optical-flow';
end
registration.transformationModel = 'non-rigid';
% add, compositive, diffeomorphic
registration.accumulationMethod = accumulationMethod;
% Number of scales
registration.startScale = startScale;
registration.stopScale = stopScale;
% Iterations per scale
registration.iterationsPerScale = numberOfIterationsPerScale;
% Symmetric
registration.symmetric = symmetric;
% Inverse
registration.inverse = inverse;
% Regularization parameters
registration.fluidRegularization = fluidRegularization;
registration.fluidRegularizationData = fluidRegularizationData;
registration.elasticRegularization = elasticRegularization;
registration.elasticRegularizationData = elasticRegularizationData;
% Launch registration
registration_execute();
% Get results
deformed = registration.deformed;
displacementField = registration.displacementField;
if inverse
displacementFieldInverse = registration.inverseDisplacementField;
else
displacementFieldInverse = cell(3,1);
end
end