SPM12から搭載された灰白質、白質、脳脊髄液容積算出機能

SPM12では、灰白質(GM)、白質(WM)、脳脊髄液(CSF)容積もプラグインなしで算出できるようになりました。

まず、segmentationを行います。そうすると、seg8.matというファイルができます。これには、MRIをGM, WM, CSFにどのように分割したかという情報が入っています。
SPM12ではこれを用いて、これらの容積を求めることができます。

SPMのBatch editorを起動し、メニューから、SPM -> Util -> Tissue Volumesと選択します。

tissue_volumes1

そうすると、下図のような内容が出てきます。

tissue_volumes2

Segmentation mat-files: ここにseg8.matを指定します。
Maximum tissue class, Mask image: これはともにデフォルトのままでOKです。
Output file: 適当な名前をつけましょう。私はTissue_volumesという名前をいつもつけています。

これで実行すると、Matlab上に

Running 'Tissue Volumes'

Segmentation files:
	/home/kiyotaka/img_data/ID001_seg8.mat
	/home/kiyotaka/img_data/ID002_seg8.mat
	/home/kiyotaka/img_data/ID003_seg8.mat

Volumes (litres):
    0.8050    0.5908    0.3279
    0.8012    0.5897    0.3340
    0.7893    0.5739    0.3914

Done    'Tissue Volumes'
Done

といった内容が出力され、ワーキングディレクトリにTissue_volumes.csvというファイルも作られています。

一目瞭然ですが、左から順にGM, WM, CSFがリットルで表示されています。
これをすべて足せばICVになるわけですね。便利ですね。

SPM12から搭載された解剖学的名称の同定

この内容は、以前、東大の小池先生から教えていただいた内容です。
まとめるのが遅くなりましたが、まとめてみました。

すぐできるVBMでは、Anatomy Toolboxを使って結果の解剖学的位置の同定を行いました。執筆時点では知らなかったのですが、SPM12から、プラグインを導入しなくても、解剖学的位置を簡単に知ることができるようになりました。

まずは、SPMで結果を出します。

その時、SPMの左下のウィンドウに、下図に示すように、Atlasというメニューが新しく出るようになりました。これを選んで、Label using -> Neuromorphometricsと選択します。

labels1

そうすると、右のSPMの座標が表示されている表の、座標のところを右クリックすると、その座標がどのくらいの確率でどの場所かということが表示されます。
今の場合、座標 [0 26 40] は、21.4%の確率で、左上前頭回内側面、18.0%の確率で右上前頭回内側面、16.2%の確率で左補足運動野ということがわかります。(x軸の座標が0なので、左右どちらもあります。例があまりよくなかったですね…)

labels2

さらに、これは、結果の表だけでなく、Sectionなどで画像を表示したときも利用することができます。

Overlays -> Sections で、画像を表示した後に、画像の上で右クリックをするとメニューが出てきます。

そこで、Display -> Labels -> Neuromorphometricsと選択していきます。

labels3

そうすると、画面にその座標が表示されるようになります。

ちなみに、同じDisplayの中にあるcoordinateを選ぶと座標が表示されますし、Fileを選択すると、背景に使った画像のファイル名が表示されます。

SPMの結果の上での右クリックはバージョンがあがるにつれ、様々な機能が追加されていますので、時間があるときに試してみると発見があります。

ということで、小池先生、遅ればせながらありがとうございました。

fslmathsでproportional scalingをするワンライナー

Proportional scalingは割り算するだけなので大変ではありません。
スクリプトを書くまでもなく、ワンライナーでいけないかと思いました。
次のような感じでばっちりいけました。

必要なものはファイル名となんらかのglobalが記載されているテキストファイルです。ここでは、id_volume.txtとします。

$ cat id_volume.txt

filename1 1.34
filename2 1.43
filename3 1.36
...
filename4 1.47

それでは、本題です。

$ cat id_volume.txt | while read line; \
do id=`echo $line | awk '{ print $1 }'`; \
icv=`echo $line | awk '{ print $2 }'`; \
echo "ID=${id} and ICV=${icv}"; \
fslmaths ${id} -div ${icv} p${id}; done

これで、ファイル名の前にpがついたファイルができます。

Packages to install to run MITK-2015.05.2 on Ubuntu 14.04

MITK-2015.05.2 depends on Qt5, which makes users to install some additional packages.
With trials and errors, below is what you need to do to run MITK-2015.05.2 on Ubuntu 14.04.

  1. build-essential
  2. You need this to run Qt5

    $ sudo apt-get install build-essential
    
  3. Core of Qt5
  4. $ sudo apt-get install qt5-default qttools5-dev-tools
    

    With this command bunch of software packages will be installed on your system.

  5. Two more libraries…
  6. You need two more libraries to run MITK-2015.05.2

    sudo apt-get install libqt5webkit5 libqt5svg5
    
  7. Set environment variable
  8. As the official site points out, you need to set envirament variable as below (when you use bash). I put this in ~/.bashrc

    export QT_PLUGIN_PATH=/usr/lib/x86_64-linux-gnu/qt5/plugins 
    

Now it works!

画像ファイル振り分けスクリプト

画像解析を行っている人ならば、一度は画像の振り分け作業に悩まされたことがあると思います。
私ももれなくその一人で、最近、あるプロジェクトにおいて、画像を分けなければならずしばらく放置していました。

しかし、よく考えてみたら、こういう「めんどくさい」けれども「単純な作業」こそ、コンピューターにさせるべき作業なわけです。

そこで、以下のようなスクリプトを考えてみました。コメントで何をしているか説明します。

#!/bin/sh

#filesorter.sh
#sort files according to the sorting list
#Usage: filesorter.sh sorting_list
#You need to prepare a sorting list beforehand.
#It should be like the following.
# img001  HC
# img002  PT
# img003  HC
# img004  PT
# img005  PT

#引数が指定されているかチェック
if [ $# -ne 1 ]; then
	echo "A sorting file should be specified!"
	echo "Usage: $0 sorting_file"
	echo "Sorting file includes ID and Categorized Group"
	echo "example:"
	echo "img001  HC"
	echo "img002  PT"
	echo "img003  HC"
	echo "img004  PT"
	echo "img005  PT"

	exit 1
fi

#ファイルを読み込み、空白行を削除し、1行ずつ$lineにセット
cat $1 | sed '/^$/d' | while read line
do
    #AWKを使って、第1フィールドを変数idにセット
    #第2フィールドを変数categoryにセット
    id=`echo $line | awk '{ print $1 }'`
    category=`echo $line | awk '{ print $2 }'`
	
    #変数categoryの名前に相当するディレクトリがなければ作成
    if [ ! -e $category ]; then
        mkdir $category
    fi

    #変数idを含むファイルを先ほど作成したディレクトリにコピー
    echo "copy $id to $category"
    cp *${id}* $category
done

興味のある方は、こちらからダウンロードしてください。

ANTsのmacOSへのインストール方法(ソースからのコンパイル)

ANTs (Advanced Normalization Tools)は、近年注目されている脳画像解析ツールです。性能が高いのですが、導入方法などが親切に説明されていません。macOSへのインストール方法を説明します。

2021.01.02: Homebrewを使えばcmakeが入れられるので、そちらに情報を更新しました。また、インストールスクリプトを公開しました。
こちら(右クリックで保存)からダウンロードしてください。Downloadsフォルダにダウンロードしたと仮定しますと、以下でインストールできます。コンパイルにしばらく時間がかかりますので30分くらいかかると思ってください。

cd ~/Downloads
chmod 755 ANTs_installer_macOS.sh
./ANTs_installer_macOS.sh

開発者によるHow-toはこちらに記載されています。

以下、詳しい説明です。

続きを読む

MRIcronのMacOSへのインストール方法

*23 Jan 2021: MRIcronのインストーラーが変更になったこと、MRIconにdcm2niixが含まれるようになったことから、全面改訂を行いました。

MRIcron自体のインストールは難しくないのですが、ターミナルからdcm2niixを使用するためには少し工夫が必要ですので、それを記載します。なお、シェルは bash という前提でいきます。zshを利用の方は、chsh -s /bin/bash で bash に変更できます。(再起動が必要です)

続きを読む

脳画像解析ミニ勉強会 in 神戸 (2015年9月6日)

(25/Aug/2015:参加を締め切りました)

2015年9月に神戸で脳画像解析のミニ勉強会を開催します。

ふだんは、私はSPM-VBMを中心にしていますが、

今回は、”脳画像解析Tips -コマンドラインは怖くない-“と題して、

主にdcm2niiとFSLを用いて、コマンドラインを使った処理について学べたらと思います。
GUIでなく、ターミナル内で作業をすることにおっかなびっくりな方々が対象です。
既にコマンドラインを使いこなしている方は学ぶことはほとんどないです。
冷やかしがてら、協力してくださる方々の参加は歓迎です。

続きを読む

A Matlab script to generate ROI masks using an Atlas in SPM12

SPM12 introduces some useful functions such as spm_atlas or new atlas “labels_Neuromorphometrics.” We find the description about labels_Neuromorphometrics in SPM12 Release note.

Maximum probability tissue labels derived from the “MICCAI 2012 Grand Challenge and Workshop on Multi-Atlas Labeling” are available in files tpm/labels Neuromorphometrics.{nii,xml}. These
data were released under the Creative Commons Attribution-NonCommercial (CC BY-NC) with no end date. Users should credit the MRI scans as originating from the OASIS project and the labeled
data as “provided by Neuromorphometrics, Inc. under academic subscription”. These references should be included in all workshop and final publications. See spm templates.man for more details about the generation of this file.

I wanted to generate masks of some regions using this labels_Neuromorphometrics.

Below is the tiny script which generates masks from your preferred atlas.
Running script brings up a file selector. You can choose any atlas you want.
Then it brings up another dialog which lists the region within the atlas. You can choose as many regions as you want, and the scripts generates masks whose file name is the name of the regions.


%generate_masks_from_atlas.m
%This script generate mask files from any atlases you prefer.
%K. Nemoto 25 April 2015

xA=spm_atlas('load');
S=spm_atlas('select',xA);

for i = 1:size(S,2)
    fname=strcat(S{i},'.nii');
    VM=spm_atlas('mask',xA,S{i});
    VM.fname=fname;
    spm_write_vol(VM,spm_read_vols(VM));
end

Download generate_masks_from_atlas.m (right click and save as)

A Matlab script to set origin to the center of the image using SPM

Sometimes we need to handle images whose origin is far from the center of the image.
My friend, Fumio Yamashita, from Iwate Medical University wrote a script which set the origin to the center of the image.

Usage is very simple.

    Download

  1. setorigin_center.m (Right click and save as…) and save in SPM directory or any directories under MATLAB path.
  2. Type the following in Matlab command window.
  3. >>setorigin_center
  4. This will pop up the file selector, so choose the images you want to set the origin to the center.

That’s it.

Below is the example of the script.

Left is before applying the script. You can see that the origin is far from the center.
Right is after applying the script. Now origin is set to the center.

setorigin_screenshot

Ubuntu 14.04 へのFreeSurferのインストール

2017.10.04 本記事は古い物です。最新版はこちらになりますので、そちらをご参照ください。

以前、FreeSurferのインストールの方法を記載しましたが、しばらく時間が経ちました。最近、改めてFreeSurferをインストールする機会がありましたので、改めてまとめてみます。2014年12月現在、FreeSurferのバージョンは、5.3.0です。コピペでもいけるぐらいにまとめてみます。

英語でのFreesurferをLinuxにインストールするときの具体的な方法は、
http://surfer.nmr.mgh.harvard.edu/fswiki/LinuxInstall
に書いてあります。以下は英語を読むのがつらい方のためです。

続きを読む

2014年12月包括脳VBMチュートリアル配布資料(VBMの実践)

2014年12月13日に包括脳MRI脳画像解析チュートリアルが開催されました。
私はVBMチュートリアルを担当しましたが、その時の配布資料を公開します。

このPDFは、「すぐできるVBM」の補足のような位置づけにあります。
すぐできるVBMのサンプルデータを使っていますし、チュートリアルを受講していない方々でも、このPDFを参考にしていただければ、さらなる知識が得られるのではないかと思います。

ダウンロードはこちらからどうぞ。

SPM8とSPM12をよりスマートに同一のMatlabのライセンスで使用する方法

しばらく前に、SPM8とSPM12を同一のMatlabのライセンスで使用する方法という記事を書きましたが、先日の包括脳チュートリアルのアンケートで、「SPM8とSPM12を併存する方法がわかりにくい」というご指摘をいただきました。

ということで、ひとつのスクリプトで全部解決できる方法を考えてみました。
奇しくも、SPM-MLで同じような話題があり、私もその話題に加わったもので、その結果を受けています。

続きを読む

Mac OSX YosemiteにアップグレードしたらFSLが動かなくなった場合の対処法

私自身も遭遇しましたし、他の方からも問い合わせがありましたので、簡単に記載しておきます。

MacをYosemiteにアップグレードした際に、FSLが動作しなくなることがあります。
その際には、バタバタする前に、まず、XQuartzを再インストールしてみてください。
それだけで問題が解決することがあります。

本家にも情報がありました。

Note for Yosemite (10.10) users: even if you have previously installed X11, you will need to reinstall it from the XQuartz web page to get FSL to work.

これで、問題解決といって大丈夫そうです。

Scripts to convert hdr/img format into nii format

Nifti file havs two kinds of extensions, hdr/img or nii.
Sometimes hdr/file nifti files can be confusing because the extension is the same as ANALYZE format.
There are some ways to convert hdr/img into nifti.
One is to use fslchfiletype included in FSL. This is the easiest, but it requires FSL.
The other is to use SPM. However, some scripting is needed.
So I wrote tiny scripts for converting hdr/img format into nii format.

If you prefer SPM, use img2nii_spm.m. Save the script into a folder under MATLAB path and type “img2nii_spm” from MATLAB command window.

%%img2nii.m--------------------------------------------
%Script to convert hdr/img files to nii.
%This script uses SPM function, so you need to install SPM5 or later.
%Kiyotaka Nemoto 05-Nov-2014

%select files
f = spm_select(Inf,'img$','Select img files to be converted');

%convert img files to nii
for i=1:size(f,1)
	input = deblank(f(i,:));
	[pathstr,fname,ext] = fileparts(input);
    output = strcat(fname,'.nii');
    V=spm_vol(input);
    ima=spm_read_vols(V);
    V.fname=output;
    spm_write_vol(V,ima);
end

If you prefer FSL, use img2nii_fsl.sh. Save the script into a folder, add executable attribute using “chmod 755 img2nii_fsl.sh”.
Wild card can be used for selecting files.

#!/bin/sh

#Image file type converter using fslchfiletype
#This script converts various image files into NIFTI format (.nii) files.
#K. Nemoto 19 Jan 2013

if [ $# -lt 1 ] ; then
	echo "Please specify the files you want to convert!"
	echo "Usage: $0 filename"
	exit 1
fi

for file in "$@" ; do
	if [ -f $file ] ; then
		fslchfiletype NIFTI $file
	else
		echo "$file: No such file"
	fi
done

Download img2nii_spm.m (Right-click and save).
Download img2nii_fsl.sh (Right-click and save).

SPM12におけるspm_defaults.mの設定 【重要】

SPM12の正式版が出て、気づいたのですが、spm_defaults.mの設定に関して、重要な変更が行われました。

今後は、spm_defaults.mは直接いじってはいけないことになりました。
これは、非常に理にかなっていることで、これまで、spm_defaults.mを修正しても、アップデートによってこれが上書きされてしまうことがありました。これを回避するために、新たな方法が導入されました。

「すぐできるVBM」のp.22 「8.SPMの設定」に関しての記載を以下のように訂正します。

続きを読む

包括脳MRIチュートリアルが12月13日に開催されます

毎年開催している包括脳MRIチュートリアルですが、
今年は12月13日(土)に開催されることになりました。

詳しい案内はこちらをご覧ください。

VBMチュートリアルに関しては、席はあと20席程度残っているとのことです。

今回は、

・画像のQC(岩手医大山下先生)
・多重比較補正についての理解(京大川口先生)
・SPMの結果を論文に載せるまでの具体的手順

といった内容を計画しています。

関心のある方は、こちらにある申し込み方法をご覧のうえ、ご応募ください。