S.EB
S.EB

Reputation: 2236

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?

Upvotes: 3

Views: 11539

Answers (2)

Angeluz-07
Angeluz-07

Reputation: 81

I needed to reorient NIfTI images (files with .nii or .nii.gz extension) and the above solution didn't work for me. After some research in the SimpleITK library I found the function sitk.DICOMOrient() which worked for me.

Adjusting to your example (Tested in version 2.1.1 of SimpleITK and Python 3.7.1) :

# Given a sitk image instance -> img

reoriented = sitk.DICOMOrient(img, 'LPS')

It is also possible to test multiple permutations of the orientation labels, check this doc to understand the about the meaning of the labels.

Upvotes: 5

g.stevo
g.stevo

Reputation: 740

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')

Upvotes: 6

Related Questions