Search code examples
pythonpython-3.ximageimage-processingnibabel

How to remove a modality from MRI image - Python Nibabel


I am trying to use MRI brain imaging data for deep learning model. Currently my image has 4 dimensions as shown below but I would like to retain only the T1c modality of the MRI image because my model input should only be 1 channel 3D MRIs (T1c).

I did try to make use of the Nibabel package as shown below

import nibabel as nib 
ff = glob.glob('imagesTr\*')
a = nib.load(ff[0])
a.shape

This returns the below output

enter image description here

I am also pasting the header info of 'a'

enter image description here

From this, which of the dimension is used to identify the MRI modality like (T1,T2, T1c, FLAIR etc)? and How can I retain only T1c?. Can you please help?


Solution

  • First you need to identify the order of the images stores in the 4th dimensions.

    Probably the header will help:

    print(a.header)
    

    Next, to keep only 1 modality you can use this:

    data = a.get_fdata()
    modality_1 = data[:,:,:,0]
    

    EDIT 1:

    Based on the website of the challenge:

    All BraTS multimodal scans are available as NIfTI files (.nii.gz) and describe a) native (T1) and b) post-contrast T1-weighted (T1Gd), c) T2-weighted (T2), and d) T2 Fluid Attenuated Inversion Recovery (FLAIR) volumes, and were acquired with different clinical protocols and various scanners from multiple (n=19) institutions, mentioned as data contributors here.

    and

    The provided data are distributed after their pre-processing, i.e. co-registered to the same anatomical template, interpolated to the same resolution (1 mm^3) and skull-stripped.

    So the header will not help in this case (equal dimensions for all modalities due to preprocessing).

    If you are looking for the post-contrast T1-weighted (T1Gd) images then it's the 2nd dimension so use:

    data = a.get_fdata()
    modality_1 = data[:,:,:,1]
    

    Additionally, we can visualize the each 3D volume (data[:,:,:,0], data[:,:,:,1],data[:,:,:,2], data[:,:,:,3]) and verify my statement.

    See here: https://gofile.io/?c=fhoZTu