前期准备:在MATLAB中安装SPM
下载SPM安装包并解压:SPM12 Software - Statistical Parametric Mapping
点击设置路径→添加并包含子文件夹...→找到刚才存放spm12的文件夹→选择文件夹→保存→关闭。
然后在matlab命令窗口中输入SPM即可启动,然后选择fMRI,也可以直接输入SPM fMRI,如果成功启动,说明安装成功
二、SPM单个被试预处理
略
三、使用spm Batch 功能对核磁数据进行预处理
前几个tr存在匀场的过程(每个序列开始前的空扫时间,然后除以tr时间都得到tr数),在数据处理时需要把这部分内容去掉 ,去掉时需要确保是最开始的前几个tr,可以根据tr的名字进行辨别。
搭建batch框架
1.三个dicom import (t1加flanker1,flanker2)
二、时间矫正
点dependency选择数据,
Siemens磁共振ascending interleaved扫描特殊的地方,
如果总层数为偶数(如32层), 就从第2层开始, 接下来4、6、8…32,1、3、5…31, 此时slice order为[2:2:32 1:2:31]
如果总层数为奇数(如33层), 就从第1层开始, 接下来3、5、7…33,2、4、6…32, 此时slice order为[1:2:33 2:2:32]
其他扫描仪不存在这样的问题,都从第1层开始,32层:[1:2:31 2:2:32],33层:[1:2:33 2:2:32]
三、头动矫正
不要超过3mm
四、结构像与功能像对齐
五,分割
Segment将大脑图像分割为包括灰质、白质、脑脊液等结构
同时还可以生成参数文件,帮助接下来大脑空间标准化
六,标准化
voxel size改为[3 3 3]
中国人被试的bounding box建议[-90 -126 -72;90 90 108]
七、平滑
四、批处理核磁数据
为每个需要处理的被试建立输出文件夹
batch editor 窗口点击view,选择show.mcode,然后改一改代码
%-----------------------------------------------------------------------
% Job saved on 09-Aug-2025 18:04:43 by cfg_util (rev $Rev: 7345 $)
% spm SPM - SPM12 (7771)
% cfg_basicio BasicIO - Unknown
parentdir = 'D:\学习\批处理'; % 定义储存各被试源文件的上级文件夹
cd(parentdir); % 进入这个上级文件夹
allsubjects = dir('sub*');%查找该文件夹下的所有被试
numsubjects = numel(allsubjects); % 数找到了多少被试
for i=1:numsubjects % 对每个被试进行循环
cursubject = allsubjects(i).name; % 找到当前被试的名字
matlabbatch=cell(1);
%% 查找结构像
% 查找第i个被试的anat文件夹的绝对路径
curanat = fullfile(parentdir,cursubject,'anat');
% 查找第i个被试的anat下的所有dcm文件
alldcms = dir(fullfile(curanat,'*.dcm'));
% 再把alldcms中每个文件添加上绝对路径,这样才能让spm的batch找到文件
alldcms = fullfile(curanat,{alldcms.name}');
matlabbatch{1}.spm.util.import.dicom.data = alldcms; % 把刚刚结构像的文件告诉给matlabbatch{1}.spm.util.import.dicom.data
% 接下来指定anat的输出文件夹
curanatout=fullfile('D:\学习\批处理\output',cursubject,'anat');
if ~exist(curanatout,'dir')
mkdir(curanatout);
end
%%
%%
matlabbatch{1}.spm.util.import.dicom.root = 'flat';
matlabbatch{1}.spm.util.import.dicom.outdir = {curanatout};
matlabbatch{1}.spm.util.import.dicom.protfilter = '.*';
matlabbatch{1}.spm.util.import.dicom.convopts.format = 'nii';
matlabbatch{1}.spm.util.import.dicom.convopts.meta = 0;
matlabbatch{1}.spm.util.import.dicom.convopts.icedims = 0;
%%
%% 查找flanker1
% 查找第i个被试的flanker1文件夹的绝对路径
curflanker1 = fullfile(parentdir,cursubject,'flanker1');
% 查找第i个被试的anat下的所有dcm文件
alldcms = dir(fullfile(curflanker1,'*.dcm'));
% 再把alldcms中每个文件添加上绝对路径,这样才能让spm的batch找到文件
alldcms = fullfile(curflanker1,{alldcms.name}');
matlabbatch{2}.spm.util.import.dicom.data = alldcms(5:end,1);
curfucout=fullfile('D:\学习\批处理\output',cursubject,'flanker1');
if ~exist(curfucout,'dir')
mkdir(curfucout);
end
%%
matlabbatch{2}.spm.util.import.dicom.root = 'flat';
matlabbatch{2}.spm.util.import.dicom.outdir = {curfucout};
matlabbatch{2}.spm.util.import.dicom.protfilter = '.*';
matlabbatch{2}.spm.util.import.dicom.convopts.format = 'nii';
matlabbatch{2}.spm.util.import.dicom.convopts.meta = 0;
matlabbatch{2}.spm.util.import.dicom.convopts.icedims = 0;
%%
%% 查找flanker2
% 查找第i个被试的flanker2文件夹的绝对路径
curflanker2 = fullfile(parentdir,cursubject,'flanker2');
% 查找第i个被试的flanker2下的所有dcm文件
alldcms = dir(fullfile(curflanker2,'*.dcm'));
% 再把alldcms中每个文件添加上绝对路径,这样才能让spm的batch找到文件
alldcms = fullfile(curflanker2,{alldcms.name}');
matlabbatch{3}.spm.util.import.dicom.data = alldcms(5:end,1);
%% 接下来指定flanker2的输出文件夹
curfucout=fullfile('D:\学习\批处理\output',cursubject,'flanker2');
if ~exist(curfucout,'dir')
mkdir(curfucout);
end
%%
matlabbatch{3}.spm.util.import.dicom.root = 'flat';
matlabbatch{3}.spm.util.import.dicom.outdir = {curfucout};
matlabbatch{3}.spm.util.import.dicom.protfilter = '.*';
matlabbatch{3}.spm.util.import.dicom.convopts.format = 'nii';
matlabbatch{3}.spm.util.import.dicom.convopts.meta = 0;
matlabbatch{3}.spm.util.import.dicom.convopts.icedims = 0;
matlabbatch{4}.spm.temporal.st.scans{1}(1) = cfg_dep('DICOM Import: Converted Images', substruct('.','val', '{}',{2}, '.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}), substruct('.','files'));
matlabbatch{4}.spm.temporal.st.scans{2}(1) = cfg_dep('DICOM Import: Converted Images', substruct('.','val', '{}',{3}, '.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}), substruct('.','files'));
matlabbatch{4}.spm.temporal.st.nslices = 32;
matlabbatch{4}.spm.temporal.st.tr = 2;
matlabbatch{4}.spm.temporal.st.ta = 1.9375;
matlabbatch{4}.spm.temporal.st.so = [2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31];
matlabbatch{4}.spm.temporal.st.refslice = 32;
matlabbatch{4}.spm.temporal.st.prefix = 'a';
matlabbatch{5}.spm.spatial.realign.estwrite.data{1}(1) = cfg_dep('Slice Timing: Slice Timing Corr. Images (Sess 1)', substruct('.','val', '{}',{4}, '.','val', '{}',{1}, '.','val', '{}',{1}), substruct('()',{1}, '.','files'));
matlabbatch{5}.spm.spatial.realign.estwrite.data{2}(1) = cfg_dep('Slice Timing: Slice Timing Corr. Images (Sess 2)', substruct('.','val', '{}',{4}, '.','val', '{}',{1}, '.','val', '{}',{1}), substruct('()',{2}, '.','files'));
matlabbatch{5}.spm.spatial.realign.estwrite.eoptions.quality = 0.9;
matlabbatch{5}.spm.spatial.realign.estwrite.eoptions.sep = 4;
matlabbatch{5}.spm.spatial.realign.estwrite.eoptions.fwhm = 5;
matlabbatch{5}.spm.spatial.realign.estwrite.eoptions.rtm = 1;
matlabbatch{5}.spm.spatial.realign.estwrite.eoptions.interp = 2;
matlabbatch{5}.spm.spatial.realign.estwrite.eoptions.wrap = [0 0 0];
matlabbatch{5}.spm.spatial.realign.estwrite.eoptions.weight = '';
matlabbatch{5}.spm.spatial.realign.estwrite.roptions.which = [2 1];
matlabbatch{5}.spm.spatial.realign.estwrite.roptions.interp = 4;
matlabbatch{5}.spm.spatial.realign.estwrite.roptions.wrap = [0 0 0];
matlabbatch{5}.spm.spatial.realign.estwrite.roptions.mask = 1;
matlabbatch{5}.spm.spatial.realign.estwrite.roptions.prefix = 'r';
matlabbatch{6}.spm.spatial.coreg.estimate.ref(1) = cfg_dep('Realign: Estimate & Reslice: Mean Image', substruct('.','val', '{}',{5}, '.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}), substruct('.','rmean'));
matlabbatch{6}.spm.spatial.coreg.estimate.source(1) = cfg_dep('DICOM Import: Converted Images', substruct('.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}), substruct('.','files'));
matlabbatch{6}.spm.spatial.coreg.estimate.other = {''};
matlabbatch{6}.spm.spatial.coreg.estimate.eoptions.cost_fun = 'nmi';
matlabbatch{6}.spm.spatial.coreg.estimate.eoptions.sep = [4 2];
matlabbatch{6}.spm.spatial.coreg.estimate.eoptions.tol = [0.02 0.02 0.02 0.001 0.001 0.001 0.01 0.01 0.01 0.001 0.001 0.001];
matlabbatch{6}.spm.spatial.coreg.estimate.eoptions.fwhm = [7 7];
matlabbatch{7}.spm.spatial.preproc.channel.vols(1) = cfg_dep('Coregister: Estimate: Coregistered Images', substruct('.','val', '{}',{6}, '.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}), substruct('.','cfiles'));
matlabbatch{7}.spm.spatial.preproc.channel.biasreg = 0.001;
matlabbatch{7}.spm.spatial.preproc.channel.biasfwhm = 60;
matlabbatch{7}.spm.spatial.preproc.channel.write = [0 0];
matlabbatch{7}.spm.spatial.preproc.tissue(1).tpm = {'D:\MATLAB\spm12\tpm\TPM.nii,1'};
matlabbatch{7}.spm.spatial.preproc.tissue(1).ngaus = 1;
matlabbatch{7}.spm.spatial.preproc.tissue(1).native = [1 0];
matlabbatch{7}.spm.spatial.preproc.tissue(1).warped = [0 0];
matlabbatch{7}.spm.spatial.preproc.tissue(2).tpm = {'D:\MATLAB\spm12\tpm\TPM.nii,2'};
matlabbatch{7}.spm.spatial.preproc.tissue(2).ngaus = 1;
matlabbatch{7}.spm.spatial.preproc.tissue(2).native = [1 0];
matlabbatch{7}.spm.spatial.preproc.tissue(2).warped = [0 0];
matlabbatch{7}.spm.spatial.preproc.tissue(3).tpm = {'D:\MATLAB\spm12\tpm\TPM.nii,3'};
matlabbatch{7}.spm.spatial.preproc.tissue(3).ngaus = 2;
matlabbatch{7}.spm.spatial.preproc.tissue(3).native = [1 0];
matlabbatch{7}.spm.spatial.preproc.tissue(3).warped = [0 0];
matlabbatch{7}.spm.spatial.preproc.tissue(4).tpm = {'D:\MATLAB\spm12\tpm\TPM.nii,4'};
matlabbatch{7}.spm.spatial.preproc.tissue(4).ngaus = 3;
matlabbatch{7}.spm.spatial.preproc.tissue(4).native = [1 0];
matlabbatch{7}.spm.spatial.preproc.tissue(4).warped = [0 0];
matlabbatch{7}.spm.spatial.preproc.tissue(5).tpm = {'D:\MATLAB\spm12\tpm\TPM.nii,5'};
matlabbatch{7}.spm.spatial.preproc.tissue(5).ngaus = 4;
matlabbatch{7}.spm.spatial.preproc.tissue(5).native = [1 0];
matlabbatch{7}.spm.spatial.preproc.tissue(5).warped = [0 0];
matlabbatch{7}.spm.spatial.preproc.tissue(6).tpm = {'D:\MATLAB\spm12\tpm\TPM.nii,6'};
matlabbatch{7}.spm.spatial.preproc.tissue(6).ngaus = 2;
matlabbatch{7}.spm.spatial.preproc.tissue(6).native = [0 0];
matlabbatch{7}.spm.spatial.preproc.tissue(6).warped = [0 0];
matlabbatch{7}.spm.spatial.preproc.warp.mrf = 1;
matlabbatch{7}.spm.spatial.preproc.warp.cleanup = 1;
matlabbatch{7}.spm.spatial.preproc.warp.reg = [0 0.001 0.5 0.05 0.2];
matlabbatch{7}.spm.spatial.preproc.warp.affreg = 'mni';
matlabbatch{7}.spm.spatial.preproc.warp.fwhm = 0;
matlabbatch{7}.spm.spatial.preproc.warp.samp = 3;
matlabbatch{7}.spm.spatial.preproc.warp.write = [1 1];
matlabbatch{7}.spm.spatial.preproc.warp.vox = NaN;
matlabbatch{7}.spm.spatial.preproc.warp.bb = [NaN NaN NaN
NaN NaN NaN];
matlabbatch{8}.spm.spatial.normalise.write.subj.def(1) = cfg_dep('Segment: Forward Deformations', substruct('.','val', '{}',{7}, '.','val', '{}',{1}, '.','val', '{}',{1}), substruct('.','fordef', '()',{':'}));
matlabbatch{8}.spm.spatial.normalise.write.subj.resample(1) = cfg_dep('Realign: Estimate & Reslice: Resliced Images (Sess 1)', substruct('.','val', '{}',{5}, '.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}), substruct('.','sess', '()',{1}, '.','rfiles'));
matlabbatch{8}.spm.spatial.normalise.write.subj.resample(2) = cfg_dep('Realign: Estimate & Reslice: Resliced Images (Sess 2)', substruct('.','val', '{}',{5}, '.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}), substruct('.','sess', '()',{2}, '.','rfiles'));
matlabbatch{8}.spm.spatial.normalise.write.woptions.bb = [-90 -126 -72
90 90 108];
matlabbatch{8}.spm.spatial.normalise.write.woptions.vox = [3 3 3];
matlabbatch{8}.spm.spatial.normalise.write.woptions.interp = 4;
matlabbatch{8}.spm.spatial.normalise.write.woptions.prefix = 'w';
matlabbatch{9}.spm.spatial.smooth.data(1) = cfg_dep('Normalise: Write: Normalised Images (Subj 1)', substruct('.','val', '{}',{8}, '.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}), substruct('()',{1}, '.','files'));
matlabbatch{9}.spm.spatial.smooth.fwhm = [8 8 8];
matlabbatch{9}.spm.spatial.smooth.dtype = 0;
matlabbatch{9}.spm.spatial.smooth.im = 0;
matlabbatch{9}.spm.spatial.smooth.prefix = 's';
save(fullfile(parentdir,cursubject,'batch.mat'),'matlabbatch');
spm_jobman('run',matlabbatch);
end