Hello, I am trying to achieve the attached process (which I had been doing using nibabel
) with SimpleITK. I’m not sure how to interpret the cosine matrix, though, so I’m unclear about how to ensure L-P-S orientation:
DESIRED_ORIENTATION = ('L', 'P', 'S')
def extract_orientation(ctx):
ctx.affine = ctx.mos_img.affine
ctx.orientation = nib.aff2axcodes(ctx.affine)
def reorient_mask(ctx, desired_orientation):
ctx.orig_orientation = ctx.orientation
# Convert the orientations to the orientation matrices
current_ornt = nib.orientations.axcodes2ornt(ctx.orientation)
desired_ornt = nib.orientations.axcodes2ornt(desired_orientation)
# Get the transform from current to desired orientation
ctx.transform_ornt = nib.orientations.ornt_transform(current_ornt, desired_ornt)
# Apply the transform
ctx.orig_mos_data = ctx.mos_data
ctx.mos_data = nib.apply_orientation(ctx.mos_data, ctx.transform_ornt)
# Update the affine matrix
ctx.orig_affine = ctx.affine
ctx.affine = ctx.affine @ nib.orientations.inv_ornt_aff(ctx.transform_ornt, ctx.mos_img.shape)
# Reorient the image
ctx.orig_img = ctx.mos_img
ctx.mos_img = nib.Nifti1Image(ctx.mos_data, ctx.affine)
# Update the orientation in the context object
extract_orientation(ctx)