/
ft_convert_coordsys.m
320 lines (284 loc) · 12.7 KB
/
ft_convert_coordsys.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
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
function [object] = ft_convert_coordsys(object, target, varargin)
% FT_CONVERT_COORDSYS changes the coordinate system of the input object to the
% specified coordinate system. The coordinate system of the input object is
% determined from the 'coordsys' field in the input data, or needs to be determined
% and specified interactively by the user.
%
% Use as
% [output] = ft_convert_coordsys(input, target)
% [output] = ft_convert_coordsys(input, target, method)
% [output] = ft_convert_coordsys(input, target, method, template)
% to determine and convert the coordinate system.
%
% With the optional method input argument you can determine whether to use SPM for an
% affine or non-linear transformation.
% method = 0: only an approximate coregistration (default for non-MRI data)
% method = 1: an approximate coregistration, followed by spm_affreg
% method = 2: an approximate coregistration, followed by spm_normalise (default for MRI data)
%
% The following input data structures are supported
% electrode or gradiometer array, see FT_DATATYPE_SENS
% volume conduction model, see FT_DATATYPE_HEADMODEL
% source model, see FT_DATATYPE_SOURCE and FT_PREPARE_SOURCEMODEL
% anatomical mri, see FT_DATATYPE_VOLUME
% segmented mri, see FT_DATATYPE_SEGMENTATION
% anatomical or functional atlas, see FT_READ_ATLAS
%
% Recognized and supported coordinate systems are 'ctf', 'bti', '4d', 'yokogawa',
% 'eeglab', 'neuromag', 'itab', 'acpc', 'spm', 'mni', 'fsaverage', 'tal', 'scanras',
% 'scanlps', 'dicom'.
%
% Furthermore, supported coordinate systems that do not specify the origin are 'ras',
% 'als', 'lps', etc. See https://www.fieldtriptoolbox.org/faq/coordsys for more
% details.
%
% Note that the conversion will be an automatic and approximate conversion, not
% taking into account differences in individual anatomies/differences in conventions
% where to put the fiducials.
%
% See also FT_DETERMINE_COORDSYS, FT_DETERMINE_UNITS, FT_CONVERT_UNITS, FT_PLOT_AXES, FT_PLOT_XXX
% Undocumented options
% feedback = string, 'yes' or 'no' (default = 'no')
% Copyright (C) 2005-2023, Robert Oostenveld & Jan-Mathijs Schoffelen
%
% This file is part of FieldTrip, see http://www.fieldtriptoolbox.org
% for the documentation and details.
%
% FieldTrip 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.
%
% FieldTrip 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 FieldTrip. If not, see <http://www.gnu.org/licenses/>.
%
% $Id$
if nargin>3 && isnumeric(varargin{1})
% old-style with 2 extra input arguments
tmp = {'method', varargin{1}, 'template', varargin{2}};
varargin = tmp;
elseif nargin>2 && isnumeric(varargin{1})
% old-style with 1 extra input argument
tmp = {'method', varargin{1}};
varargin = tmp;
end
method = ft_getopt(varargin, 'method'); % default is handled below
templatefile = ft_getopt(varargin, 'template'); % default is handled in the SPM section
feedback = ft_getopt(varargin, 'feedback', 'no');
spmversion = ft_getopt(varargin, 'spmversion');
% use the version that is on the path, or default to spm12
if isempty(spmversion)
if ~ft_hastoolbox('spm')
spmversion = 'spm12';
else
spmversion = lower(spm('ver'));
end
end
if ~isfield(object, 'coordsys') || isempty(object.coordsys)
% determine the coordinate system of the input object
object = ft_determine_coordsys(object, 'interactive', 'yes');
if ~isfield(object, 'coordsys') || isempty(object.coordsys)
% the call to ft_determine_coordsys should have taken care of this, but
% it is possible that the user aborted the coordinate system
% determination. See http://bugzilla.fieldtriptoolbox.org/show_bug.cgi?id=2526
ft_error('the coordinate system of the geometrical object is not specified');
end
ft_notice('the coordinate system is determined as ''%s''', object.coordsys);
end
if isequal(object.coordsys, target)
% no further conversion is needed
return
end
if isempty(method)
if isfield(object, 'transform') && isfield(object, 'anatomy')
% the default for an anatomical MRI is to start with an approximate alignment,
% followed by a call to spm_normalise for a better quality alignment
method = 2;
else
% the default for all other objects is to do only an approximate alignment
method = 0;
end
end
if ~ismember(object.coordsys, {'spm', 'mni', 'fsaverage', 'tal'}) && ismember(target, {'spm', 'mni', 'fsaverage', 'tal'})
% the input appears to be an individual subject MRI which has not been rescaled
% the target is a template coordinate system
% see http://bugzilla.fieldtriptoolbox.org/show_bug.cgi?id=3304 and http://bit.ly/2sw7eC4
ft_warning('Not applying any scaling, using ''acpc'' instead of ''%s''. See http://bit.ly/2sw7eC4', target);
target = 'acpc';
end
if ~isfield(object, 'unit') || isempty(object.unit)
% determine the units of the input object
object = ft_determine_units(object);
ft_notice('the units are determined as ''%s''', object.unit);
end
% all of the internal logic inside this function requires that the units are in millimeter
% it will be converted back at the end of this function
original = object;
object = ft_convert_units(object, 'mm');
%--------------------------------------------------------------------------
% Do an initial affine registration (rigid body) alignment to the target coordinate
% system. This deals with 90-degree rotations between RAS and ALS, or between CTF and
% NEUROMAG, but also with approximate alignment between external NAS/LPA/RPA
% landmark-based coordinates to internal ACPCP landmark-based coordinate systems.
% these are the 48 generic axis orientation triplets, these specify the axes but no origin
% a = anterior
% p = posterior
% l = left
% r = right
% s = superior
% i = inferior
generic = {
'als'; 'ali'; 'ars'; 'ari';...
'pls'; 'pli'; 'prs'; 'pri';...
'las'; 'lai'; 'ras'; 'rai';...
'lps'; 'lpi'; 'rps'; 'rpi';...
'asl'; 'ail'; 'asr'; 'air';...
'psl'; 'pil'; 'psr'; 'pir';...
'sal'; 'ial'; 'sar'; 'iar';...
'spl'; 'ipl'; 'spr'; 'ipr';...
'sla'; 'ila'; 'sra'; 'ira';...
'slp'; 'ilp'; 'srp'; 'irp';...
'lsa'; 'lia'; 'rsa'; 'ria';...
'lsp'; 'lip'; 'rsp'; 'rip'}';
if ismember(object.coordsys, generic) && strcmp(target, 'acpc') && method>0
% converting anatomical MRI data from a generic coordinate system to ACPC with SPM should work
% do an initial alignment of the anatomical MRI data to RAS to bring it closer to ACPC
initial = ft_affinecoordinates(object.coordsys, 'ras');
else
initial = ft_affinecoordinates(object.coordsys, target);
end
object = ft_transform_geometry(initial, object);
object.coordsys = target;
%--------------------------------------------------------------------------
% Do a second round of affine registration (rigid body) to get improved
% alignment with ACPC coordinate system. This is needed because there may be
% different conventions defining LPA and RPA. The affine registration may
% fail however, e.g. if the initial alignment is not close enough. In that
% case SPM will throw an error.
%
% We expect the template MRIs to be expressed in millimeter and to be
% approximately aligned with ACPC.
if method>0
if isdeployed && isempty(templatefile)
ft_error('you need to specify a template filename for the coregistration');
end
if ~isfield(object, 'transform') || ~isfield(object, 'anatomy')
ft_error('affine or non-linear transformation are only supported for anatomical MRIs');
end
if ~isfield(object, 'unit') || ~strcmp(object.unit, 'mm')
ft_error('affine or non-linear transformation require the anatomial MRI to be expressed in mm');
end
% the following requires SPM to be on the path
ft_hastoolbox(spmversion, 1);
end
if method==1
% use spm_affreg
if isempty(templatefile)
switch lower(spm('ver'))
case 'spm2'
if isdeployed
if nargin<3, ft_error('you need to specify a template filename when in deployed mode and using method==1'); end
else
templatefile = fullfile(spm('Dir'),'templates','T1.mnc');
end
case 'spm8'
if isdeployed
if nargin<3, ft_error('you need to specify a template filename when in deployed mode and using method==1'); end
elseif isempty(templatefile)
templatefile = fullfile(spm('Dir'),'templates','T1.nii');
end
case 'spm12'
if isdeployed
if nargin<3, ft_error('you need to specify a template filename when in deployed mode and using method==1'); end
else
templatefile = fullfile(spm('Dir'),'toolbox','OldNorm','T1.nii');
if ~exist('spm_affreg', 'file')
addpath(fullfile(spm('Dir'),'toolbox','OldNorm'));
end
end
fprintf('using ''OldNorm'' affine registration\n');
otherwise
ft_error('unsupported SPM version');
end
end
template = ft_read_mri(templatefile);
tname1 = [tempname, '.img'];
tname2 = [tempname, '.img'];
V1 = ft_write_mri(tname1, object.anatomy, 'transform', object.transform, 'spmversion', spm('ver'), 'dataformat', 'nifti_spm');
V2 = ft_write_mri(tname2, template.anatomy, 'transform', template.transform, 'spmversion', spm('ver'), 'dataformat', 'nifti_spm');
% the below, using just spm_affreg does not work robustly enough in some cases
flags.regtype = 'rigid';
[M, scale] = spm_affreg(V1,V2,flags);
% some juggling around with the transformation matrices
vox2head = object.transform;
vox2acpc2 = M \ V1.mat;
acpc2head2 = vox2head / vox2acpc2;
% update the transformation matrix
object.transform = vox2acpc2;
% delete the temporary files
delete(tname1); delete(strrep(tname1, 'img', 'hdr'));
delete(tname2); delete(strrep(tname2, 'img', 'hdr'));
elseif method==2
% use spm_normalise
if isempty(templatefile)
switch lower(spm('ver'))
case 'spm2'
if isdeployed
if nargin<3, ft_error('you need to specify a template filename when in deployed mode and using method==2'); end
else
templatefile = fullfile(spm('Dir'),'templates','T1.mnc');
end
case 'spm8'
if isdeployed
if nargin<3, ft_error('you need to specify a template filename when in deployed mode and using method==2'); end
else
templatefile = fullfile(spm('Dir'),'templates','T1.nii');
end
case 'spm12'
% this uses the 'OldNorm' functionality, so the path needs to be added, can only be done if non-deployed.
if isdeployed
if nargin<3, ft_error('you need to specify a template filename when in deployed mode and using method==2'); end
else
templatefile = fullfile(spm('Dir'),'toolbox','OldNorm','T1.nii');
if ~exist('spm_normalise', 'file')
addpath(fullfile(spm('Dir'),'toolbox','OldNorm'));
end
end
fprintf('using ''OldNorm'' normalisation\n');
otherwise
ft_error('unsupported SPM version');
end
end
template = ft_read_mri(templatefile);
tname1 = [tempname, '.img'];
tname2 = [tempname, '.img'];
V1 = ft_write_mri(tname1, object.anatomy, 'transform', object.transform, 'unit', object.unit, 'spmversion', spm('ver'), 'dataformat', 'nifti_spm');
V2 = ft_write_mri(tname2, template.anatomy, 'transform', template.transform, 'unit', template.unit, 'spmversion', spm('ver'), 'dataformat', 'nifti_spm');
flags.nits = 0; % set number of non-linear iterations to zero
flags.regtype = 'rigid';
params = spm_normalise(V2,V1,[],[],[],flags);
% some juggling around with the transformation matrices
vox2head = object.transform;
acpc2head2 = V1.mat*params.Affine/V2.mat;
vox2acpc2 = acpc2head2\vox2head;
% update the transformation matrix
object.transform = vox2acpc2;
% delete the temporary files
delete(tname1); delete(strrep(tname1, 'img', 'hdr'));
delete(tname2); delete(strrep(tname2, 'img', 'hdr'));
end
if istrue(feedback)
% give some graphical feedback
ft_determine_coordsys(object, 'interactive', 'no', 'fontsize', 15);
% also add the original axes
transform = object.transform * inv(original.transform);
ft_plot_axes([], 'transform', transform, 'unit', 'mm', 'coordsys', original.coordsys, 'fontsize', 15);
end
% all of the internal logic inside this function required that the units were in millimeter
% convert back to the original units
object = ft_convert_units(object, original.unit);