Preserve colors from segment editor node and export it as Nifti LabelMap

Slicer 5.2.2

Hello, i am currently using segment editor and selected Generic Color Table and made following sample segments.

and then i move the segments to segmentation module and export it as a new label map, once the new label map is created and the segments are exported to the new LabelMap node, on the main screen i selected the new label map and it was preserving the segment colors.

But once i save the label map to my destination folder and reload it again with the scan, the colors are not preserved. (its changing to color code 1 and color code 2)

I want to preserve the colors that have been used at the segment editor in the saved Nifti label map. i need the output folder to have a Nifti LabelMap as i will be using python to read Nifti files and extract segmented regions based on the color codes in the LabelMap.

This came up recently in this thread. This is the important sentence from the documentation:

The label value is index of the color table entry that has the same name as the segment name.

Do you have entries called 31 and 307 in your custom color table?

its not a custom table, i have selected the colors from “GenericColors” and the colors were named 31 and 307. so gave the segments same name.

The point is you do have to create a custom color table, if you want the indices to reflect your values.

Saved that genericColor table, somewhere on the disk, and edit with text editor to change the indices (the leftmost value in the rows) to match your indices. Save it. Load into your scene, and export your segmentation one more time to a labelmap by specifying the in the Segmentation module
Here is an example one that exports my labels with indices 35, 42, 74, 500 and 502

# Color table file /Users/amaga/Documents/35_mic_23strain_mus_template_UCHAR-label.nii.gz-label_ColorTable.ctbl
# 6 values
35 Skull 128 174 128 255
42 Right_Mandible 241 214 145 255
74 Vertebra 177 122 101 255
400 Left_Mandible 111 184 210 255
502 Endocast 216 101 79 255
1 Like

there is already a color node called “GenericColors” which is available as one of the standard options in 3D slicer. i was just using this color table.

However, the error occurs when I export the segment regions created using ‘Segment Editor’ module as a label map using the ‘Segmentations’ module. when I load the saved label file again in a new scene The colors are not preserved, irrespective of any names given to a segment.

I will also give the custom color table solution a try.

You should create a custom table, if you want to use your own colors. As you can see number 1 is green, and two is yellow in GenericColors, and since you specified that it rightly exported the file with those colors and indices.

@Vishal_P here are some instructiosn with MRHead.
Create three blank segments with names, Red, Blue and Green, and assign those colors to these segments. Then proceed with generating some segmentation like this:

Go to the segmentations module, and export this as a labelmap (without specfying any color table). Notice that exported labelmap has correct colors. Then save this labelmap and reload this into Slicer, and you will see that colors are now green yellow and brown, because without knowing anything about your custom colors, slicer applied the Generic Color table.

During the export, Segmentations actually exported your custom color table as well. So go to the volumes module and change the assigned color table from the Lookup table and scroll down to find your table (which in my case is a called Segmentation-label_ColorTable). When you switch to this, you should see your original colors.

Now, if you also want to change the indices from 1, 2, 3 to some custom numeric index, you should save this color table to disk, open it and edit the index values and then reload, and re-export using this color table (e.g., in the screenshot below, the indices in the labelmap is now 100, 200, 300, and colors are the original colors).

The point is, labelmap representation does know not anything about the colors it needs to be assigned. You need to specify that yourself.

1 Like

Thanks for the explanation. Now, I understand what is happening. Is there any way that, when reloading the label file in the ‘Add Data into the Scene’ screen and clicking on ‘Show Options,’ it automatically detects the intended color map (in your example, ‘Segmentation-label_ColorTable’)?"

and i also noticed if the segments are directly saved without exporting to label map, in “.seg.nrrd” the colors are preserved.

You should always save your segmentation in the seg.nrrd format for archival purposes, particularly if you care for data fidelity. labelmap doesn’t have those features hence is only supported as a “Export” format.

It will be probably be more convenient if you simply have your custom color map added to the Slicer defaults, and you can choose the correct color map from the volume lookup table. I imagine it is also possible to automate this through some customization script.