softbrainmask.py 714 B

12345678910111213141516171819
  1. from templateflow import api
  2. from scipy.ndimage import gaussian_filter
  3. import nibabel as nb
  4. import numpy as np
  5. from skimage.morphology import ball
  6. from scipy.ndimage import morphology as m
  7. path = api.get('MNI152NLin2009cAsym', desc='brain', resolution=1, suffix='mask')
  8. im = nb.load(path)
  9. data = np.asanyarray(im.dataobj).astype(bool)
  10. dilate1 = m.binary_dilation(data == 1, ball(1))
  11. dilate2 = m.binary_dilation(dilate1, ball(1))
  12. soft = 0.95 * dilate2
  13. soft[dilate1] = 1.0
  14. gauss = gaussian_filter(soft.astype('float32'), 1)
  15. header = im.header.copy()
  16. header.set_data_dtype('float32')
  17. nb.Nifti1Image(gauss.astype('float32'), im.affine, header).to_filename('tpl-MNI152NLin2009cAsym_res-01_desc-brain_probseg.nii.gz')