python-3.xthree.jsnumpy-ndarraynibabelami.js

How to modify orientation of mgh/dicom/nifti file using nibabel


I have a hard time, figuring out a proper affine transformation for 3 different views i.e. coronal, axial and saggital, each having separate issues like below:

1: Axial color map get overlapped with the saggital original view.

enter image description here

2: Similarly Sagittal color map gets overlapped with the axial original image.

enter image description here

3: And everyone has some kind of orientation issues like best visible here when the color map and original image for coronal come correct but with wrong orientation.

enter image description here

I am saving the original file that I am sending to the server for some kind of prediction, which generates a color map and returns that file for visualization, later I am displaying everything together.

In server after prediction, here is the code to save the file.

 nifti_img = nib.MGHImage(idx, affine, header=header)

Whereas affine and header are the original affine and header extracted from the file I sent.

I need to process "idx" value that holds the raw data in Numpy array format, but not sure what exactly to be done. Need help here.

Was trying hard to solve the issue using nibabel python library, but due to very limited knowledge of mine about how these files work and about affine transformation, I am having a hard time figuring out what should I do to make them correct.

I am using AMI js with threejs support in the frontend and nibabel with python in the back end. Solution on the frontend or back end anywhere is acceptable.

Please help. Thanks in advance.


Solution

  • It was simple, using numpy.moveaxis() and numpy.flip() operation on rawdata from nibabel. as below.

        # Getting raw data back to process for better orienation and label mapping.
        orig_img_data = nib.MGHImage(numpy_arr, affine)
        nifti_img = nib.MGHImage(segmented_arr_output, affine)  
    
        # Getting original and predicted data to preprocess to original shape and view for visualisation.
        orig_img = orig_img_data.get_fdata()
        seg_img = nifti_img.get_fdata()
    
        # Placing proper views in proper place and flipping it for a better visualisation as required.
        # moveaxis to get original order.
        orig_img_ = np.moveaxis(orig_img, -1, 0)
        seg_img = np.moveaxis(seg_img, -1, 0)
    
        # Flip axis to overcome mirror image/ flipped view.        
        orig_img_ = np.flip(orig_img_, 2)
        seg_img = np.flip(seg_img, 2)
    
        orig_img_data_ = nib.MGHImage(orig_img_.astype(np.uint8), np.eye(4), header)
        nifti_img_ = nib.MGHImage(seg_img.astype(np.uint8), np.eye(4), header)
    

    Note: It's very important to have same affine matrix to wrap both of these array back. A 4*4 Identity matrix is better rather than using original affine matrix as that was creating problem for me.