Centerline extraction from python

Operating system: Mac
Slicer version:4.11
Expected behavior: The centerline model from GUI of slicer has a different number of points compared with the centerline model I get from the python script
Actual behavior:

This is the code I use in python to get a centerline model:

import ExtractCenterline
def centerlineExtractcenterline(nameSeg:str, nameRegion:str, nameFids:str):
    ec = ExtractCenterline.ExtractCenterlineLogic()
    n = getNode(nameSeg) 
    s = n.GetSegmentation()
    ss = s.GetSegment(s.GetSegmentIdBySegmentName(nameRegion)).GetRepresentation('Closed surface')
    fids = getNode(nameFids)
    centerlinePolyData,voronoiDiagramPolyData = ec.extractCenterline(ss, fids)
    return centerlinePolyData, voronoiDiagramPolyData

centerlinePolyData, voronoiDiagramPolyData = centerlineExtractcenterline('seg_nerve', 'R_optic_nerve', 'F1')

model = slicer.vtkMRMLModelNode()
model.SetAndObservePolyData(centerlinePolyData)
modelDisplay = slicer.vtkMRMLModelDisplayNode()

modelDisplay.SetColor(1, 1, 0)
modelDisplay.BackfaceCullingOff()
modelDisplay.SetOpacity(0.8)
modelDisplay.SetPointSize(3)

modelDisplay.SetSliceIntersectionVisibility(True)
modelDisplay.SetVisibility(True)
slicer.mrmlScene.AddNode(modelDisplay)
model.SetAndObserveDisplayNodeID(modelDisplay.GetID())
modelDisplay.SetInputPolyDataConnection(model.GetPolyDataConnection())
slicer.mrmlScene.AddNode(model)

Thank you so much!

Ge Tang

I don’t see any obvious problem with your code snippet. I would recommend to compare the execution path in the Centerline module GUI by stepping through the code step by step using a Python debugger.

Thank you Dr. Lassonan. I figured out that the python code did not run the preprocess input surface. But the GUI set this as default​. :slightly_smiling_face: