-
Notifications
You must be signed in to change notification settings - Fork 7
/
center_image.m
35 lines (30 loc) · 898 Bytes
/
center_image.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
function W = center_image(filename)
if(~ischar(filename))
error('The input argument filename has to be a string.');
end
V = spm_vol(filename);
N = length(V);
if(N==1)
W = cg_set_com_mod(filename);
elseif(N>1)
% Create sum image
[p,n,e] = fileparts(filename);
sum_filename = fullfile(p,[n '_tempsum' e]);
img = spm_read_vols(V);
sum_img = sum(img,4);
V0 = V(1);
V0.fname = sum_filename;
spm_write_vol(V0,sum_img);
% Estimate orientation matrix using the sum image
W = cg_set_com_mod(sum_filename);
delete(sum_filename);
% Use the orientation matrix for all the individual images
images = cellstr(spm_select('ExtFPList',p,[n e]));
for i = 1:N
spm_get_space(images{i},W);
end
%spm_reorient(images,W);
else
error('Could not center %s. There is possibly something wrong with the file.\n',filename);
end
end