When you press the reset orientation button in SPM, you can select all
the images to be reset. Or use the code attached here for an
automatically job.
The first one works for the images converted by SPM or voxbo. The second
one is for images converted by other software. To check whether you need
to use the second one or not, you should make a copy of the nii file and
manually reset its origin using spm, and then display the image to see
whether the orientation turned into posterior to anterior (the axial
image shows the posterior brain on top while anterior part on bottom)
and right to left (this is little bit tricky to know). If so, you can
use the second m file for batch resting the origins.
Wish this could help.
Ze
On 4/9/2013 1:31 PM, Lucas Lessa wrote:
> Good afternoon,
>
> I have many files from a research (about 50) and I processed then in FSL.
> Now I'd like to compare the results, but to process these files in SPM I'll have to chance the origin of everyone.
> Is there any script or program that do it automatically?
> Thank you very much.
>
> Lucas Lessa.
>
>
--
Ze Wang, Ph.D
Research Assistant Professor of Biomedical Engineering,
Departments of Psychiatry and Radiology,
Perelman School of Medicine,
University of Pennsylvania,
3900 Chestnut Street,
Philadelphia, PA 19104, USA
http://cfn.upenn.edu/~zewang
Tel: 215-222-3200 ext 123
% batch reset T1 orientation
global PAR
par
fprintf('\r%-40s\n','batch reset orientation ')
%%%%% the following codes are changed from batch_smooth
% dirnames,
% get the subdirectories in the main directory
for sb = 1:PAR.nsubs % for each subject
%go get the sessions
str = sprintf('sub #%3d/%3d: %-5s',sb,PAR.nsubs,PAR.subjects{sb} );
fprintf('\r%-40s %30s',str,' ')
fprintf('%s%30s',repmat(sprintf('\b'),1,30),'...reseting') %-#
%now get all images to reset orientation
%prepare directory
P=[];
for c=1:PAR.ncond
% get files in this directory
%Ptmp=spm_select('FPList', char(PAR.condirs{sb,c}), ['^' PAR.confilters{c} '\w*\.img$']);
Ptmp=spm_select('ExtFPList', char(PAR.condirs{sb,c}), ['^' PAR.confilters{c} '.*\.img$'],1:400);
P=strvcat(P,Ptmp);
if isempty(P)
disp('!!!!!!!!\n NO epi file\n!!!!!!!!!!')
continue;
end;
V = spm_vol(P);
for i=1:size(V,1)
fprintf('%s%30s',repmat(sprintf('\b'),1,10),sprintf('%s%5d/%5d ','...reseting epi ',i,size(P,1)) ) %-#
M = V(i).mat;
vox = sqrt(sum(M(1:3,1:3).^2));
smat=abs(M(1:3,1:3));
[mv,loc]=max(smat,[],1);
[mv,locoff]=max(smat,[],2);
if det(M(1:3,1:3))<0, vox(1) = -vox(1); end;
orig = (V(i).dim(1:3)+1)/2;
off = -vox.*orig;
M = [0 0 0 off(locoff(1))
0 0 0 off(locoff(2))
0 0 0 off(locoff(3))
0 0 0 1];
M(loc(1),1)=vox(1);
M(loc(2),2)=vox(2);
M(loc(3),3)=vox(3);
spm_get_space(P(i,:),M);
v=spm_vol(P(i,:));
v.mat(2,:)=-v.mat(2,:);
spm_get_space(P(i,:),v.mat);
end
end
P=[];
Ptmp=spm_select('FPList', char(PAR.structdir{sb}), ['^' PAR.structprefs '\w*.img$']);
P=strvcat(P,Ptmp);
if strcmp(P(1,:),'/')
disp('!!!!!\n NO struct file\n!!!!!!!!!!!!!!!')
continue;
end;
for i=1:size(P,1),
fprintf('%s%30s',repmat(sprintf('\b'),1,30),sprintf('%s%5d/%5d','...reseting T1 ',i,size(P,1)) ) %-#
V = spm_vol(deblank(P(i,:)));
M = V(i).mat;
vox = sqrt(sum(M(1:3,1:3).^2));
smat=abs(M(1:3,1:3));
[mv,loc]=max(smat,[],1);
[mv,locoff]=max(smat,[],2);
if det(M(1:3,1:3))<0, vox(1) = -vox(1); end;
orig = (V(i).dim(1:3)+1)/2;
off = -vox.*orig;
M = [0 0 0 off(locoff(1))
0 0 0 off(locoff(2))
0 0 0 off(locoff(3))
0 0 0 1];
M(loc(1),1)=vox(1);
M(loc(2),2)=vox(2);
M(loc(3),3)=vox(3);
spm_get_space(P(i,:),M);
v=spm_vol(P(i,:));
v.mat(2,:)=-v.mat(2,:);
spm_get_space(P(i,:),v.mat);
end;
fprintf('%s%30s',repmat(sprintf('\b'),1,30),'...done') %-#
end
% batch scripts adapted from ASLtbx for processing the CARDIA resting bold and resting ASL data
% Ze Wang.
par
fprintf('\r%-40s\n','batch reset orientation ')
% get the subdirectories in the main directory
for sb =1:length(PAR.subjects) % for each subject % 1-172 UAB use sub 31 3-0144 to check left right
%go get the sessions
for ses=1:PAR.nsess(sb)
% get files in this directory
for jj=1%:PAR.ncond
P=[];
P=spm_select('ExtFPList', char(PAR.subs(sb).ses(ses).condirs{jj}), ['^' PAR.fileprefix{jj} '_.*\.nii$'],1:600);
P=strvcat(P, spm_select('ExtFPList', char(PAR.subs(sb).ses(ses).condirs{jj}), ['^EPI_noskull\.nii'],1));
if isempty(P)
disp('!!!!!!!!!!!!!!!!\n NO epi file\n!!!!!!!!!!!!!!!!!!!!!')
continue;
end;
% P=strvcat(P,spm_select('ExtFPList', char(PAR.subs(sb).ses(ses).condirs{jj}), ['^r' PAR.fileprefix{jj} '.*\.nii$'],1:600));
str = sprintf('sub #%3d/%3d: %-5s',sb,length(PAR.subjects),PAR.subjects{sb});
fprintf('\r%-10s %5s',str,' ')
fprintf('%s%20s',repmat(sprintf('\b'),1,2),'...reseting\n') %-#
V = spm_vol(P);
for i=1:size(V,1)
% fprintf('%s%30s',repmat(sprintf('\b'),1,2),sprintf('%s%5d/%5d ','...reseting epi ',i,size(P,1)) ) %-#
M = V(i).mat;
vox = sqrt(sum(M(1:3,1:3).^2));
smat=abs(M(1:3,1:3));
[mv,loc]=max(smat,[],1);
[mv,locoff]=max(smat,[],2);
% be careful for using the following lines: this will only
% work if the data were acquired or saved from right to
% left, from anterior to posterio and inferior to superior
vox(1)=-abs(vox(1));
vox(2)=-abs(vox(2));
orig = (V(i).dim(1:3)+1)/2;
off = -vox.*orig;
M = [0 0 0 off(locoff(1))
0 0 0 off(locoff(2))
0 0 0 off(locoff(3))
0 0 0 1];
M(loc(1),1)=vox(1);
M(loc(2),2)=vox(2);
M(loc(3),3)=vox(3);
% M(2,:)= -M(2,:);
spm_get_space(P(i,:),M);
end
end
% P=spm_select('ExtFPList', char(PAR.subs(sb).ses(ses).structdir), ['^' PAR.structprefs '.*\.nii$'],1);
% if isempty(P)
% disp('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n NO struct file\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!')
% continue;
% end;
% V = spm_vol(P);
% for i=1:size(V,1)
%
% % fprintf('\n%s%30s',repmat(sprintf('\b'),1,30),sprintf('%s%5d/%5d','...reseting T1 ',i,size(P,1)) ) %-#
% fprintf('%30s',sprintf('%s %2d %s%5d/%5d',PAR.subjects{sb},ses,'...reseting T1 ',i,size(P,1)) ) %-#
% M = V(i).mat;
% vox = sqrt(sum(M(1:3,1:3).^2));
% smat=abs(M(1:3,1:3));
% [mv,loc]=max(smat,[],1);
% [mv,locoff]=max(smat,[],2);
% if det(M(1:3,1:3))<0, vox(1) = -vox(1); end;
% orig = (V(i).dim(1:3)+1)/2;
% off = -vox.*orig;
% M = [0 0 0 off(locoff(1))
% 0 0 0 off(locoff(2))
% 0 0 0 off(locoff(3))
% 0 0 0 1];
% M(loc(1),1)=vox(1);
% M(loc(2),2)=vox(2);
% M(loc(3),3)=vox(3);
% M(2,:)=-M(2,:);
%
% spm_get_space(P(i,:),M);
% end;
end
fprintf('%s%30s',repmat(sprintf('\b'),1,30),'...done') %-#
end
|