Error when trying SimpleITK with Segmentations / conversion

Dear all,

I would like to use SimpleITK with Segmentations. For this, I tried the approaches described by @lassoan here: Segmentation Using SimpleITK - #2 by lassoan

My inputSeg stems from a vtkMRMLSegmentationNode I get from a qMRMLNodeComboBox.

I first tried:

    labelmapVolumeNode = slicer.mrmlScene.AddNewNodeByClass('vtkMRMLLabelMapVolumeNode')

    segs = vtk.vtkStringArray()

    slicer.modules.segmentations.logic().ExportSegmentsToLabelmapNode(inputSeg, segs, labelmapVolumeNode)

    segimg = sitkUtils.PullVolumeFromSlicer(labelmapVolumeNode)

This gives the error:
RuntimeError: Exception thrown in SimpleITK ReadImage: /tmp/SimpleITK/Code/IO/src/sitkImageReaderBase.cxx:318:
sitk::ERROR: Logic error!

Trying sitk.ReadImage(sitkUtils.GetSlicerITKReadWriteAddress(labelmapVolumeNode.GetName())) gives the same error.

I then tried to get to the label map representation from the segmentation:

img = vtkSegmentationCore.vtkOrientedImageData()
segimg2 = inputSeg.GetBinaryLabelmapRepresentation(segs.GetValue(3), img)

But this results in a segimimg2 with NoneType that I cannot further process.

I do not need the label maps to be accessible through e.g. the Data module and use it only internally. That’s why I would prefer something like getting the label map without adding a node.

I am new to handling segmentations and need to use SimpleITK functions. Any help to get there highly appreciated!

I am switching to a scripted CLI, looks more straightforward to use my existing code based on SimpleITK