-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspm_get_space.m
59 lines (52 loc) · 1.77 KB
/
spm_get_space.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
function M = spm_get_space(P,M)
% Get/set the voxel-to-world mapping of an image
% FORMAT M = spm_get_space(P)
% spm_get_space(P,M)
% P - image filename
% M - voxel-to-world mapping
%__________________________________________________________________________
% Copyright (C) 1996-2015 Wellcome Trust Centre for Neuroimaging
% John Ashburner
% $Id: spm_get_space.m 6379 2015-03-16 13:27:26Z guillaume $
[pth,nam,ext,num] = spm_fileparts(P);
if ~isempty(num), n = str2num(num(2:end)); else n = [1 1]; end
P = fullfile(pth,[nam ext]);
N = nifti(P);
if nargin==2
if isempty(N.mat_intent), N.mat = N.mat; end
N.mat_intent = 'Aligned';
if n(1)==1
% Ensure volumes 2..N have the original matrix
if size(N.dat,4)>1 && sum(sum((N.mat-M).^2))>1e-8
M0 = N.mat;
if ~isfield(N.extras,'mat')
N.extras.mat = zeros([4 4 size(N.dat,4)]);
else
if size(N.extras.mat,3)<size(N.dat,4)
N.extras.mat(:,:,size(N.dat,4)) = zeros(4);
end
end
for i=2:size(N.dat,4)
if sum(sum(N.extras.mat(:,:,i).^2))==0
N.extras.mat(:,:,i) = M0;
end
end
end
N.mat = M;
if strcmp(N.mat0_intent,'Aligned'), N.mat0 = M; end
if ~isempty(N.extras) && isstruct(N.extras) && isfield(N.extras,'mat') &&...
size(N.extras.mat,3)>=1
N.extras.mat(:,:,n(1)) = M;
end
else
N.extras.mat(:,:,n(1)) = M;
end
create(N);
else
if ~isempty(N.extras) && isstruct(N.extras) && isfield(N.extras,'mat') &&...
size(N.extras.mat,3)>=n(1) && sum(sum(N.extras.mat(:,:,n(1)).^2))
M = N.extras.mat(:,:,n(1));
else
M = N.mat;
end
end