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

Note on Bounding Box

Here I have an MRI data with the following parameters;


Vox dim: 91 109 91
Vox size: 2x2x2 mm
Origin: 46 64 37

Now I want to change the image into this;


Vox dim: 157 189 156
Vox size: 1x1x1 mm
Origin: 79 113 71

How can we set the Bounding Box?
As a result of trial and error, I got this.


[-78 -112 -70; 78 76 85]

Simple way to calculate this is the following;


Vox dim – Origin = positive value.

  • 157-79=78 <– positive value in X
  • 189-113=76 <– positive value in Y
  • 156-71=85 <– positive value in Z


-(Vox dim – positive value -1) = negative value

  • -(157-78-1)=-78 <– negative value in X
  • -(189-76-1)=-112 <– negative value in Y
  • -(156-85-1)=-70 <– negative value in Z

誰も教えてくれないBounding Boxの話

画像解析をするとBounding Boxという言葉にぶつかります。 Bounding Boxは何か?と調べると、

画像データをResliceするときの範囲。

といった表現は見つかりますが、それ以外に見つかりません。 さらに、SPMなどでは、Bounding Boxの設定の項目を見ると、

-90:90 -126:90 -72:108

なんて表現にぶつかる一方で、

-90 -126 -72; 90 90 108

なんて表現にもぶつかったりします。

で、いろいろ考えてみたらたいしたことはありませんでした。Bounding Boxとは、画像の中心を[0, 0, 0]と仮定したとき、画像がx, y, z軸方向にどれだけあるのかという情報のようです。

つまり、-90:90 -126:90 -72:108は、

x軸は中心から左右ともに90まで、y軸は前方には90、後方に126、z軸は上方に108、下方に72の範囲で画像がありますよ

という意味になります。ちょっとすっきりしました。

Unbelievable accident

The other day the Web-hosting service provider I rent a server lost all of my data on the server I rent. It’s unbelievable…

It’s no use crying over the accident, so I restart the blog.

I will recover the contents of the blog as much as possible from now on.