threshold_whitematter.py 729 B

123456789101112131415161718192021222324
  1. # -*- coding: utf-8 -*-
  2. import os
  3. import nibabel as nib
  4. import numpy as np
  5. # Set up the directory paths
  6. code_dir = os.path.dirname(os.path.abspath(__file__))
  7. parent_dir = os.path.dirname(code_dir)
  8. file_path = os.path.join(parent_dir, "input", "C57BL6_mouse_qa.nii.gz")
  9. output_path = os.path.join(parent_dir, "output", "C57BL6_mouse_qa.nii.gz")
  10. # Load the image
  11. img = nib.load(file_path)
  12. data = img.get_fdata()
  13. # Thresholding
  14. threshold_value = 0.3 * np.max(data)
  15. mask_data = np.where(data >= threshold_value, 1, 0)
  16. # Save the mask
  17. output_path = output_path.replace("C57BL6_mouse_qa.nii.gz", "White_matter_mask.nii.gz")
  18. mask_img = nib.Nifti1Image(mask_data, img.affine)
  19. nib.save(mask_img, output_path)