Resample segments in segmentation using Python scripting

Is there a way to force the segments to the common image geometry through python code?

I am importing segmentations from a volumelabelmap and I can’t run ‘fill between slices’ on the imported segments.

Fill between slices should resample all segments into the same geometry. How do you know that it does not?

You can force resampling by applying any operation on it (e.g., paint and erase a point) or saving and loading.

Hi Andras,

I am trying to change the current segmentation node used by the segment editor programmatically. We have a drop-down menu to specify the rough mask (segmentation node) in our module.

We want to select another segmentation node and apply edits on that. However, let’s say for example my rough mask has a segment on slice 1 and 10. When I change to that rough mask and try to fill between slices, nothing happens. But if I apply any paint, even the tiniest bit, on those segments and run fill between slices, it works. So, I am guessing that’s what you meant by any edit forces resampling. However, this is quite time consuming if I have many slices to edit. Therefore, I was asking if this resampling can be done programmatically through python rather than manually applying an edit.

Here is the code I use to import a rough mask into the segment editor, we initially have the rough mask as a label map that we convert to a segmentation node:

def changeRoughMask(self, roughMaskNode, masterVolumeNode, segmentEditor):
    segmentNode = slicer.mrmlScene.GetNodeByID(self._segmentNodeId)

    if (roughMaskNode and masterVolumeNode):
      segmentNode = slicer.mrmlScene.AddNewNodeByClass('vtkMRMLSegmentationNode')
      self._segmentNodeId = segmentNode.GetID()
      self.labelmapToSegmentationNode(roughMaskNode, segmentNode)
      # set segmentation node and master volume node in segmentation editor
      # update viewer windows
                                      label= segmentNode,

      return True
    return False


This should resample all the segments in the segmentation using the current geometry:

segmentationNode = getNode('Segmentation')
segGeomLogic = slicer.vtkSlicerSegmentationGeometryLogic()