SimpleITKPythonPackage icon indicating copy to clipboard operation
SimpleITKPythonPackage copied to clipboard

How do I get the affine transform from a Nifti image?

Open Karol-G opened this issue 2 years ago • 1 comments

Hey,

I would like to get the affine transform from a Nifti image, but I am not quite sure how? There is a simple method to get the image as a numpy array or the spacing from a loaded sitk Image object, but not for the affine.

Here is example code that illustrates my problem:

import SimpleITK as sitk

image_data = sitk.ReadImage(filename)
image = sitk.GetArrayFromImage(image_data)  # Easy to get the numpy image
spacing = image_data.GetSpacing()  # Easy to get the image spacing
affine = ...  # ???

I hope someone can help me.

Best, Karol

Karol-G avatar May 04 '23 10:05 Karol-G

Hello @Karol-G,

affine = image_date.GetDirection()

For more details on the image structure see the toolkit's fundamental concepts and this Jupyter notebook.

For future reference, please use the ITK discourse forum for Q&A.

zivy avatar May 04 '23 12:05 zivy