questions > Using dcm2niix on subfolders with an archive
Showing 1-2 of 2 posts
Jul 12, 2019 10:07 PM | Ari Kahn
Using dcm2niix on subfolders with an archive
Is dcm2niix able to search subfolders within a zip file? I haven't
been able to get dcm2niix to recognize the scans, and it works fine
if I decompress it and recompress without relative paths, but
figured I'd check before trying to modify all the archives.
Jul 13, 2019 12:07 PM | Chris Rorden
RE: Using dcm2niix on subfolders with an archive
No, dcm2niix does not handle DICOMs archived in compressed zip
files. You can use your favorite scripting language here. Below is
my Matlab script for tgz archives which can easily be modified for
zip files:
----------------------------
function tgz2dcm1
%matlab script to convert DICOM images saved in compressed .tgz archives
tgzDir = '/Volumes/TbStick/DIOCM';
outDir = '/Volumes/TbStick/nifti';
dcm2niix = '/usr/local/bin/dcm2niix';
if ~exist(dcm2niix,'file'), error('Unable to find %s',dcm2niix); end;
d = dir(fullfile(tgzDir,'*.tgz'));
if isempty(tgzDir), error('No tgz files in %s', tgzDir); end
nams ={d.name};
nams =sort(nams);
for i = numel(nams): -1 : 1 %173numel(nams)
nam = nams{i};
[~, n] = fileparts(nam);
fprintf('%d/%d >> %s\n', i, numel(nams), n);
nam2 = fullfile(tgzDir,nam);
outDir2 = fullfile(outDir,n);
if exist(outDir2,'dir'), continue; end;
mkdir(outDir2);
outDir3 = fullfile(outDir2,n);
mkdir(outDir3);
untar(nam2, outDir3);
%convert DICOM -> Json
cmd = sprintf('%s -9 -b y -ba n -z y -i y -f %%t_%%s_%%p %s', dcm2niix, outDir2);
fprintf('Running \n %s\n', cmd);
system(cmd,'-echo');
rmdir(outDir3, 's')
end
----------------------------
function tgz2dcm1
%matlab script to convert DICOM images saved in compressed .tgz archives
tgzDir = '/Volumes/TbStick/DIOCM';
outDir = '/Volumes/TbStick/nifti';
dcm2niix = '/usr/local/bin/dcm2niix';
if ~exist(dcm2niix,'file'), error('Unable to find %s',dcm2niix); end;
d = dir(fullfile(tgzDir,'*.tgz'));
if isempty(tgzDir), error('No tgz files in %s', tgzDir); end
nams ={d.name};
nams =sort(nams);
for i = numel(nams): -1 : 1 %173numel(nams)
nam = nams{i};
[~, n] = fileparts(nam);
fprintf('%d/%d >> %s\n', i, numel(nams), n);
nam2 = fullfile(tgzDir,nam);
outDir2 = fullfile(outDir,n);
if exist(outDir2,'dir'), continue; end;
mkdir(outDir2);
outDir3 = fullfile(outDir2,n);
mkdir(outDir3);
untar(nam2, outDir3);
%convert DICOM -> Json
cmd = sprintf('%s -9 -b y -ba n -z y -i y -f %%t_%%s_%%p %s', dcm2niix, outDir2);
fprintf('Running \n %s\n', cmd);
system(cmd,'-echo');
rmdir(outDir3, 's')
end