Convert DICOM to NIFTI by using dcm2niix 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 --- Output status : execute status (=0:success) nifti_filename : output nifti filename(without path) --- 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) prog_path : path for 'dcm2niix' program ---- Output file name convention Options : -1..-9 : gz compression level (1=fastest..9=smallest, default 6) -a : adjacent DICOMs (images from same series always in same folder) for faster conversion (n/y, default n) -b : BIDS sidecar (y/n/o [o=only: no NIfTI], default y) -ba : anonymize BIDS (y/n, default y) -c : comment stored in NIfTI aux_file (provide up to 24 characters e.g. '-c first_visit') -d : directory search depth. Convert DICOMs in sub-folders of in_folder? (0..9, default 5) -e : export as NRRD (y) or MGH (o) instead of NIfTI (y/n/o, default n) -f : filename (%a=antenna (coil) name, %b=basename, %c=comments, %d=description, %e=echo number, %f=folder name, %g=accession number, %i=ID of patient, %j=seriesInstanceUID, %k=studyInstanceUID, %m=manufacturer, %n=name of patient, %o=mediaObjectInstanceUID, %p=protocol, %r=instance number, %s=series number, %t=time, %u=acquisition number, %v=vendor, %x=study ID; %z=sequence name; default '%f_%p_%t_%s') -g : generate defaults file (y/n/o/i [o=only: reset and write defaults; i=ignore: reset defaults], default n) -h : show help -i : ignore derived, localizer and 2D images (y/n, default n) -l : losslessly scale 16-bit integers to use dynamic range (y/n/o [yes=scale, no=no, but uint16->int16, o=original], default o) -m : merge 2D slices from same series regardless of echo, exposure, etc. (n/y or 0/1/2, default 2) [no, yes, auto] -n : only convert this series CRC number - can be used up to 16 times (default convert all) -o : output directory (omit to save to input folder) -p : Philips precise float (not display) scaling (y/n, default y) -r : rename instead of convert DICOMs (y/n, default n) -s : single file mode, do not convert other images in folder (y/n, default n) -u : up-to-date check -v : verbose (n/y or 0/1/2, default 0) [no, yes, logorrheic] -w : write behavior for name conflicts (0,1,2, default 2: 0=skip duplicates, 1=overwrite, 2=add suffix) -x : crop 3D acquisitions (y/n/i, default n, use 'i'gnore to neither crop nor rotate 3D acquistions) -z : gz compress images (y/o/i/n/3, default n) [y=pigz, o=optimal pigz, i=internal:zlib, n=no, 3=no,3D] --big-endian : byte order (y/n/o, default o) [y=big-end, n=little-end, o=optimal/native] --progress : Slicer format progress information (y/n, default n) --ignore_trigger_times : disregard values in 0018,1060 and 0020,9153 --terse : omit filename post-fixes (can cause overwrites) --version : report version --xml : Slicer format features Defaults file : /home/cbi/rhayashi/.dcm2nii.ini Examples : dcm2niix /Users/chris/dir dcm2niix -c "my comment" /Users/chris/dir dcm2niix -o /users/cr/outdir/ -z y ~/dicomdir dcm2niix -f %p_%s -b y -ba n ~/dicomdir dcm2niix -f mystudy%s ~/dicomdir dcm2niix -o "~/dir with spaces/dir" ~/dicomdir Example output filename: 'myFolder_MPRAGE_19770703150928_1.nii'
0001 function [status, nifti_filename] = convert_dicom_nifti_cmd(fname,outdir,prog_path,opt) 0002 % Convert DICOM to NIFTI by using dcm2niix 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 % --- Output 0008 % status : execute status (=0:success) 0009 % nifti_filename : output nifti filename(without path) 0010 % --- Optional Input 0011 % outdir : Directory for output file 0012 % if this is not specified, output file is saved in the same directory 0013 % opt : option string (See the following option rule) 0014 % 0015 % prog_path : path for 'dcm2niix' program 0016 % 0017 % ---- Output file name convention 0018 % Options : 0019 % -1..-9 : gz compression level (1=fastest..9=smallest, default 6) 0020 % -a : adjacent DICOMs (images from same series always in same folder) for faster conversion (n/y, default n) 0021 % -b : BIDS sidecar (y/n/o [o=only: no NIfTI], default y) 0022 % -ba : anonymize BIDS (y/n, default y) 0023 % -c : comment stored in NIfTI aux_file (provide up to 24 characters e.g. '-c first_visit') 0024 % -d : directory search depth. Convert DICOMs in sub-folders of in_folder? (0..9, default 5) 0025 % -e : export as NRRD (y) or MGH (o) instead of NIfTI (y/n/o, default n) 0026 % -f : filename (%a=antenna (coil) name, %b=basename, %c=comments, %d=description, %e=echo number, %f=folder name, %g=accession number, %i=ID of patient, %j=seriesInstanceUID, %k=studyInstanceUID, %m=manufacturer, %n=name of patient, %o=mediaObjectInstanceUID, %p=protocol, %r=instance number, %s=series number, %t=time, %u=acquisition number, %v=vendor, %x=study ID; %z=sequence name; default '%f_%p_%t_%s') 0027 % -g : generate defaults file (y/n/o/i [o=only: reset and write defaults; i=ignore: reset defaults], default n) 0028 % -h : show help 0029 % -i : ignore derived, localizer and 2D images (y/n, default n) 0030 % -l : losslessly scale 16-bit integers to use dynamic range (y/n/o [yes=scale, no=no, but uint16->int16, o=original], default o) 0031 % -m : merge 2D slices from same series regardless of echo, exposure, etc. (n/y or 0/1/2, default 2) [no, yes, auto] 0032 % -n : only convert this series CRC number - can be used up to 16 times (default convert all) 0033 % -o : output directory (omit to save to input folder) 0034 % -p : Philips precise float (not display) scaling (y/n, default y) 0035 % -r : rename instead of convert DICOMs (y/n, default n) 0036 % -s : single file mode, do not convert other images in folder (y/n, default n) 0037 % -u : up-to-date check 0038 % -v : verbose (n/y or 0/1/2, default 0) [no, yes, logorrheic] 0039 % -w : write behavior for name conflicts (0,1,2, default 2: 0=skip duplicates, 1=overwrite, 2=add suffix) 0040 % -x : crop 3D acquisitions (y/n/i, default n, use 'i'gnore to neither crop nor rotate 3D acquistions) 0041 % -z : gz compress images (y/o/i/n/3, default n) [y=pigz, o=optimal pigz, i=internal:zlib, n=no, 3=no,3D] 0042 % --big-endian : byte order (y/n/o, default o) [y=big-end, n=little-end, o=optimal/native] 0043 % --progress : Slicer format progress information (y/n, default n) 0044 % --ignore_trigger_times : disregard values in 0018,1060 and 0020,9153 0045 % --terse : omit filename post-fixes (can cause overwrites) 0046 % --version : report version 0047 % --xml : Slicer format features 0048 % Defaults file : /home/cbi/rhayashi/.dcm2nii.ini 0049 % Examples : 0050 % dcm2niix /Users/chris/dir 0051 % dcm2niix -c "my comment" /Users/chris/dir 0052 % dcm2niix -o /users/cr/outdir/ -z y ~/dicomdir 0053 % dcm2niix -f %p_%s -b y -ba n ~/dicomdir 0054 % dcm2niix -f mystudy%s ~/dicomdir 0055 % dcm2niix -o "~/dir with spaces/dir" ~/dicomdir 0056 % Example output filename: 'myFolder_MPRAGE_19770703150928_1.nii' 0057 0058 % option 0059 % -ba y : Anonymize BIDS 0060 % -f %i : Use patient ID in filename 0061 % -t n : No save patient details. 0062 if ~exist('opt','var') 0063 % opt = ' -ba y -w 1 -f tmp_subject -t n '; 0064 opt = ' -ba y -w 1 -f %i -t n '; 0065 else 0066 opt = [' ' opt ' ']; 0067 end 0068 0069 % output directory 0070 if ~exist('outdir','var') || isempty(outdir) 0071 outdir = '.'; 0072 end 0073 outdir_tmp = tempname(outdir); 0074 mkdir(outdir_tmp); 0075 0076 % program 0077 if ~exist('prog_path','var') || isempty(prog_path) 0078 [arch] = computer; 0079 if ispc 0080 prog_path = which('dcm2niix.exe'); 0081 elseif strcmp(arch, 'GLNX86') || strcmp(arch, 'GLNXA64') 0082 prog_path = which('dcm2niix_linux64.'); 0083 else 0084 prog_path = which('dcm2niix_mac.'); 0085 end 0086 end 0087 if ~ispc 0088 [stat, mess] = fileattrib(prog_path); 0089 if mess.UserExecute ~= 1 0090 fprintf('trying to change permission of dcm2niix executable.\n'); 0091 [stat2, mess] = fileattrib(prog_path, '+x'); 0092 if stat2 ~= 1 0093 error([prog_path ' need to have executable permission.']); 0094 end 0095 end 0096 end 0097 0098 prog_path = dblquote(prog_path); 0099 cmd = [prog_path, opt, ' -o ' dblquote(outdir_tmp) ' ', dblquote(fname), ... 0100 ' > ' dblquote([outdir_tmp, '/dcm2niix.log'])]; 0101 fprintf('command: %s\n', cmd); 0102 [status] = system(cmd); 0103 0104 % get nifti filename 0105 d = dir(fullfile(outdir_tmp, '*.nii')); 0106 nifti_filename = d.name; 0107 nifti_file = fullfile(outdir, nifti_filename); 0108 0109 % move to output directory 0110 movefile(fullfile(outdir_tmp, '*.nii'), outdir, 'f'); 0111 rmdir(outdir_tmp, 's'); 0112 0113 %status = dos(cmd, '-echo'); 0114 0115 function q_str = dblquote(str) 0116 0117 q_str = ['"', str, '"']; 0118