Niftis being plotted skewed

64 views Asked by At

When I plot single images they appear to be skewed, but doesn't appear that way when I look at the images in 3DSlicer or another viewer. I'm not sure if there's something I should be adjusting that I'm not aware of. The below is how I converted from DICOM:

dicom2nifti.convert_directory(path_to_dicom_before, path_to_dicom_before_converted, compression=True, reorient=True)
dicom2nifti.convert_directory(path_to_dicom_post, path_to_dicom_post_converted, compression=True, reorient=True)

print(glob(path_to_dicom_before_converted + '*.nii.gz'))

nii_before = nib.load(glob(path_to_dicom_before_converted + '*.nii.gz')[0])
nii_after = nib.load(glob(path_to_dicom_post_converted + '*.nii.gz')[0])

nii_before_data = nii_before.get_fdata()
nii_after_data = nii_after.get_fdata()

fig, ax = plt.subplots(figsize=[10, 5])
plotting.plot_img(nii_before, cmap='gray', axes=ax)
plt.show()

fig, ax = plt.subplots(figsize=[10, 5])
plotting.plot_img(nii_after, cmap='gray', axes=ax)
plt.show()

plt.imshow(nii_before_data[100], cmap='bone')
plt.axis('off')
plt.show()

Affine of the first:

[[-3.19454312e-01  7.17869774e-02  3.95075195e-02  6.01478424e+01]
 [ 5.83867840e-02  2.97792435e-01 -2.28872180e-01  1.27874863e+02]
 [ 4.69673797e-02  1.18071720e-01  5.53225577e-01  1.12181287e+03]
 [ 0.00000000e+00  0.00000000e+00  0.00000000e+00  1.00000000e+00]]

Image

1

There are 1 answers

0
Bilal On

As you can see in this answer you are plotting the row 100 with all columns and all slices! Also you need to plot the pixel array nii_before_data and not the whole Nifti image nii_before which contains other types of data.

you can try:

nii_before = nib.load(glob(path_to_dicom_before_converted + '*.nii.gz')[0])
nii_after = nib.load(glob(path_to_dicom_post_converted + '*.nii.gz')[0])

nii_before_data = nii_before.get_fdata()
nii_after_data = nii_after.get_fdata()

## Same goes for nii_after_data
if(len(nii_before_data.shape)==3):
   for slice_Number in range(nii_before_data.shape[2]):
       plt.imshow(nii_before_data[:,:,slice_Number ])
       plt.show()
if(len(nii_before_data.shape)==4):
   for frame in range(nii_before_data.shape[3]):
       for slice_Number in range(nii_before_data.shape[2]):
           plt.imshow(nii_before_data[:,:,slice_Number,frame])
           plt.show()

If you can provide a sample Nifti Image the solution might be more precise according to your data.