Is it possible to apply a lookuptable to a label map using python?

I have two separate files, one being a lookup table , and the other being a .nii labelvolume.

I know that you can use slicer.util.loadColorTable() , as well as slicer.util.loadLabelVolume() to load the files into slicer, but I haven’t found a way to apply the lookuptable to the volume as a python command/script.

By hand i can easily do this, but I have 5000+ image/table pairs that need to be combined before processing into segmentations


you can use slicer.util.getNode() to find the volume node.

Then with that volume node, you can use GetDisplayNode() to create a sub-node that contains all the display data (which includes where the Look up Table reference is).

with the displayNode , you can call SetAndObserveColorNodeID([ColorTable]) , where [ColorTable] is the lookup table that you loaded initially. [ColorTable] has to be a string that corresponds to whatever the label of the Lookup Table is.

In my case, since I only loaded a single ColorTable , it was labeled as ‘vtkMRMLColorTableNode1’ . You can check what the current ColorTableNode is by printing out the node, as well as you can check what the loaded colorTable node name is.

After the lookup table was applied, I created another node that references the original volume, and then used slicer.mrmlScene.AddNewNodeByClass(‘vtkMRMLSegmentationNode’) to create a new segmentation node.

slicer.modules.segmentations.logic().ImportLabelmapToSegmentationNode(labelmapVolumeNode, segNode) was used to create the segmentation.

slicer.util.saveNode(slicer.mrmlScene.GetNodeByID(“vtkMRMLSegmentationNode1”), “/tmp/foo.seg.nrrd”) was then used to save the labeled segmentations to a directory as a .seg.nrrd file.

replace the directory name with wherever you want the output segment file to be written.

When reloading the output segment file, all Label and Color information is saved properly, and is read back properly into slicer

EDIT: there are probably ways to label the node references as they are being created/loaded, instead of having them be labeled ‘ColorNode1 / SegmentationNode1’ , but I am new to Slicer, and haven’t looked up how to do it yet.