pythondicomsimpleitkimage-registration

How to sort DICOM images in reverse order (by Slice Location) with SimpleITK?


I am new to SimpleITK, so I would really appreciate any help! My question is specific to SimpleITK (not Pydicom).

I imported CT DICOM slices from a directory and opened them using SimpleITK. The slices are ordered by the slice location. I was wondering if it's possible to reverse the order of the images? I am sure I can do that after getting the image array and manipulating it. But would it be possible to reverse the order before getting the image array?

Here is the code that I am working with:

import SimpleITK as sitk

dir_name = "Directory of the Folder that contains individual DICOM slices"
reader = sitk.ImageSeriesReader()
filenames = reader.GetGDCMSeriesFileNames(dir_name)
reader.SetFileNames(filenames)
reader.MetaDataDictionaryArrayUpdateOn() # to get metadata
image_import = reader.Execute()
image_array = sitk.GetArrayFromImage(image_import) # getting the image array

I guess I can sort by the reverse order by manipulating image_array, but I was wondering if there is other way which does that beforehand. (I thought the other way would be useful for image registration down the line.)


Solution

  • SimpleITK has a FlipImageFilter class or Flip function that you can use to reverse the Z. After your read the image add the following line:

    image_import = sitk.Flip(image_import, [False, False, True])
    

    The array of booleans tell the function to not flip X or Y but only flip Z.