2
$\begingroup$

How do you programmatically in Python open and read segmentation data in an aparc+aseg.nii.gz file programmatically?

Is this correct?

import numpy as np
from pathlib import Path
import torchio as tio

dataset_dir_name = '/home/shared/projects/BrainSegNet3D/data'
dataset_dir = Path(dataset_dir_name)
labels_dir = dataset_dir / 'label'
label_paths = sorted(labels_dir.glob('*.nii.gz'))
label_path = label_paths[0]
abcd_free_surfer_labels = tio.LabelMap(label_path)
free_surfer_labels = abcd_free_surfer_labels.data[0].numpy()
np.unique(free_surfer_labels.astype('int'))

array([   0,    2,    4,    5,    7,    8,   10,   11,   12,   13,   14,
         15,   16,   17,   18,   24,   26,   28,   30,   31,   41,   43,
         44,   46,   47,   49,   50,   51,   52,   53,   54,   58,   60,
         62,   63,   72,   77,   80,   85,  251,  252,  253,  254,  255,
       1000, 1001, 1002, 1003, 1005, 1006, 1007, 1008, 1009, 1010, 1011,
       1012, 1013, 1014, 1015, 1016, 1017, 1018, 1019, 1020, 1021, 1022,
       1023, 1024, 1025, 1026, 1027, 1028, 1029, 1030, 1031, 1032, 1033,
       1034, 1035, 2000, 2001, 2002, 2003, 2005, 2006, 2007, 2008, 2009,
       2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020,
       2021, 2022, 2023, 2024, 2025, 2026, 2027, 2028, 2029, 2030, 2031,
       2032, 2033, 2034, 2035])

Why are some of the numbers so large? Does FreeSurfer really distinguish between over 2000 regions of the brain?

$\endgroup$
3
  • $\begingroup$ I am not sure you can open a nii.gz file with nibabel.freesurfer.io.read_annot method. From the documentation, it is for opening the *.annot files. To read nii.gz files, you may simply be able to use, nibabel.load method. $\endgroup$ – ssm Mar 29 at 21:16
  • $\begingroup$ And then what? It's the gory details that I need. $\endgroup$ – Paul Reiners Mar 29 at 21:21
  • $\begingroup$ nipy.org/nibabel/gettingstarted.html might help accessing data in the file. $\endgroup$ – ssm Mar 29 at 21:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.