# Centerline extraction C++ having different result from the slicer

**URL:** <https://discourse.slicer.org/t/centerline-extraction-c-having-different-result-from-the-slicer/32370>\
**Category:** Development\
**Tags:** centerlines, vmtk-centerline\
**Created:** [October 22, 2023, 7:43am UTC](https://discourse.slicer.org/t/centerline-extraction-c-having-different-result-from-the-slicer/32370 "2023-10-22T07:43:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![liy0na](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/liy0na/32/67951_2.png) [@liy0na](https://discourse.slicer.org/u/liy0na)\
**Post date:** [October 22, 2023, 7:43am UTC](https://discourse.slicer.org/t/centerline-extraction-c-having-different-result-from-the-slicer/32370/1 "2023-10-22T07:43:16Z")

</div>

Hello,

I have an [STL file](https://www.dropbox.com/scl/fi/8gt64nbh0za9yy6gctdiy/surface_mesh.stl?rlkey=c51rxxzg81vctjuod4saaatit&dl=0) containing a surface mesh.

When I use the slicer I can easily extract the centerlines from it.

 ![slicer](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/8/4/84026e7831f4d1feb6f6339b3ab3a39d899d0620.png)

However when I use the following C++ code. I am only getting 3 output points.  
vtkSmartPointer reader = vtkSmartPointer::New();  
reader-\>SetFileName( “D:\Projects\cooling to test\conformal\surface\_mesh.stl” );  
reader-\>Update();

vtkSmartPointer inletSeedIds = vtkSmartPointer::New();  
vtkSmartPointer outletSeedIds = vtkSmartPointer::New();

//Locate points  
vtkSmartPointer pointLocator = vtkSmartPointer::New();  
pointLocator-\>SetDataSet( reader-\>GetOutput() );  
pointLocator-\>BuildLocator();

double sourcePoint[3];  
sourcePoint[0] = -6.092;  
sourcePoint[1] = -6.036;  
sourcePoint[2] = 128.186;

double targetPoint[3];

targetPoint[0] = -15.177;  
targetPoint[1] = -6.048;  
targetPoint[2] = 128.183;

int sourceid = pointLocator-\>FindClosestPoint( sourcePoint );  
int targetid = pointLocator-\>FindClosestPoint( targetPoint );

inletSeedIds-\>InsertNextId( sourceid );  
outletSeedIds-\>InsertNextId( targetid );

//// Compute Centerlines  
vtkSmartPointer centerlineFilter = vtkSmartPointer::New();  
centerlineFilter-\>SetInputData( reader-\>GetOutput() );

centerlineFilter-\>SetSourceSeedIds( inletSeedIds );  
centerlineFilter-\>SetTargetSeedIds( outletSeedIds );  
centerlineFilter-\>SetRadiusArrayName( “MaximumInscribedSphereRadius” );  
centerlineFilter-\>SetCostFunction( “1/R” );  
centerlineFilter-\>SetFlipNormals( false );  
centerlineFilter-\>SetAppendEndPointsToCenterlines( 0 );  
centerlineFilter-\>SetSimplifyVoronoi( 0 );  
centerlineFilter-\>SetCenterlineResampling( 0 );  
centerlineFilter-\>SetResamplingStepLength( 1.0 );  
centerlineFilter-\>Update();

// access centerline points  
vtkSmartPointer centerlinesPolyData = vtkSmartPointer::New();  
centerlinesPolyData = centerlineFilter-\>GetOutput();

vtkSmartPointer points = centerlinesPolyData-\>GetPoints();  
sim::Dbl3Array centerline\_points\_array;

int numberOfCells = centerlinesPolyData-\>GetNumberOfCells();  
int numberOfPoints = centerlinesPolyData-\>GetNumberOfPoints();  
int numberOfLines = centerlinesPolyData-\>GetNumberOfLines();  
//int numberOfVerts = centerlinesPolyData-\>GetNumberOfVerts();  
//int numberOfStrips = centerlinesPolyData-\>GetNumberOfStrips();  
int numberOfPieces = centerlinesPolyData-\>GetNumberOfPieces();  
int numberOfPolys = centerlinesPolyData-\>GetNumberOfPolys();

I would appreciate it if you could let me know what I am doing wrong.

---

<div class="post-metadata">

**Author:** ![Lulu](https://avatars.discourse-cdn.com/v4/letter/l/dec6dc/32.png) [@Lulu](https://discourse.slicer.org/u/Lulu)\
**Post date:** [August 9, 2024, 4:10am UTC](https://discourse.slicer.org/t/centerline-extraction-c-having-different-result-from-the-slicer/32370/2 "2024-08-09T04:10:52Z")

</div>

Hi，  
I have the same problem as you.Apply the same code but generate a different centerline than the one generated in slicer.  
Have you solved the problem？I would appreciate it if you have any suggestions

---

<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:** [August 12, 2024, 12:47pm UTC](https://discourse.slicer.org/t/centerline-extraction-c-having-different-result-from-the-slicer/32370/3 "2024-08-12T12:47:56Z")

</div>

Slicer’s `Extract Centerline` module uses the VMTK C++ code via a thin Python wrapping layer so there is no difference due to using C++ or Python.

You get different results when you use the code above because you missed the required preprocessing steps - see the [centerline extraction module source code](https://github.com/vmtk/SlicerExtension-VMTK/blob/master/ExtractCenterline/ExtractCenterline.py).
