Convert DICOM to NIFTI by using dcm2nii status = convert_dicom_nifti_cmd(fname,outdir,prog_path,opt) --- Input fname : one of DICOM file All the files in DICOM image should be saved in the same directory --- Optional Input outdir : Directory for output file if this is not specified, output file is saved in the same directory opt : option string (See the following option rule) opt = ' -n n -s n -d n -e n -i n -p n -f y -g n -a n ' [default] prog_path : path for 'dcm2nii' program --- Output files Three set of NIFTI files are saved fname.hdr/img : original image o+fname.hdr/img : reoriented image with RAS orientation co+fname.hdr/img : reoriented & cropped image with RAS orientation --- Note Before using this program, path for 'dcm2nii' should be set --- Option for dcm2nii -o Output Directory, [default: source directory] -n output .nii file [if no, create .hdr/.img pair]: [Y],N -s SPM2/Analyze not SPM5/NIfTI [ignored if '-n y']: Y,[N] -v Convert every PAR file in the directory: [Y],N ---- Output file name convention -d Date in filename : Y,[N] -e events (series/acq) in filename : [Y],N -f Source filename : Y,[N] -i ID in filename : Y,[N] -p Protocol in filename : [Y],N -b load settings from specified inifile, e.g. '-b C:\set\t1.ini' -g gzip output, [ignored if '-n n']: [Y],N -a Anonymize [remove identifying information]: [Y],N Copyright (C) 2011, ATR All Rights Reserved. License : New BSD License(see VBMEG_LICENSE.txt)
0001 function status = convert_dicom_nifti_cmd(fname,outdir,prog_path,opt) 0002 % Convert DICOM to NIFTI by using dcm2nii 0003 % status = convert_dicom_nifti_cmd(fname,outdir,prog_path,opt) 0004 % --- Input 0005 % fname : one of DICOM file 0006 % All the files in DICOM image should be saved in the same directory 0007 % --- Optional Input 0008 % outdir : Directory for output file 0009 % if this is not specified, output file is saved in the same directory 0010 % opt : option string (See the following option rule) 0011 % opt = ' -n n -s n -d n -e n -i n -p n -f y -g n -a n ' [default] 0012 % prog_path : path for 'dcm2nii' program 0013 % --- Output files 0014 % Three set of NIFTI files are saved 0015 % fname.hdr/img : original image 0016 % o+fname.hdr/img : reoriented image with RAS orientation 0017 % co+fname.hdr/img : reoriented & cropped image with RAS orientation 0018 % 0019 % --- Note 0020 % Before using this program, path for 'dcm2nii' should be set 0021 % --- Option for dcm2nii 0022 % -o Output Directory, [default: source directory] 0023 % 0024 % -n output .nii file [if no, create .hdr/.img pair]: [Y],N 0025 % -s SPM2/Analyze not SPM5/NIfTI [ignored if '-n y']: Y,[N] 0026 % -v Convert every PAR file in the directory: [Y],N 0027 % 0028 % ---- Output file name convention 0029 % -d Date in filename : Y,[N] 0030 % -e events (series/acq) in filename : [Y],N 0031 % -f Source filename : Y,[N] 0032 % -i ID in filename : Y,[N] 0033 % -p Protocol in filename : [Y],N 0034 % -b load settings from specified inifile, e.g. '-b C:\set\t1.ini' 0035 % -g gzip output, [ignored if '-n n']: [Y],N 0036 % -a Anonymize [remove identifying information]: [Y],N 0037 % 0038 % Copyright (C) 2011, ATR All Rights Reserved. 0039 % License : New BSD License(see VBMEG_LICENSE.txt) 0040 0041 % option 0042 if ~exist('opt','var') 0043 % opt = ' -n N -s N -d N -e N -i Y -p N -f N -g N -a N '; 0044 opt = ' -n N -s N -d N -e N -i Y -p N -f N -g N -a Y '; 0045 else 0046 opt = [' ' opt ' ']; 0047 end 0048 % -n N : Create .hdr/.img pair 0049 % -s N : NIfTI format 0050 % -d N : No Date in filename 0051 % -e N : No series/acq in filename 0052 % -i Y : Use patient ID in filename 0053 % -p N : No Protocol in filename 0054 % -f N : No filename parameter 0055 % -g N : No gzip output 0056 % -a Y : Anonymize 0057 0058 % output directory 0059 if ~exist('outdir','var') || isempty(outdir) 0060 outdir = '.'; 0061 end 0062 0063 % program 0064 if ~exist('prog_path','var') || isempty(prog_path) 0065 [arch] = computer; 0066 if ispc 0067 prog_path = which('dcm2nii.exe'); 0068 elseif strcmp(arch, 'GLNX86') 0069 prog_path = which('dcm2nii_linux32.'); 0070 elseif strcmp(arch, 'GLNXA64') 0071 prog_path = which('dcm2nii_linux64.'); 0072 else 0073 prog_path = which('dcm2nii_mac.'); 0074 end 0075 end 0076 if ~ispc 0077 [stat, mess] = fileattrib(prog_path); 0078 if mess.UserExecute ~= 1 0079 fprintf('trying to change permission of dcm2nii executable.\n'); 0080 [stat2, mess] = fileattrib(prog_path, '+x'); 0081 if stat2 ~= 1 0082 error([prog_path ' need to have executable permission.']); 0083 end 0084 end 0085 end 0086 0087 prog_path = dblquote(prog_path); 0088 cmd = [prog_path, ' -b ', dblquote(which('dcm2nii.ini')), ' ', opt, ... 0089 ' -o ' dblquote(outdir) ' ', dblquote(fname), ... 0090 ' > ' dblquote([outdir, '/dcm2nii.log'])]; 0091 fprintf('command: %s\n', cmd); 0092 status = system(cmd); 0093 0094 %status = dos(cmd, '-echo'); 0095 0096 function q_str = dblquote(str) 0097 0098 q_str = ['"', str, '"']; 0099