# Centerline extraction from python

**URL:** <https://discourse.slicer.org/t/centerline-extraction-from-python/16728>\
**Category:** VMTK\
**Tags:** extensions-manager, python\
**Created:** [March 23, 2021, 9:11pm UTC](https://discourse.slicer.org/t/centerline-extraction-from-python/16728 "2021-03-23T21:11:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ge\_Tang](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/ge_tang/32/9935_2.png) [@Ge\_Tang](https://discourse.slicer.org/u/Ge_Tang)\
**Post date:** [March 23, 2021, 9:11pm UTC](https://discourse.slicer.org/t/centerline-extraction-from-python/16728/1 "2021-03-23T21:11:24Z")

</div>

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:

```python
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

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [March 24, 2021, 4:19pm UTC](https://discourse.slicer.org/t/centerline-extraction-from-python/16728/2 "2021-03-24T16:19:29Z")

</div>

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](https://www.slicer.org/wiki/Documentation/Nightly/Extensions/DebuggingTools).

---

<div class="post-metadata">

**Author:** ![Ge\_Tang](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/ge_tang/32/9935_2.png) [@Ge\_Tang](https://discourse.slicer.org/u/Ge_Tang)\
**Post date:** [March 25, 2021, 5:14pm UTC](https://discourse.slicer.org/t/centerline-extraction-from-python/16728/3 "2021-03-25T17:14:01Z")

</div>

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​. 🙂
