Matlab script for automatic AC-PC setting in SPM5, 8, or 12

ON 17 Oct 2008, Carlton Chu posted a nice script to SPM mailing list.
Though it doesn’t have any response from the list, I find it very useful, so I introduce the script on my blog.

The purpose of the script is simple: Set AC-PC automatically for you!

Though it takes time (around 5 minutes per subject in my circumstance), It’s so much time-saving for me who have more than 1000 subjects which I need to realign.

If you are interested, try the following script. It works for SPM5 and later (SPM8 or 12).

In the original post, function which calls select files window was commented out. I uncommented it and it runs without any problems.


function auto_reorient(p)
spmDir=which('spm');
spmDir=spmDir(1:end-5);
tmpl=[spmDir 'canonical/avg152T1.nii'];
vg=spm_vol(tmpl);
flags.regtype='rigid';
p=spm_select(inf,'image');
for i=1:size(p,1)
f=strtrim(p(i,:));
spm_smooth(f,'temp.nii',[12 12 12]);
vf=spm_vol('temp.nii');
[M,scal] = spm_affreg(vg,vf,flags);
M3=M(1:3,1:3);
[u s v]=svd(M3);
M3=u*v';
M(1:3,1:3)=M3;
N=nifti(f);
N.mat=M*N.mat;
create(N);
end

Usage: Download the file below, put the file into the SPM5, 8, or 12 directory, and run “auto_reorient” from Matlab window.

auto_reorient.m