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.
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).
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?
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()
segmentationNode.GetSegmentation().GetSegmentIDs(segmentIDs)
#Create desired geometryImageData with overSamplingFactor
segmentationGeometryLogic = slicer.vtkSlicerSegmentationGeometryLogic()
segmentationGeometryLogic.SetInputSegmentationNode(segmentationNode)
segmentationGeometryLogic.SetSourceGeometryNode(segmentationNode)
segmentationGeometryLogic.SetOversamplingFactor(0.5)
segmentationGeometryLogic.CalculateOutputGeometry()
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))
segmentationNode.Modified()