Change segmentation oversampling factor


I have been trying to mimic SegmentationGeometryWidget in python. In the widget, I can select a segmentation node and give a oversampling factor. After pressing the OK button, I will get a modified segmentation.

I have searched for some python implementation, such as Update oversampling factor and Create cylinder shell shaped segment. However, SetConversionParameter seems not working at all for me.

Here’s my code. Am I doing anything wrong or do I miss anything else? Thank for helping.

# Create the segmentation, the segmentation is created correctly.
segmentationNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLSegmentationNode")
segmentationNode.AddSegmentFromBinaryLabelmapRepresentation(orientedImage, "Segmentation")

#Modify the oversampling factor
segmentationNode.GetSegmentation().SetConversionParameter('Oversampling factor', str(self.oversamplingFactorSpinBox.value))
segmentationNode.GetSegmentation().CreateRepresentation('Binary labelmap', True)

By the way, the reason I tried to code the function is I can’t correctly show the SegmentationGeometryWidget. I have tried to open it based on How, through python, to reach or open Segmentation-Geometry-Widget dialog or set up parameters before opening a dialog?, but the widget will suddenly open and then close itself.

Conversion parameters are only used if conversion is performed.

If you have a closed surface master representation then you can delete the binary labelmap representation and then recreate it.

If you already have a binary labelmap representation then you need to resample manually. In latest Slicer Preview Release, we have made manual resampling conveniently available from Python (vtkSlicerSegmentationGeometryLogic::ResampleLabelmapsInSegmentationNode method).

@lassoan Thanks for your reply!

It’s good to know that there’s a simple way to resample the binary labelmap in preview release. However, I’m afraid I can’t switch to latest Slicer Preview Release due to other issues. My current working version of Slicer is 4.11, is there any code example for manually resample binary labelmap?

You need to run [all the steps implemented in vtkSlicerSegmentationGeometryLogic::ResampleLabelmapsInSegmentationNode method. See the source code here.

We’ll soon replace the stable release with the content of the current preview release. What issues prevent you from upgrading?

1 Like

@lassoan Thanks for your help again!

The source code of vtkSlicerSegmentationGeometryLogic::ResampleLabelmapsInSegmentationNode is really helpful, and I finally completed this function correctly based on the source code. The issues aren’t technical issues, and since it works right now, there’s no needs to worry about upgrading.

For those who are interested, here is my code.

segmentIDs = vtk.vtkStringArray()

#Create desired geometryImageData with overSamplingFactor
segmentationGeometryLogic = slicer.vtkSlicerSegmentationGeometryLogic()
geometryImageData = segmentationGeometryLogic.GetOutputGeometryImageData()

for index in range(segmentIDs.GetNumberOfValues()):
    currentSegmentID = segmentIDs.GetValue(index)
    currentSegment = segmentationNode.GetSegmentation().GetSegment(currentSegmentID)
    currentLabelmap = currentSegment.GetRepresentation("Binary labelmap")

    success = slicer.vtkOrientedImageDataResample.ResampleOrientedImageToReferenceOrientedImage(currentLabelmap, geometryImageData, currentLabelmap, False, True)

    if not success:
            print("Segment {}/{} failed to be resampled".format(segmentationNode.GetName(), currentSegmentID))


Dear Andras Lasso,
Is it possible to upgrade Slicer and stay with my current extensions and self written modules? I mean that without installing the new version and stay on my current (in the point of view of extensions and self added features) with only updating the slicer core, that includes initial modules?
Where I can read about this process?

Thanks a lot!
Fantastic decision!
I’ve inserted some additional lines for my case and the flight is normal!)

We try to preserve backward API compatibility between Slicer versions, so some Python modules may work without changes on more recent Slicer versions. ABI compatibility is not preserved between Slicer versions, so you need to reinstall all extensions that contain modules implemented in C++.

Slicer-5.0.3 allows you to bookmark extensions and reinstall all your bookmarked extensions by a single click after you upgrade Slicer core.

1 Like