I am trying to export the masks in mimics to nifti format, via the scripting module. However, I have a question regarding affine transformations as I am getting something wrong in the process. I think it is a really silly mistake but for some reason I cannot figure it out. I am using this page as a source of information: Neuroimaging in Python — NiBabel 3.2.1+100.g9fdf5e3e documentation
From what I understand, the tag 0020, 0037 provides the orientation of the rows and column of the slice of the MRI (in this case) image to transform the image into the patient (dicom) coordinate system. In my images, I get the following info from this tag:
1 0 0 \ 0 -1 0
From my readings, I understand that the patient/world coordinate for Dicom standard is left +, posterior +, superior + [LPS+]. This means that the images coordinate system is left+, inferior+ and posterior+. Therefore, the transformation affine matrix is from Dicom to Patient (assuming pixel spacing and slice thickness of 1 to keep it simple) and switching/flipping rows and columns as mentioned in the web page referenced above:
A= [0 1 0
0 0 1
-1 0 0]
From what I have read, the Nifti format has a different patient coordinate system than the Dicom, where right is +, anterior is +, and superior is+. Therefore, i and j are inverted with respect to Dicom and the Dicom patient to Nifti patient transformation matrix is
B= [ 0 -1 0
0 0 1
-1 0 0]
To obtain the nifti patient orientation (world), I would do the following:
Nifti patient [x; y; z]= [A][B]Dicom image[x; y; z] where the resulting rotation matrix is:
affine= [ 0 -1 0
0 0 1
1 0 0]
I obtain the mask using the scripting commands:
mimics_mask = mimics.data.masks.find(name=muscle)
mask_value = mimics_mask.get_voxel_buffer()
mask_array = np.asarray(mask_value)
mask_array = mask_array.astype(np.float)
I then export to nifti using
mask_img = nib.nifti1.Nifti1Image(mask_array, affine)
However, the resulting images (showing here the mask of a deformed vertebral column) are oriented all wrong, when I view it in ITK-snap.
-the sagittal plane image (first column, top row of the image quadrant) is in the location for the axial image
-the coronal plane image (second column, top row) is in the location for the sagittal image and it needs to be rotated clockwise by 90 degrees
-the axial plane image (second column, bottom row) is in the locatin for the coronal image and it needs to be rotated 90 degrees
It’s clear that I am getting something wrong in the affine transformation matrices, since the orientations are wrong, but I am not sure what it is?
I also tried using dicom2nifti function by importing it in the scripting module. Then I used :
image = nib.load (path to image)
affine_nifti = image.affine
To get the affine matrix this way. Then I use the same code as above to export the mask in Nifti format. I get a different result (!), but just as wrong in ITK, where the affine transformation is:
[-1 0 0
0 0 -1
0 -1 0]
I also noticed that the row and columns were not switched as instructed in the web page referenced above for the DICOM to Nifti conversion.
I was hoping to get some help from you to get the correct orientation in the nifti output…
Thank you,
Erica