Change segmentation oversampling factor

@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()
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()
3 Likes