Come and visit this free open source event:

FOSDEM
Faq /

Stack

How to stack DICOM series into one volume?

[1] AMIDE

For stacking multi-file DICOM volumes or time series, you should give AMIDE a first try, as it uses the excellent DCMTK OFFIS library for proper interpretation of slice and time sequencing.

[2a] XMedCon

The quick trial by error uses (X)MedCon's -stack3d option directly:

   $> medcon -f *.dcm -c dicom -stack3d -n -qc

The above line should generate a single NM dicom file, where all single slice files are stacked into one volume.

Your only prerequisite is that the sequence of files offered on the command line - i.e. alphabetically when using wildcards - should represent the true sequence according to the actual volume. (X)MedCon just stacks as given. It doesn't check for true slice locations.

You can follow two examples on this asciinema screencast.

[2b] XMedCon with shell script for sorting

What follows hereafter is an extremely elaborated, yet scriptable alternative in getting the files somehow sorted as it should be according to series, acquisition and instance numbers found in the DICOM files. But be warned, this neither guarantee true sequencing.

So the reason you're looking in this FAQ is probably because in some directory, you now have a bunch of single DICOM files with cryptic numbered filenames such as:

   $> ls -1
000001.ima
000002.ima
000003.ima
...
000315.ima
000316.ima
000317.ima

or even worse:

   $> ls -1
MR.2.16.840.1.113662.4.8796817846943.938718605.312682898607943975
MR.2.16.840.1.113662.4.8796817846943.938718605.313546807656970005
MR.2.16.840.1.113662.4.8796817846943.938718606.299611781114643417
...
MR.2.16.840.1.113662.4.8796817846943.938718852.961660032350274905
MR.2.16.840.1.113662.4.8796817846943.938718852.964022027709774806
MR.2.16.840.1.113662.4.8796817846943.938718852.966922267941187168

... and would like to stack those images into a single 3D volume file. The command-line tool we use for achieving this is medcon available in the (X)MedCon "source or binary" packages and downloadable from here. You got the binary? Then proceed ...

The first thing to do is to rename those pesky files into a human readable format. For a fast solution without wasting diskspace, you can apply this little shell script called 'mklinks.sh', which makes use of the --echo-alias-name option. The more elaborated script 'mklinks2.sh' is easier to addapt to your sites naming convention. How? Just read the comments within the script. For both scripts, the only argument needed is a directory containing your cryptic named images.

   $> mklinks.sh /images/subdir

In case of proper DICOM or ACR/NEMA formats, this will result in links or filenames with the following syntax (version >= 0.6.5):

   <patient_name>+<study_descr>+<study_date>+<study_time>+<nr_series>+<nr_acquisition>+<nr_instance>


<<Index | Top>>

As an example you should get something similar like:

  $> ls -1
patient_name+study_example+19960131+81511+00000+00001+00001.dcm
patient_name+study_example+19960131+81511+00000+00001+00002.dcm
patient_name+study_example+19960131+81511+00000+00001+00003.dcm
patient_name+study_example+19960131+81511+00000+00001+00004.dcm
patient_name+study_example+19960131+81511+00000+00001+00005.dcm
patient_name+study_example+19960131+81511+00000+00001+00006.dcm
patient_name+study_example+19960131+81511+00000+00001+00007.dcm
patient_name+study_example+19960131+81511+00000+00001+00008.dcm
patient_name+study_example+19960131+81511+00000+00001+00009.dcm
patient_name+study_example+19960131+81511+00000+00001+00010.dcm
patient_name+study_example+19960131+81511+00000+00001+00011.dcm
patient_name+study_example+19960131+81857+00000+00001+00012.dcm
patient_name+study_example+19960131+81857+00000+00001+00013.dcm
patient_name+study_example+19960131+81857+00000+00001+00014.dcm
patient_name+study_example+19960131+84035+00000+00001+00056.dcm
...
patient_name+study_example+19960131+90455+00000+00001+00190.dcm
patient_name+study_example+19960131+90455+00000+00001+00191.dcm
patient_name+study_example+19960131+90455+00000+00001+00192.dcm
patient_name+study_example+19960131+91902+00000+00000+00193.dcm
patient_name+study_example+19960131+92629+00000+00008+00194.dcm
patient_name+study_example+19960131+92701+00000+00004+00195.dcm
patient_name+study_example+19960131+93535+00000+00000+00196.dcm
patient_name+study_example+19960131+94254+00000+00008+00197.dcm
patient_name+study_example+19960131+94320+00000+00004+00198.dcm

<<Index | Top>>

Now we have the advantage of being able to figure out which files could belong to a single volume when grouped and sorted alphabetically. The latter is useful in conjunction with the -stack3d option. Again, this option requires that the files on the argument line are given in the proper sequence of the volume. Now which stacking rule to apply here?

'filenames identical up to the +nr_acquisitions+ probably belong to the same volume'

Be warned that this isn't always the case. This rule for example, will not separate MRI proton density from T2-weighted series. We could use the "Echo Time" but that seems not always available in older files. So we stick here to the simple rule noted above.

Sometimes, it will be sufficient to group files into a volume, based on the +study_time+ found in the filename. Let us assume this is the case here. Next is a command-line to quickly filter out the unique +study_time+ parameters:

   $>ls -1 patient_name* | awk -F+ '{ print $4 }' | sort -u
81511
81857
84035
90455
91902
92629
92701
93535
94254
94320

Now it is time to stack those single slice files together. Here we do it for the first group:

   $> medcon -f *+81511+* -c dicom -stack3d -n -qc

The name of the output file will have the m000-stack3d- prefix. You need to be carefull with wildcards though and check the volume in a decent viewer. When you 're creative with scripts, you can automate this process for sure.

Do not neglect any warning messages during this last process. They most definitely reveil that not all selected images belong to the same volume, unless you intended to do so.

This process although flexible is not failsafe. At least we hope you get a general idea behind the principle. Enjoy it.

Work to be done:

  • do alias naming within medcon
  • using a list tree for sorting

<<Index | Top>>