医学影像批量配准,matlab 代码
配准后的文件与配准前的文件保存在统一目录下,命名以‘r’开头。
%-----------------------------------------------------------------------
% Job saved on 21-Nov-2019 15:35:09 by cfg_util (rev $Rev: 7345 $)
% spm SPM - SPM12 (7487)
% cfg_basicio BasicIO - Unknown
% 用于批量配准
%-----------------------------------------------------------------------
clear
spm('defaults', 'fmri');
spm_jobman('initcfg');
subjectsdir = {'E:\nii_path'}; % 这里是data文件夹的绝对位置
path = 'E:\nii_path';
subjects = dir(path);
subjects = {subjects.name}; % 单个或多个被试的文件夹
subjects(:,[1,2]) = [];
funcdir = fullfile('T1C'); % reference的文件夹,向哪个序列配准,哪个是reference
anatdir = fullfile('T1'); % source的文件夹,被配准的序列
nsubj = length(subjects); % 配准的病例数
for csubj = 1:nsubj
subjdir = {spm_select('CPath', subjects{csubj}, subjectsdir)};
% source file
adir = spm_select('CPath', anatdir, subjdir); % 选择当前source文件夹
anatfile = spm_select('FPList', adir, '/*.nii'); % 该文件夹中.nii结尾的文件
if size(anatfile, 1) == 0
sprintf('No anat file found for %s',subjects{csubj})
continue;
end
% reference file
fdir = {spm_select('CPath', funcdir, subjdir)}; % 选择当前reference文件夹
ffiles = spm_select('FPList', fdir, '/*.nii'); % 该文件夹中.nii结尾的文件
if size(ffiles,1) == 0
sprintf('No source file found for %s', subjects{csubj})
continue;
end
% Coregister
n_nii = size(anatfile,1); % nii文件个数,被配准的序列可能不止一个nii
for n = 1:n_nii
clear matlabbatch
% 以下spm12默认设置
matlabbatch{1}.spm.spatial.coreg.estwrite.ref = {ffiles};
matlabbatch{1}.spm.spatial.coreg.estwrite.source = {anatfile(n,:)};
matlabbatch{1}.spm.spatial.coreg.estwrite.other = {''};
matlabbatch{1}.spm.spatial.coreg.estwrite.roptions.interp = 4;
matlabbatch{1}.spm.spatial.coreg.estwrite.roptions.wrap = [0 0 0];
matlabbatch{1}.spm.spatial.coreg.estwrite.roptions.mask = 0;
matlabbatch{1}.spm.spatial.coreg.estwrite.roptions.prefix = 'r';
% 运行
spm_jobman('run', matlabbatch);
end
end