首页 > 其他分享 >DICOM格式转NII格式——SPM12批量码

DICOM格式转NII格式——SPM12批量码

时间:2024-07-24 20:28:50浏览次数:6  
标签:fMRI NII DICOM matlabbatch spm csubj 格式 goalDirectory subj

 

运行说明:subjectsdir换成自己的文件夹地址, 有多少个受试者的DICOM就有多少个subjects子元素 。

 

%-----------------------------------------------------------------------
% 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
  

  

标签:fMRI,NII,DICOM,matlabbatch,spm,csubj,格式,goalDirectory,subj
From: https://www.cnblogs.com/zhoushusheng/p/18321667

相关文章

  • Golang格式化占位符详解
    Golang格式化占位符详解在Golang编程中,格式化字符串是一种常见的操作,它可以用来将不同类型的数据格式化为字符串输出。在格式化字符串时,占位符起着至关重要的作用,通过占位符可以指定数据的输出格式。本文将详细介绍Golang中一些常用的格式化占位符,并提供具体的代码示例供大......
  • pyMysql插入数据格式化问题!(秒解决)
    运行以下代码出现的问题:%dformat:anumberisrequired,notstr。username=input("请输入用户名:")password=input("请输入密码:")password=hashlib.md5(password.encode("utf-8")).hexdigest()real_name=input("请输入真实姓名:")......
  • 纹理压缩格式
    参考:移动平台打包纹理压缩设置-JeasonBoy-博客园(cnblogs.com)Unity官方的文档对于纹理压缩的说明:Recommended,default,andsupportedtextureformats,byplatform-Unity手册Unity支持许多常见的图像格式作为导入纹理的源文件(例如JPG、PNG、PSD和TGA)。但是,3D......
  • 关于时间格式存入Redis里面乱码的问题
    java的LocalDateTime时间类型不能转json传输,一般都是用这两个注解解决@JsonDeserialize(using=LocalDateTimeDeserializer.class)@JsonSerialize(using=LocalDateTimeSerializer.class)  @JsonDeserialize:json反序列化注解,用于字段或set方法上,作用于setter()方法,将jso......
  • 将mat格式转化为png格式的方法
    **方法一:**importscipy.ioimportnumpyasnpimportcv2importosinput_folder='D:\daima\CrackForest-dataset-master\CrackForest-dataset-master\groundTruth'#输出文件夹路径output_folder='D:\daima\CrackForest-dataset-master\CrackForest-d......
  • 021集——批量txt格式坐标转dwg——vba代码实现
    在工作中遇到txt转dwg的重复性工作,详细如下:已知若干个txt文件,坐标格式如下:要求:将每个txt格式坐标文件转为dwg,名称与原txt名称一样,即1.txt生成1.dwg。因txt文件中存在一些非坐标文本数字,我们需进行判断只提取xy坐标数据,然后在cad中输入坐标,生成一个轻量线。此项工作只需一......
  • Python Pandas 从使用第 3 部分 API 自动生成的 Excel 文件中读取不一致的日期格式
    我正在使用PDF4meAPI将PDF发票转换为Excel文件。API可以正确读取日期。但是,当我打开Excel文件时,日期显示不一致:某些日期以正确的格式(dd/mm/yyyy)显示,而其他日期以错误的mm/dd/yyyy格式显示。当该月的某天小于或等于12时,似乎会出现这种不一致。......
  • 处理json格式的数据
    如何处理json格式的数据1.主要处理三类数据带有json格式的数据形式:[{key:value}]提取:文字、图片链接、文档链接[{"content":"点击“产品开发库”,右上角“展开筛选”,按条件筛选流程信息。","type":"text"},{"content":"\n","type":"text"},{"......
  • 更改 pyplot 中 xaxis 标签的格式
    我正在尝试重新格式化x轴,但我所做的一切都不起作用当我使用:importpandasaspdimportmatplotlib.pyplotaspltz=pd.DataFrame([[2000,1,2],[2001,2,3],[2004,5,6]])z=z.set_index(0)z.plot()plt.show()x轴标签格式为2,000.0到2,000.0如何让它简单地显......
  • 如何使代码格式再次适用于 Python(Mac 上的 Visual Studio Code)?
    在Mac上,Option+Shift+F现在会显示“没有安装用于‘python’文件的格式化程序”。消息框:我尝试安装这个插件,但没有看到这种情况的变化:我已经为Python安装了这两个插件:但是正如@starball提到的,它可能已经减少了支持现在。......