Trouble generating Labelmap from segmentation

Good morning,
my goal is to automatically load segmentations and save them in a labelmap format, but i’m having trouble:
When i create the labelmap manually (from the segment editor module), save it manually and load it back its perfect. But when i try to create the labelmap by code one of the label level is sorrounded by another level (there is a grey level sorrounding it,and this is very harmful since i’m trying to train a model to do the segmentation)

How the labelmap looks when created by code before saving it:

How the labelmap looks after being saved and loaded back:

saved labelmap (black-white) vs original labelmap (colored)

Also: when i try to save the labelmap by code in the nightly version only one label is saved (it’s missing all the yellow/top part):

The code snippet i’m using:

slicer.util.loadSegmentation('Input_file_dir')
seg_node = slicer.util.getNode('Input_file_name')
merged_labelmap_node = slicer.vtkMRMLLabelMapVolumeNode()
slicer.mrmlScene.AddNode(merged_labelmap_node)
slicer.vtkSlicerSegmentationsModuleLogic.ExportAllSegmentsToLabelmapNode(seg_node, merged_labelmap_node)
slicer.util.saveNode(merged_labelmap_node, output_file_dir)

Any help is greatly appreciated

If segments do not overlap then in recent Slicer Preview Releases, segmentation is already saved in a simple 3D labelmap format (with some additional metadata that you can safely ignore).

Do you need help with accessing individual slices of a 3D labelmap?

What is your complete data flow for generating training data?

to create my training dataset i manually segment my area of interest (a small size cropped volume from a head cbct containing my ROI (the TMJ artic.)) in 3 parts: mandible, cranium, soft tissue. I also smooth them by joint smoothing. Segments should never overlap each other.
Since i already have many saved segmentations (and volumes) i just wanted to convert the segs to labelmaps automatically to create the dataset to feed the model. (i use the code generated by my script directly on py interactor of slicer since i dont want to use a CLI)
I don’t need to access the single slices (i’m using niftynet which does all the deep learning work for me). I’m just concerned (looking at the first trained models i’m getting) that the little grey border on the mandible (the green label in the pics) is actually recognized by the model as cranium (which is actually grey in the scalar volume).
I did not try to train directly with the segmentations files from the latest nightly. Neither did i try using .nrrd files instead of .nii

after some research i discovered that my problem was in the interpolation, both in slicer (that altered the scalar volume image to have that grey border around my black label level (pic 2 of the post above)) and in NiftyNet (that altered the image in the same way). By disabling the interpolation on slicer and setting it to nearest neighbor in NN (just for labels) i think i solved my problem.
Thanks @lassoan

1 Like