Getting saved labels from nibabel

I’m using ITK-SNAP for segmenting some brains and I can successfully draw them, change the colors, and rename their label names. I would like to access to this information from python and I’ve been always using nibabel for that, however I cannot find where to get information about the name of the labels I saved and their corresponding label value.

Edit: I’ve tried to save a loaded image and the information about the labels disappears. Maybe doing nib.load it does not load the name of the labels. Is there a way to write the name of those labels in python so they will be stored? My ultimate goal is to create a nii file with the name of those labels, color, etc.

What format data are you using, and how are labels stored? Are there associated files, or is this data intended to be part of the image itself?

The data is stored in .nii (the one I read and the one I write), but no, it’s not saved together with the brain image in the same file. The nii file I read/write it’s just a label file with .nii extensions whose values are discrete numbers representing the labels.

I don’t believe NIFTI has any way to store textual labels. So you’re storing the corresponding numbers, but if you want to recover the labels, you’ll need to store some correspondence between number and text, e.g., in an associated .json or .yml or .tsv file. Those can then be easily loaded in Python using common tools, such as json, PyYAML, csv.

That’s what I thought in the beginning but actually when I open the nifti files using ITK-SNAP they preserve the label names. I tried it with multiple scans and different label names and it seems that that information is stored probably in the nifti file. Maybe is that nibabel is not able to read it?

It’s possible that ITK-SNAP is either saving the label names in a separate file and automatically loading it or using a common lookup table. Another possibility is a header extension that we don’t know about. Would you be willing to share a copy of such a file?

I also have trouble working with photo labels .nii .
I have a folder called (LabelsTr) where (.nii) files are stored and I also have two other folders where brain tumor pictures are stored.
But I do not know how to find and recognize their labels.
Thank you for your help.
I want to give brain tumor pictures to the neural network