show_image.py 930 B

1234567891011121314151617181920212223242526
  1. import pandas as pd
  2. import nibabel as nib
  3. import matplotlib.pyplot as plt
  4. '''
  5. Function to load and show image. If the image is NL, control must be true.
  6. '''
  7. def show_image(image_id, show=True, data_path='./ADNI_volumes_customtemplate_float32/', annotations_path='./LP_ADNIMERGE.csv'):
  8. print('Image ID: ' + str(image_id))
  9. annotations_file = pd.read_csv(annotations_path)
  10. image_info = annotations_file[annotations_file["Image Data ID"] == image_id]
  11. print(image_info)
  12. if image_info.iat[0,0] == "CN":
  13. loaded_data = nib.load(data_path + 'Inf_NaN_stableNL__I' + str(image_id) + '_masked_brain.nii.nii').get_fdata()
  14. else:
  15. loaded_data = nib.load(data_path + 'Inf_NaN_stableAD__I' + str(image_id) + '_masked_brain.nii.nii').get_fdata()
  16. print(loaded_data.shape) # Dimensions: (91, 109, 91)
  17. if show:
  18. image = loaded_data[:, :, 40]
  19. plt.imshow(image)
  20. plt.show()