Reordering DICOM Files

Many grant cycles ago, an alert reader asked how to numerically reorder DICOM files that come from the scanner out of order. I said that I didn't know; but, not wishing to overtly display my ignorance, I kept it relegated to the comments section, away from the eye of public scrutiny.

However, another reader recently pointed out that this problem can be rectified by a script available on the UCSD website. Apparently DICOM files are generated in an out-of-order sequence by General Electric scanners (and possibly others) after an equipment upgrade. After a thorough investigation into why this was happening - why dozens, if not hundreds, of researchers were needlessly suffering from a hardware upgrade that was supposed to make their neuroimaging lives easier, not more difficult - the CEO of General Electric saw no alternative but to take action and jack up executive bonuses. We can all now rest easier.

The link to the script (which is in Perl) is here; I've also copied and pasted the script below, both to take away their Internet traffic, and to season this post with the flavor of programming rigor.

#! /usr/bin/perl
 use Shell;
 use Cwd; # module for finding the current working directory
$|=1;    # turn off I/O buffering

print "\n";

if ($#ARGV == -1) { # if no arguments are entered
print "\n";
else { # read in the arguments
 for ($j=0; $j<$#ARGV+1; $j++) {
  $tempdir = $ARGV[$j];
  if ($tempdir eq "."){
   $tempdir = &cwd
  opendir(DIR,$tempdir) or die "$tempdir does not exist or I can't open it\n"; # check the directories inputted
  @dirlist = $tempdir;
  foreach my $name (@dirlist) {
   print "\n";

sub ScanDirectory {
    my ($p) = 0;
 my ($workdir) = shift; 
    my($startdir) = &cwd; # keep track of where we began
 print "Processing Directory $workdir \n";
    chdir($workdir) or die "\nUnable to enter dir $workdir:$!\n";
    opendir(DIR, ".") or die "\nUnable to open $workdir:$!\n";
    my @names = readdir(DIR);
 $command = "mkdir backupimg";
 system ($command); 
    foreach my $name (@names){
        next if ($name eq "."); 
        next if ($name eq "..");
  next if ($name eq "backupimg");
        if (-d $name){                     # is this a directory?
  #do something with file
  if (grep(/\.MRDC\./, $name)){
   $p = $p + 1;
   $command = "cp $name backupimg/";
   system ($command); 
   $old_name = $name;
   $name =~ s/i(.*)\.MRDC\.(.*)/i\.CFMRI\.$2/;
   $num = "";
   $num = sprintf("%5d", $2);
   $num=~ tr/ /0/;
   $name = "i$num\.CFMRI\.$2";
   rename("$old_name", "$name") || die "Cannot rename $old_name: $!";
 print "     Directory $workdir has $p files processed \n"; # print size
 $command = "rm -rf backupimg";
 system ($command); 
    chdir($startdir) or die "Unable to change to dir $startdir:$!\n";

sub instructions {
print "This program renames and reorders the dicom files acquired on the GE scanners at UCSD - CFMRI.\n";
  print "Usage: imseq [directories to convert] \n";
  print "Example:  imseq directory1 directory2 directory3 \n\n";



FSL Tutorial 0: Conversion from DICOM to NIFTI

After publishing my last tutorial, I realized that I should probably take a step back and detail how to convert raw scanner images into something that can be used as FSL. This latest walkthrough will show you how to download MRIcron and use one of its tools, dcm2nii, to convert raw scanner image formats such as DICOM or PAR/REC to nifti, which can be used by almost all fMRI software analysis packages.

Note: I've been using Camtasia Studio to make these screencasts, and so far I have been very happy with it. The video editor is simple and intuitive, and making videos isn't too difficult. It runs for $99, which is a little pricey for a screencast tool, but if you're going to be making them on a regular basis and doing tutorials, I would recommend buying it.