MATLAB-SMP12-fMRI-BOLD图像批量预处理脚本
使用SMP12对BOLD图像进行批量预处理
直接上代码了
需要修改的参数有slice_number是扫描层数;TR重复时间;slice_order这里是间隔扫描;refslice1是中心位置的图像,根据slice_order确定;path是总目录,总目录下有应该是多个实验对象单独的文件夹,文件存储格式是分层存储每个被试文件夹里应该有结构像(其文件夹识别名称为MPRAGE)和功能像(其文件夹识别名称为BOLD)
clear
%%
spm('defaults', 'fmri');
spm_jobman('initcfg');
%%----------------------------------------------------------------------------------------------------------------------
% 设置基本参数,这个模块内的参数需要根据实际扫描参数进行修改
% slice_number是扫描层数;TR重复时间;slice_order这里是间隔扫描;refslice1是中心位置的图像,根据slice_order确定
slice_number = 36;
TR = 3.2;
TA = TR-TR\slice_number;
slice_order = [2:2:slice_number 1:2:slice_number];
refslice1 = 36;
%%----------------------------------------------------------------------------------------------------------------------
%%----------------------------------------------------------------------------------------------------------------------
%获取目录下文件夹名Folds
%path是总目录,总目录下有应该是多个实验对象单独的文件夹,文件存储格式是分层存储
path = 'E:\project\brain\test';
%%----------------------------------------------------------------------------------------------------------------------
d = dir(path);
isub = [d(:).isdir]; %# returns logical vector
Folds = {d(isub).name}';
Folds(ismember(Folds,{'.','..'})) = [];
%%
for n = 1:length(Folds)
%1级文件夹
path1=fullfile(path,Folds{n});
d1 = dir(path1);
isub1 = [d1(:).isdir]; %# returns logical vector
Folds1 = {d1(isub1).name}';
Folds1(ismember(Folds1,{'.','..'})) = [];
%2级文件夹
path2=fullfile(path1,Folds1{1});
d2 = dir(path2);
isub1 = [d2(:).isdir]; %# returns logical vector
Folds2 = {d2(isub1).name}';
Folds2(ismember(Folds2,{'.','..'})) = [];
%%----------------------------------------------------------------------------------------------------------------------
%正则匹配namebold,namet1wi
%每个被试文件夹里应该有结构像(其文件夹识别名称为MPRAGE)和功能像(其文件夹识别名称为BOLD)
fbold = Folds2(~cellfun('isempty',regexp(Folds2, 'BOLD', 'match')));
namebold = fbold{1};
st1wi = Folds2(~cellfun('isempty',regexp(Folds2, 'MPRAGE', 'match')));
namet1wi = st1wi{1};
bold = fullfile(path2,namebold);
t1wi = fullfile(path2,namet1wi);
%%----------------------------------------------------------------------------------------------------------------------
clear matlabbatch
bold_list = spm_select('FPList', bold, '/*.IMA');
bold_list = cellstr(bold_list);
t1wi_list = spm_select('FPList', t1wi, '/*.IMA');
t1wi_list = cellstr(t1wi_list);
%%
matlabbatch{1}.spm.util.import.dicom.data = bold_list;%bold_list
matlabbatch{1}.spm.util.import.dicom.root = 'series';
matlabbatch{1}.spm.util.import.dicom.outdir = {path1};%path1
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;
%%
matlabbatch{2}.spm.util.import.dicom.data = t1wi_list;%t1wi_list
matlabbatch{2}.spm.util.import.dicom.root = 'series';
matlabbatch{2}.spm.util.import.dicom.outdir = {path1};%path1
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;
%%
matlabbatch{3}.spm.temporal.st.scans{1}(1) = cfg_dep('DICOM Import: Converted Images', substruct('.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}), substruct('.','files'));
matlabbatch{3}.spm.temporal.st.nslices = slice_number; %slice_number
matlabbatch{3}.spm.temporal.st.tr = TR; %TR
matlabbatch{3}.spm.temporal.st.ta = TA; %TA
matlabbatch{3}.spm.temporal.st.so = slice_order; %slice_order
matlabbatch{3}.spm.temporal.st.refslice = refslice1; %refslice1
matlabbatch{3}.spm.temporal.st.prefix = 'a';
%%
matlabbatch{4}.spm.spatial.realign.estwrite.data{1}(1) = cfg_dep('Slice Timing: Slice Timing Corr. Images (Sess 1)', substruct('.','val', '{}',{3}, '.','val', '{}',{1}, '.','val', '{}',{1}), substruct('()',{1}, '.','files'));
matlabbatch{4}.spm.spatial.realign.estwrite.eoptions.quality = 0.9;
matlabbatch{4}.spm.spatial.realign.estwrite.eoptions.sep = 4;
matlabbatch{4}.spm.spatial.realign.estwrite.eoptions.fwhm = 5;
matlabbatch{4}.spm.spatial.realign.estwrite.eoptions.rtm = 1;
matlabbatch{4}.spm.spatial.realign.estwrite.eoptions.interp = 2;
matlabbatch{4}.spm.spatial.realign.estwrite.eoptions.wrap = [0 0 0];
matlabbatch{4}.spm.spatial.realign.estwrite.eoptions.weight = '';
matlabbatch{4}.spm.spatial.realign.estwrite.roptions.which = [2 1];
matlabbatch{4}.spm.spatial.realign.estwrite.roptions.interp = 4;
matlabbatch{4}.spm.spatial.realign.estwrite.roptions.wrap = [0 0 0];
matlabbatch{4}.spm.spatial.realign.estwrite.roptions.mask = 1;
matlabbatch{4}.spm.spatial.realign.estwrite.roptions.prefix = 'r';
%%
matlabbatch{5}.spm.spatial.coreg.estimate.ref(1) = cfg_dep('Realign: Estimate & Reslice: Mean Image', substruct('.','val', '{}',{4}, '.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}), substruct('.','rmean'));
matlabbatch{5}.spm.spatial.coreg.estimate.source(1) = cfg_dep('DICOM Import: Converted Images', substruct('.','val', '{}',{2}, '.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}), substruct('.','files'));
matlabbatch{5}.spm.spatial.coreg.estimate.other = {''};
matlabbatch{5}.spm.spatial.coreg.estimate.eoptions.cost_fun = 'nmi';
matlabbatch{5}.spm.spatial.coreg.estimate.eoptions.sep = [4 2];
matlabbatch{5}.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{5}.spm.spatial.coreg.estimate.eoptions.fwhm = [7 7];
matlabbatch{6}.spm.spatial.preproc.channel.vols(1) = cfg_dep('Coregister: Estimate: Coregistered Images', substruct('.','val', '{}',{5}, '.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}), substruct('.','cfiles'));
matlabbatch{6}.spm.spatial.preproc.channel.biasreg = 0.001;
matlabbatch{6}.spm.spatial.preproc.channel.biasfwhm = 60;
matlabbatch{6}.spm.spatial.preproc.channel.write = [0 1];
matlabbatch{6}.spm.spatial.preproc.tissue(1).tpm = {'D:\MATLAB\R2018b\toolbox\spm12\tpm\TPM.nii,1'};
matlabbatch{6}.spm.spatial.preproc.tissue(1).ngaus = 1;
matlabbatch{6}.spm.spatial.preproc.tissue(1).native = [1 0];
matlabbatch{6}.spm.spatial.preproc.tissue(1).warped = [0 0];
matlabbatch{6}.spm.spatial.preproc.tissue(2).tpm = {'D:\MATLAB\R2018b\toolbox\spm12\tpm\TPM.nii,2'};
matlabbatch{6}.spm.spatial.preproc.tissue(2).ngaus = 1;
matlabbatch{6}.spm.spatial.preproc.tissue(2).native = [1 0];
matlabbatch{6}.spm.spatial.preproc.tissue(2).warped = [0 0];
matlabbatch{6}.spm.spatial.preproc.tissue(3).tpm = {'D:\MATLAB\R2018b\toolbox\spm12\tpm\TPM.nii,3'};
matlabbatch{6}.spm.spatial.preproc.tissue(3).ngaus = 2;
matlabbatch{6}.spm.spatial.preproc.tissue(3).native = [1 0];
matlabbatch{6}.spm.spatial.preproc.tissue(3).warped = [0 0];
matlabbatch{6}.spm.spatial.preproc.tissue(4).tpm = {'D:\MATLAB\R2018b\toolbox\spm12\tpm\TPM.nii,4'};
matlabbatch{6}.spm.spatial.preproc.tissue(4).ngaus = 3;
matlabbatch{6}.spm.spatial.preproc.tissue(4).native = [1 0];
matlabbatch{6}.spm.spatial.preproc.tissue(4).warped = [0 0];
matlabbatch{6}.spm.spatial.preproc.tissue(5).tpm = {'D:\MATLAB\R2018b\toolbox\spm12\tpm\TPM.nii,5'};
matlabbatch{6}.spm.spatial.preproc.tissue(5).ngaus = 4;
matlabbatch{6}.spm.spatial.preproc.tissue(5).native = [1 0];
matlabbatch{6}.spm.spatial.preproc.tissue(5).warped = [0 0];
matlabbatch{6}.spm.spatial.preproc.tissue(6).tpm = {'D:\MATLAB\R2018b\toolbox\spm12\tpm\TPM.nii,6'};
matlabbatch{6}.spm.spatial.preproc.tissue(6).ngaus = 2;
matlabbatch{6}.spm.spatial.preproc.tissue(6).native = [0 0];
matlabbatch{6}.spm.spatial.preproc.tissue(6).warped = [0 0];
matlabbatch{6}.spm.spatial.preproc.warp.mrf = 1;
matlabbatch{6}.spm.spatial.preproc.warp.cleanup = 1;
matlabbatch{6}.spm.spatial.preproc.warp.reg = [0 0.001 0.5 0.05 0.2];
matlabbatch{6}.spm.spatial.preproc.warp.affreg = 'mni';%%
matlabbatch{6}.spm.spatial.preproc.warp.fwhm = 0;
matlabbatch{6}.spm.spatial.preproc.warp.samp = 3;
matlabbatch{6}.spm.spatial.preproc.warp.write = [0 1];
matlabbatch{6}.spm.spatial.preproc.warp.vox = NaN;
matlabbatch{6}.spm.spatial.preproc.warp.bb = [NaN NaN NaN
NaN NaN NaN];
%%
matlabbatch{7}.spm.spatial.normalise.write.subj.def(1) = cfg_dep('Segment: Forward Deformations', substruct('.','val', '{}',{6}, '.','val', '{}',{1}, '.','val', '{}',{1}), substruct('.','fordef', '()',{':'}));
matlabbatch{7}.spm.spatial.normalise.write.subj.resample(1) = cfg_dep('Realign: Estimate & Reslice: Realigned Images (Sess 1)', substruct('.','val', '{}',{4}, '.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}), substruct('.','sess', '()',{1}, '.','cfiles'));
matlabbatch{7}.spm.spatial.normalise.write.woptions.bb = [-78 -112 -70
78 76 85];
matlabbatch{7}.spm.spatial.normalise.write.woptions.vox = [3 3 3];%%%333
matlabbatch{7}.spm.spatial.normalise.write.woptions.interp = 4;
matlabbatch{7}.spm.spatial.normalise.write.woptions.prefix = 'w';
%%
matlabbatch{8}.spm.spatial.normalise.write.subj.def(1) = cfg_dep('Segment: Forward Deformations', substruct('.','val', '{}',{6}, '.','val', '{}',{1}, '.','val', '{}',{1}), substruct('.','fordef', '()',{':'}));
matlabbatch{8}.spm.spatial.normalise.write.subj.resample(1) = cfg_dep('Segment: Bias Corrected (1)', substruct('.','val', '{}',{6}, '.','val', '{}',{1}, '.','val', '{}',{1}), substruct('.','channel', '()',{1}, '.','biascorr', '()',{':'}));
matlabbatch{8}.spm.spatial.normalise.write.woptions.bb = [-78 -112 -70
78 76 85];
matlabbatch{8}.spm.spatial.normalise.write.woptions.vox = [1 1 1]; %%%111
matlabbatch{8}.spm.spatial.normalise.write.woptions.interp = 4;
matlabbatch{8}.spm.spatial.normalise.write.woptions.prefix = strcat(Folds{n}, 'sw');
%%
matlabbatch{9}.spm.spatial.smooth.data(1) = cfg_dep('Normalise: Write: Normalised Images (Subj 1)', substruct('.','val', '{}',{7}, '.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}), substruct('()',{1}, '.','files'));
matlabbatch{9}.spm.spatial.smooth.fwhm = [6 6 6]; %%%666
matlabbatch{9}.spm.spatial.smooth.dtype = 0;
matlabbatch{9}.spm.spatial.smooth.im = 0;
matlabbatch{9}.spm.spatial.smooth.prefix = strcat(Folds{n}, 's');
%%
spm_jobman('run', matlabbatch);
end