Renaming segments based on .txt file

Dear all,
I’m new to Slicer which I learning and finding fantastic, steep learning curve though. So I hope this is not too obvious but I have a lot of segments which are numbered and I would like to rename them based on a txt file, is this possible ?

I previously found this thread

but I am not sure how this would address my point.

Thanks for your help
best

This is a good question. If you have a color table (specifying name and color for each label value) then load that first into the application and then when you load the segmentation select this color table as Color node in the Options section.

Specification of the color table file format is available here.

Unfortunately, I have in excess of 800 segments so the color table would not be convenient. I have these segments (and apologies if I am mixing terms such as segments and objects) under a seg.nrrd. file where all segments are within one file or +800 single .obj files so I guess my best best is to rename the individual files which can be easily done using a text file and a bit of command lines.
Thanks

There is no link attached. I am also curious to learn this…

Good catch, I’ve fixed the link above.

You might have misunderstood how a color table can be created or used. It is just a text file that you should be able to very easily create using Excel or any text editor, from the list of segment names that you already have. You can then load it into the scene and select it when loading the segmentation to use it for naming segments.

You can of course modify segment names in the .seg.nrrd file as well, as segment names are stored in plain text. To allow easier editing, you can save the segmentation file in .seg.nhdr format, which has the header in a separate text file.

If you have your segmentation in .obj files then you can load all of them into Slicer and import them into a segmentation (for example, you can put them into a folder in Data module, right-click on the folder, and choose “Convert models to segmentation node”).

1 Like

I am also interested in this, but it is not clear how the matching is done. I saved that ctbl file and loaded into the Slicer session. Then, I created a random segmentation from MRhead with three segments (which went segment_1, segment_2), saved the segmentation and reloaded the seg_nrrd file with the color table option. Instead of artery, bone and connective_tissue it came back with segment_1, segment_2.

Do the colors have to match exactly? Or is it the order of the segments?

If you you load a .seg.nrrd file then you already have the segment names in the file, so the color table is not needed - it will not be used.

If you export the segmentation into a simple labelmap file (.nrrd) that does not have any of the segmentation metadata then the color table will be used. Labelmap voxel values are matched to color table index. Number of segments or entries in the color table does not matter. Order in color table probably does not matter but in all files that Slicer creates and that I have seen so far have entries listed in ascending order based on color index.

We are working on segmentations by multiple users/groups. The goal is to use the results for ML training. Maintaining consistency and ordering of the labels/segments has been a challenge to say the least. We haven’t used the terminology as our terms are very different than what’s available.

Is that what you would suggest?

We recognized several years ago that it was not going to be possible to aggregate data from multiple sources, collected for various purposes and rely on a single mapping of label value → content. That’s why terminology support was introduced. It was probably too much ahead of its time and users just start to realize now why it is essential to use it.

Our proposed solution is to use seg.nrrd file for archiving data, which specifies the meaning of each label value (and volume layer, if there are overlapping segments) by using a coded term. Then, when the data is loaded from the seg.nrrd file, the user can choose what label values will be assigned to each term, for example using slicerio. The slicerio Python package that can be pip-installed in any Python environment, which makes seg.nrrd file easily usable as a source data in any data preparation workflows.

For users convenience, in the Slicer installer package we included two terminologies with some commonly used terms. However, this does not mean that you must use these terms and you could not use any other terms.

You can use any other terminology (TA2, TA98, MeSH, FMA, …) or if you find that any term is missing then you can specify your own. If you define your own terms it can make sense to add it to your Slicer extension so that your users can access it easily. The terminology file format should be self-explaining but you can learn more about it in DCMQI project documentation. You can use terminology file in the SlicerHeart project as a simple example.

1 Like