-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspm_applydef_ui.m
More file actions
66 lines (61 loc) · 2.15 KB
/
spm_applydef_ui.m
File metadata and controls
66 lines (61 loc) · 2.15 KB
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
function spm_applydef_ui(P,PT)
% Applies a deformation field to an image
%_______________________________________________________________________
% @(#)spm_applydef_ui.m 1.4 John Ashburner 04/03/24
if nargin<2
n = spm_input('Number of subjects','+0', 'n', '1', 1)';
for i=1:n
P{i} = spm_get(1,{'*y_*.img','noexpand'},['Select deformation field ' num2str(i)]);
PT{i} = spm_get(Inf,'*.img',['Image(s) to warp (' num2str(i) ')']);
end;
else
n = length(P);
if n ~=length(PT)
error('Must have matching deformation / files list cell arrays as inputs to spm_applydef_ui')
end
end
spm_progress_bar('Init',n,'Applying deformations','subjects completed');
for i=1:length(P),
Pi = [repmat([P{i} ','],3,1) num2str([1 2 3]')];
spm_applydef(Pi,PT{i});
spm_progress_bar('Set',i);
end;
spm_progress_bar('Clear')
return;
%_______________________________________________________________________
%_______________________________________________________________________
function spm_applydef(VD,VI)
if ischar(VD), VD = spm_vol(VD); end;
if ischar(VI), VI = spm_vol(VI); end;
VO = VI;
for i=1:length(VO),
VO(i).fname = prepend(VO(i).fname,'w');
VO(i).dim(1:3) = VD(1).dim(1:3);
VO(i).mat = VD(1).mat;
if ~isfield(VO,'descrip'), VO(i).descrip = ''; end;
VO(i).descrip = ['warped ' VO(i).descrip];
end;
VO = spm_create_vol(VO);
for p=1:VD(1).dim(3),
M = spm_matrix([0 0 p]);
x1 = spm_slice_vol(VD(1), M, VD(1).dim(1:2),1);
x2 = spm_slice_vol(VD(2), M, VD(1).dim(1:2),1);
x3 = spm_slice_vol(VD(3), M, VD(1).dim(1:2),1);
for i=1:length(VI),
M = inv(VI(i).mat);
y1 = M(1,1)*x1+M(1,2)*x2+M(1,3)*x3+M(1,4);
y2 = M(2,1)*x1+M(2,2)*x2+M(2,3)*x3+M(2,4);
y3 = M(3,1)*x1+M(3,2)*x2+M(3,3)*x3+M(3,4);
img = spm_sample_vol(VI(i),y1,y2,y3,1);
VO(i) = spm_write_plane(VO(i),img,p);
end;
end;
VO = spm_close_vol(VO);
return;
%_______________________________________________________________________
%_______________________________________________________________________
function out = prepend(in, pre)
[pth,nme,ext] = fileparts(in);
out = fullfile(pth,[pre nme ext]);
return;
%_______________________________________________________________________