pythonitksimpleitknibabel

How to Flip MRI image in Python


I am trying to flip image shown in picture 1 to match the orientation of the image in picture 2, but I am not sure how.

enter image description here

This is how image should be oriented:

enter image description here

Any ideas how I can do this in Python with SimpleITK or nibabel for example?


Solution

  • Assuming you are reading both images from file formats which support orientation. In SimpleITK you can do the following to obtain and approximately set the orientation with flip and mirror operations:

    import SimpleITK as sitk
    
    img1 = sitk.ReadImage(filename)
    input1_orientation = sitk.DICOMOrientImageFilter_GetOrientationFromDirectionCosines(img1.GetDirection())
    print(f"Input Orientation: {input1_orientation}")
    oriented_img1 = sitk.DICOMOrient(img1, desiredCoordinateOrientation='LPS')
    

    If the orientation is missing from the file, please first consider addressing how the images are processed upstream to correctly preserve the meta-data. Secondly, the correct orientation information can be set to the image's Direction cosine matrix. Lastly, if the orientation can not be preserved or recovered then a combination of the PermuteAxesImageFilter and the FlipImageFilter can be used.