SPM12之fMRI批量预处理——DICOM格式转NII格式
运行说明:subjectsdir换成自己的文件夹地址, 有多少个受试者的DICOM就有多少个subjects子元素 。
代码地址:https://github.com/zhoushusheng/fMRI_preprocessing/blob/main/stupid_batch_dicom_formal.m
准备工作:建立好fMRI_DICOM文件夹和MRI_DICOM文件夹,把下载好的数据输入进去
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 | %----------------------------------------------------------------------- % Job saved on 02-Sep-2019 18:21:16 by cfg_util (rev $Rev: 6942 $) % spm SPM - SPM12 (7219) % cfg_basicio BasicIO - Unknown %----------------------------------------------------------------------- %% % 这个m文件用来运行spm的matlabbatch % 该文件中的文件位置是基于linux系统的,如果是windows系统需要修改/为\ clear % spm_path = '~\spm12'; %设置spm12的位置(这里填的是安装=spm12的绝对位置) 我这里已经安装好了 % addpath(spm_path); spm( 'defaults' , 'fmri' ); spm_jobman( 'initcfg' ); %% subjectsdir = { 'B:\fMRI\preprocess_for_batch\CN' }; % 这里是data文件夹的绝对位置 subjects = { 'sbj01' , 'sbj02' , 'sbj03' , 'sbj04' , 'sbj05' , 'sbj06' , 'sbj07' , 'sbj08' , 'sbj09' , 'sbj10' , 'sbj11' , 'sbj12' , 'sbj13' , 'sbj14' , 'sbj15' , 'sbj16' , 'sbj17' , 'sbj18' , 'sbj19' , 'sbj20' , 'sbj21' , 'sbj22' , 'sbj23' , 'sbj24' , 'sbj25' , 'sbj26' , 'sbj27' , 'sbj28' , 'sbj29' , 'sbj30' , 'sbj31' , 'sbj32' , 'sbj33' , 'sbj34' , 'sbj35' , 'sbj36' }; % 单个或多个被试的文件夹 funcdir = fullfile ( 'fMRI_DICOM' ); % 第一个Session的文件夹(由于preprocessing.mat中只添加了一个Session,所以这里也只使用一个Session。) % F = fullfile(FOLDERNAME1, FOLDERNAME2, ..., FILENAME) builds a full % file specification F from the folders and file name specified. Input % arguments FOLDERNAME1, FOLDERNAME2, etc. and FILENAME can be strings, % character vectors, or cell arrays of character vectors. Non-scalar % strings and cell arrays of character vectors must all be the same size. % FULLFILE collapses inner repeated file separators unless they appear at % the beginning of the full file specification. FULLFILE also collapses % relative folders indicated by the dot symbol, unless they appear at % the end of the full file specification. Relative folders indicated % by the double-dot symbol are not collapsed. % % funcdir2 = fullfile('Session2'); % 第二个Session的文件夹 anatdir = fullfile ( 'MRI_DICOM' ); % 结构像的文件夹 nsubj = length (subjects); % 被试的数量 jobs_fMRI = cell (1,1); % job的数量,每个被试一个job jobs_MRI = cell (1,1); nsubj =2; inputs = cell (0,1); nrun=1; % session的数量 %ntimepoint=190; %删除了前面的10个时间点后剩余的时间点 %% for csubj_fMRI = 1:36 subjdir = {spm_select( 'CPath' ,subjects{csubj_fMRI}, subjectsdir)}; %Cpath 的c = canonical % Session1文件夹 fdir = {spm_select( 'CPath' , funcdir, subjdir)}; %选择当前被试(csubj_fMRI)的Session1文件夹 ffiles = spm_select( 'List' , fdir, '\\*.dcm' ); %选择这个文件夹中所有.nii结尾的文件,即Session1的所有原始功能像文件 %如果没有功能像文件就报错 nimage = size (ffiles,1); if nimage == 0 warning ( sprintf ( 'No functional file found for %s' , subjects{csubj_fMRI})) return end cffiles = cellstr (ffiles); ntimepoint = length (cffiles); funcfiles = cell (1, nrun); sessionfiles = cell (nrun,ntimepoint); for i = 1:nrun for j = 1:ntimepoint sessionfiles{ i , j }= strcat (fdir{1},'\',cffiles{ j }); % 在这里在每一个功能像文件的绝对路径后面添加,1 end funcfiles{1, i } = {sessionfiles{ i ,:}}'; % 如果有多个Sessions,funcfiles中就会有多个sessionfiles end clear matlabbatch % preprocessing job display 'Creating preprocessing job' %在matlab的命令行窗口显示Creating preprocessing job goalDirectory = { 'B:\fMRI\preprocess_for_batch\CN' }; %这样生成的cell 没有'' ,没有显示内容的类型 goalDirectory_subj = {spm_select( 'CPath' , subjects{csubj_fMRI} ,goalDirectory)}; % 这样生成的也 没有 goalDirectory_subj = char (goalDirectory_subj); %这样生成的也没有'' 但是会显示类型 if ~ exist (goalDirectory_subj, 'dir' ) % mkdir goalDirectory subjects{csubj_fMRI}%这里十分可恶 goalDirectory 不能识别为变量 goalDirectory = goalDirectory{1}; % mkdir [goalDirectory] subjects{csubj_fMRI} mkdir ([goalDirectory '\' subjects{csubj_fMRI}]); %这就是真理 上matlab 网站找着了 %% else % warning(sprintf('Folder has been created and make sure if files needed has exited', subjects{csubj_fMRI})) % return end goalDirectory_subj = {goalDirectory_subj}; %没有前面一行代码 这样显示的会有'' ,加上前面一行的话 会变成"" goalDirectory_subj_fMRI = {spm_select( 'CPath' , 'fMRI' ,goalDirectory_subj)}; %这个会自动合并 connonical {'fMRI'} 把 {}去了 goalDirectory_subj_fMRI = char (goalDirectory_subj_fMRI); %为什么这里要加1在花括号里?为甚这样就行?可能因为前面的代码,生成的就是一个cell。那更前面不也是嘛,为什么就行?原因出来了在上一行。 if ~ exist (goalDirectory_subj_fMRI, 'dir' ) %经过这里一手 就会把goalDirectory 变成cell下面就不能用了 %string = convertCharsToStrings(goalDirectory_subj_fMRI); goalDirectory_subj = goalDirectory_subj{1}; % mkdir [goalDirectory] subjects{csubj_fMRI} mkdir ([goalDirectory_subj '\' ' fMRI']); %这就是真理 上matlab 网站找着了 else warning ( sprintf ( 'Folder has been created and make sure if files needed has exited' , subjects{csubj_fMRI})) return end matlabbatch{1}.spm.util. import .dicom.data = funcfiles{1,1}(:,1); matlabbatch{1}.spm.util. import .dicom. root = 'flat' ; matlabbatch{1}.spm.util. import .dicom.outdir = {goalDirectory_subj_fMRI}; 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; matfile = sprintf ( 'preprocess_fMRI_%s.mat' , subjects{csubj_fMRI}); % save(matfile,'matlabbatch'); jobs_fMRI{csubj_fMRI} = matfile; %jobs这个变量中存储了所有被试的matlabbatch spm_jobman( 'run' ,matlabbatch); end for csubj_MRI = 1:36 subjdir = {spm_select( 'CPath' ,subjects{csubj_MRI}, subjectsdir)}; %Cpath 的c = canonical % Session1文件夹 fdir = {spm_select( 'CPath' , anatdir, subjdir)}; %选择当前被试(csubj_fMRI)的Session1文件夹 ffiles = spm_select( 'List' , fdir, '\\*.dcm' ); %选择这个文件夹中所有.nii结尾的文件,即Session1的所有原始功能像文件 %如果没有功能像文件就报错 nimage = size (ffiles,1); if nimage == 0 warning ( sprintf ( 'No functional file found for %s' , subjects{csubj_MRI})) return end cffiles = cellstr (ffiles); ntimepoint = length (cffiles); funcfiles = cell (1, nrun); sessionfiles = cell (nrun,ntimepoint); for i = 1:nrun for j = 1:ntimepoint sessionfiles{ i , j }= strcat (fdir{1},'\',cffiles{ j }); % 在这里在每一个功能像文件的绝对路径后面添加,1 end funcfiles{1, i } = {sessionfiles{ i ,:}}'; % 如果有多个Sessions,funcfiles中就会有多个sessionfiles end clear matlabbatch % preprocessing job display 'Creating preprocessing job' %在matlab的命令行窗口显示Creating preprocessing job goalDirectory = { 'B:\fMRI\preprocess_for_batch\CN' }; goalDirectory_subj = {spm_select( 'CPath' , subjects{csubj_MRI} ,goalDirectory)}; goalDirectory_subj = char (goalDirectory_subj); if ~ exist (goalDirectory_subj, 'dir' ) goalDirectory = goalDirectory{1}; mkdir ([goalDirectory '\' subjects{csubj_MRI}]); end goalDirectory_subj = {goalDirectory_subj}; goalDirectory_MRI = {spm_select( 'CPath' , 'MRI' ,goalDirectory_subj)}; goalDirectory_MRI = char (goalDirectory_MRI); if ~ exist (goalDirectory_MRI, 'dir' ) % mkdir 'B:\fMRI\preprocess_for_batch\AD' MRI goalDirectory_subj = goalDirectory_subj{1}; mkdir ([goalDirectory_subj '\' ' MRI']); else warning ( sprintf ( 'Folder has been created and make sure if files needed has exited' , subjects{csubj_MRI})) return end matlabbatch{1}.spm.util. import .dicom.data = funcfiles{1,1}(:,1); matlabbatch{1}.spm.util. import .dicom. root = 'flat' ; matlabbatch{1}.spm.util. import .dicom.outdir = {goalDirectory_MRI}; 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; matfile = sprintf ( 'preprocess_MRI_%s.mat' , subjects{csubj_MRI}); % save(matfile,'matlabbatch'); jobs_MRI{csubj_MRI} = matfile; %jobs这个变量中存储了所有被试的matlabbatch spm_jobman( 'run' ,matlabbatch); end |
本文来自博客园,作者:z_s_s,转载请注明原文链接:https://www.cnblogs.com/zhoushusheng/p/18321667
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!