Use NiBabel to read MRI of brain in Nifti format


In [1]:
import nibabel as nib


/Users/ganthony/anaconda2/envs/neon/lib/python2.7/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
  from ._conv import register_converters as _register_converters

In [2]:
img = nib.load("Brats17_CBICA_AAB_1_flair.nii.gz")

In [3]:
img


Out[3]:
<nibabel.nifti1.Nifti1Image at 0x110292610>

In [14]:
img.get_data().shape


Out[14]:
(240, 240, 155)

In [38]:
img.get_qform()


Out[38]:
array([[ -1.,   0.,   0.,   0.],
       [  0.,  -1.,   0., 239.],
       [  0.,   0.,   1.,   0.],
       [  0.,   0.,   0.,   1.]])

In [40]:
img.get_filename()


Out[40]:
'Brats17_CBICA_AAB_1_flair.nii.gz'

In [45]:
img.header.keys() # These are the metadata from the DICOM file


Out[45]:
['sizeof_hdr',
 'data_type',
 'db_name',
 'extents',
 'session_error',
 'regular',
 'dim_info',
 'dim',
 'intent_p1',
 'intent_p2',
 'intent_p3',
 'intent_code',
 'datatype',
 'bitpix',
 'slice_start',
 'pixdim',
 'vox_offset',
 'scl_slope',
 'scl_inter',
 'slice_end',
 'slice_code',
 'xyzt_units',
 'cal_max',
 'cal_min',
 'slice_duration',
 'toffset',
 'glmax',
 'glmin',
 'descrip',
 'aux_file',
 'qform_code',
 'sform_code',
 'quatern_b',
 'quatern_c',
 'quatern_d',
 'qoffset_x',
 'qoffset_y',
 'qoffset_z',
 'srow_x',
 'srow_y',
 'srow_z',
 'intent_name',
 'magic']

In [9]:
import matplotlib.pyplot as plt

In [13]:
plt.imshow(img.get_data()[:,:,80], cmap="bone")


Out[13]:
<matplotlib.image.AxesImage at 0x114a50690>

In [15]:
plt.imshow(img.get_data()[:,:,75], cmap="bone")


Out[15]:
<matplotlib.image.AxesImage at 0x114b51310>

In [ ]:


In [ ]:


In [ ]:


In [ ]:

Use SimpleITK to read MRI brain in Nifti format


In [18]:
import SimpleITK as sitk

In [19]:
imgS = sitk.ReadImage("Brats17_CBICA_AAB_1_flair.nii.gz")

In [25]:
z = 75

plt.imshow(sitk.GetArrayViewFromImage(imgS)[z,:,:], cmap="bone")


Out[25]:
<matplotlib.image.AxesImage at 0x1126e0c50>

In [28]:
imgS.GetMetaDataKeys()  # These are the meta data from the DICOM file


Out[28]:
('ITK_FileNotes',
 'aux_file',
 'bitpix',
 'cal_max',
 'cal_min',
 'datatype',
 'descrip',
 'dim[0]',
 'dim[1]',
 'dim[2]',
 'dim[3]',
 'dim[4]',
 'dim[5]',
 'dim[6]',
 'dim[7]',
 'dim_info',
 'intent_code',
 'intent_name',
 'intent_p1',
 'intent_p2',
 'intent_p3',
 'pixdim[0]',
 'pixdim[1]',
 'pixdim[2]',
 'pixdim[3]',
 'pixdim[4]',
 'pixdim[5]',
 'pixdim[6]',
 'pixdim[7]',
 'qform_code',
 'qform_code_name',
 'qoffset_x',
 'qoffset_y',
 'qoffset_z',
 'quatern_b',
 'quatern_c',
 'quatern_d',
 'scl_inter',
 'scl_slope',
 'sform_code',
 'sform_code_name',
 'slice_code',
 'slice_duration',
 'slice_end',
 'slice_start',
 'srow_x',
 'srow_y',
 'srow_z',
 'toffset',
 'vox_offset',
 'xyzt_units')

In [29]:
imgS.GetMetaData("vox_offset")


Out[29]:
'2880'

In [34]:
imgS.GetMetaData("srow_x")


Out[34]:
'-1 -0 -0 0'

In [ ]: