slicer.modules.segmentations.logic().ImportLabelmapToSegmentationNode() Not workign with given SegmentIDs

Hi, having this issue trying to convert labelmap volume to a segmentation node.
My labelmap volume here has only 0s and 1s, so theoretically, only one segment will be created running this function.

If I run the following code without specifying segmentIDs, everything works fine:

slicer.modules.segmentations.logic().ImportLabelmapToSegmentationNode(volumeNode, segmentationNode)

The function returns true and expected segment is created with the default name (“jake”)

I want to give the segment a specific ID, so I can call it afterwards by running:

id = 'Fem_P'
ids = vtk.vtkStringArray()
ids.InsertNextValue(id)
slicer.modules.segmentations.logic().ImportLabelmapToSegmentationNode(volumeNode, segmentationNode, ids)

The function returns True and does NOT create segments.

Anyone knows why? Did I do something wrong when creating the vtkStringArray?

Thanks ahead

I’ve created a PR that should fix the issue here: BUG: Create non-existing segments in ImportLabelmapToSegmentationNode by Sunderlandkyl · Pull Request #6835 · Slicer/Slicer · GitHub

Thanks for the quick PR on fixing this. Now I understand that to work around this problem, I can create an empty segment with the ID that I want to name it.

The following code works now:

id = ‘Fem_P’
segmentationNode.GetSegmentation().AddEmptySegment(id)
ids = vtk.vtkStringArray()
ids.InsertNextValue(id)
slicer.modules.segmentations.logic().ImportLabelmapToSegmentationNode(volumeNode, segmentationNode, ids)

1 Like