Just a follow-up on this-
In the end I found that (of course) scripting works best.
The drawback is that you have to keep track of the slices and what is in
which volume et cetera, but the plus is that it is possible to automate
NIFTIfying complete DICOM-directories and CD-roms with one and the same
script!
I use the command-line programs medcon (http://xmedcon.sourceforge.net)
and avwchfiletype (http://www.fmrib.ox.ac.uk/fsl) to rip the CD data to
analyze format and make nifti files, respectively. The medcon program
can be set up to rip directly to nifti, but avwchfiletype sets some
extra header fields (like orientation matrices) which may come in handy.
The medcon program (and its graphical cousin xmedcon) are quite impressive!
Please find the script attached, it may be helpful if you have a DICOM
CD. This script was for a CD full of dual-echo images and anatomical
images. Before using this script, plelase remove the lines specific to
what I needed.
Have fun
Alle Meije
########################################################################
#!/bin/bash
# this is a BASH script
CD=$PWD
# relink -make sure that all numbers have the same #digits
# i.e., 1 -> 001, 10 -> 010, etc, for ordering of slices
# (this only needs to be done once)
RELINK="YES"
if [[ "$RELINK" == "YES" ]]; then
# first, find all the image directories
# basically, look for "IM1". The IM2 etc will be at similar places
IMDIRS=""
imdirs=0;
for d in `find $CD -name IM1 -type f`; do
# add directory name minus the last "/<string>"
IMDIRS="$IMDIRS ${d%/*}";
let imdirs=$imdirs+1;
done
echo "$imdirs image directories found before renaming"
echo "Renaming 1-digit directories:"
# make links from IM1 to IM01, etc
for d in `find $CD -name IM\?`; do
ln -fs $d ${d//IM/IM0};
printf "."
done
printf "\n"
echo "Renaming 2-digit directories:"
# make links from IM10 to IM010, etc
for d in `find $CD -name IM\?\?`; do
ln -fs $d ${d//IM/IM0};
printf "."
done
printf "\n"
fi
# first, find all the image directories
# basically, look for "IM1". The IM2 etc will be at similar places
IMDIRS="";
imdirs=0;
for d in `find $CD -name IM001`; do
# add directory name minus the last "/<string>"
IMDIRS="$IMDIRS ${d%/*}";
let imdirs=$imdirs+1;
done
echo "$imdirs image directories found (after renaming)"
# then, cycle over all directories with slices
# (keeping track with counter imdir)
imdir=0;
for IMDIR in $IMDIRS; do
let imdir=$imdir+1;
# reset all slice counters
echo "gathering data for subject $IMDIR ($imdir of $imdirs)"
IMAGES="";
PDIMAGES="";
T2IMAGES="";
i=0;
# add slice images to main volume, PD volume and T2 volume
for f in `find $IMDIR -name IM\?\?\? | sort -g`; do
IMAGES="$IMAGES $f";
if (( $i % 2 == 0 )); then
PDIMAGES="$PDIMAGES $f";
else
T2IMAGES="$T2IMAGES $f";
fi
let i=($i+1)%2;
done
images=`echo $IMAGES|wc -w`;
# building the medcon command
echo "adding $images slice images in $IMDIR into a NifTI volume"
COMMAND="$HOME/usr/local/bin/medcon" # program call
COMMAND="$COMMAND -alias" # generate output name from
patient data
COMMAND="$COMMAND -n" # do not discard negative values
COMMAND="$COMMAND -b16" # generate 16-bit output
COMMAND="$COMMAND -c anlz" # output format: analyze
COMMAND="$COMMAND -preacq" # acq. # in filename
COMMAND="$COMMAND -preser" # ser. # in fliename
COMMAND="$COMMAND -cor" # coronal slices
COMMAND="$COMMAND -stack3d" # combine slices
COMMAND="$COMMAND -w " # overwrite data
COMMAND="$COMMAND -f $IMAGES" # list of input images
# clear nifti files (created before -dont know where they should go)
rm -f $PWD/*.[hni]??
# do this only for dual-echo images
if (( $images > 100 )); then
# PD and T2 images
echo "making separate proton-density weighted image"
PDCOMMAND=${COMMAND%%-f*};
PDCOMMAND="$PDCOMMAND -f $PDIMAGES"
$PDCOMMAND;
for f in $PWD/m*.[hni]??;do
mv $f `dirname $f`/PD`basename $f`;
done
echo "making separete T2-weighted image"
T2COMMAND=${COMMAND%%-f*};
T2COMMAND="$T2COMMAND -f $T2IMAGES"
$T2COMMAND;
for f in $PWD/m*.[hni]??;do
mv $f `dirname $f`/T2`basename $f`;
done
fi
# dual-echo (PD + T2) image
echo "combining all slices into one volume"
$COMMAND;
# move files to their own dir
mv -f *.[hni]?? $IMDIR
done
# make NifTI copies as well
ALLNIFTIS=`find $PWD -name \*.img`
for f in $ALLNIFTIS; do
echo "making nifti version of $f ..."
avwchfiletype NIFTI $f ${f//.img/.nii}
done
########################################################################
|