pythonpython-2.7simpleitk

How to change the orientation of SimpleITK image in Python


I used this code to read a series for dicom images into python

series_IDs = sitk.ImageSeriesReader.GetGDCMSeriesIDs(data_directory)
series_file_names = sitk.ImageSeriesReader.GetGDCMSeriesFileNames(data_directory, series_IDs[0])
series_reader = sitk.ImageSeriesReader()
series_reader.SetFileNames(series_file_names)
series_reader.LoadPrivateTagsOn()
image3D=series_reader.Execute()
size = image3D.GetSize()

currently, its orientation is the following:

enter image description here

I need to change the orientation to the following orientation:

enter image description here

Is there any command in python to be able to change the orientation of SimpleITK image?


Solution

  • Extending your code to perform this I would look at building a new image from your loaded in one and then adding a custom direction to it. Althernatively ITK has a orientimage filter in the python wrapping available. This is not in simpleitk but might solve your problem

    import SimpleITK as sitk
    
    series_IDs = sitk.ImageSeriesReader.GetGDCMSeriesIDs(data_directory)
    series_file_names = sitk.ImageSeriesReader.GetGDCMSeriesFileNames(data_directory, series_IDs[0])
    series_reader = sitk.ImageSeriesReader()
    series_reader.SetFileNames(series_file_names)
    series_reader.LoadPrivateTagsOn()
    image3D=series_reader.Execute()
    size = image3D.GetSize()
    
    #get image data
    image_out = sitk.GetImageFromArray(sitk.GetArrayFromImage(img))
    
    #setup other image characteristics
    image_out.SetOrigin(img.GetOrigin())
    image_out.SetSpacing(img.GetSpacing())
    #set to RAI
    image_out.SetDirection(tuple(1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0))
    sitk.WriteImage(image_out, 'test.mha')