# Slicer - Convert centerlines to a graph {V,E}

**URL:** <https://discourse.slicer.org/t/slicer-convert-centerlines-to-a-graph-v-e/18604>\
**Category:** Support\
**Tags:** vtk, python, vmtk-centerline\
**Created:** [July 9, 2021, 3:55pm UTC](https://discourse.slicer.org/t/slicer-convert-centerlines-to-a-graph-v-e/18604 "2021-07-09T15:55:45Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![som1197](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/som1197/32/14945_2.png) [@som1197](https://discourse.slicer.org/u/som1197)\
**Post date:** [July 9, 2021, 3:55pm UTC](https://discourse.slicer.org/t/slicer-convert-centerlines-to-a-graph-v-e/18604/1 "2021-07-09T15:55:45Z")

</div>

Hello,

I want to convert the centerline network generated from the segmentations of the Circle of Willis into a graph data structure. Currently the only way to do it in vmtk is to use the **‘vmtknetworkextraction’** class to do so - [VMTK network extraction](http://www.vmtk.org/vmtkscripts/vmtknetworkextraction.html). However, it required the input to be a surface file.

In my case, I have a centerline tree hierarchical structure already generated in slicer as shown.

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/3/0/309d7940e0e25a607e520c8acf651e1fb4fe339f.png)

I can use the .json files containing the centerline curve information to create a graph using the NetworkX library in Python. But is there a way to directly convert the Centerline model\_1.vtk file into a graph using the connectivity information stored as an ordered dictionary?

I am using the following code to get numpy arrays from the vtk file.

```auto
# Using vmtk
filename = 'Centerline model_1.vtk'
centerlineReader = vmtkscripts.vmtkSurfaceReader()
centerlineReader.InputFileName = filename
centerlineReader.Execute()
clNumpyAdaptor = vmtkscripts.vmtkCenterlinesToNumpy()
clNumpyAdaptor.Centerlines = centerlineReader.Surface
clNumpyAdaptor.Execute()
numpyCenterlines = clNumpyAdaptor.ArrayDict

```

I get the following as an output:  
Points:  
 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/4/b/4bc40e657ffb82056aa1d7e073b36635dcf8248c.png)

PointData:

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/1/f/1f57b84fcdb882c18f9e1419e9038c8298955dbc.png)

What does ‘EdgeArray’ and ‘EdgePCoordarray’ mean here? How do they represent the edge connectivity of the 1D centerline tree?  
 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/1/0/108ee57991b71b709b10068415458b9427b29c17.png)

CellData:

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/0/3/03707e309dc775430e0af16831e87ddb35bc68c8.png)

Thank you!

---

<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:** [July 11, 2021, 3:54pm UTC](https://discourse.slicer.org/t/slicer-convert-centerlines-to-a-graph-v-e/18604/2 "2021-07-11T15:54:25Z")

</div>

You could easily add an option to the Extract Centerline module in Slicer to create the graph representation you need. See source code [here](https://github.com/vmtk/SlicerExtension-VMTK/blob/master/ExtractCenterline/ExtractCenterline.py#L1068-L1110) that generates a hierarchy of curves, with a little change you can put the point lists into a Python dictionary instead. If you send a pull request with the proposed changes then we’ll be happy to merge it.

---

<div class="post-metadata">

**Author:** ![som1197](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/som1197/32/14945_2.png) [@som1197](https://discourse.slicer.org/u/som1197)\
**Post date:** [July 16, 2021, 4:42am UTC](https://discourse.slicer.org/t/slicer-convert-centerlines-to-a-graph-v-e/18604/3 "2021-07-16T04:42:25Z")

</div>

Thank you @lassoan for the piece of code. Yes I will send out a pull request with the proposed changes soon.

Moreover, I also wanted to know if scalar values of **‘GroupIDsarray’, ‘TortuosityArray’ , ‘BlankingArray’** etc. can be incorporated in the centerline model. I generated the following plots by using the conventional vmtkscripts, but it does not always work correctly.

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/8/8/882e1766835d9ccda430877ca40183bae29a5bd9.png)  
 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/2/b/2bd69ea780065406186d299c62ef460f2e619001.jpeg)

Currently there are only 3 ways to postprocess the centerline data as shown here.

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/e/f/efab10cb0050f2cad8831bc139d9d91baabdd09b.png)

- However, can we make any changes here in the base code to store values of other arrays as well?

- Also would it be possible to run this function independently in a separate python code given the vtkpolymesh surface data and the endpoint co ordinates?

> <https://github.com/vmtk/SlicerExtension-VMTK/blob/995027f17f195d802134f799aab095ea31f3e6c5/ExtractCenterline/ExtractCenterline.py#L635-L723>

This has been done for the network curves but the centerline curves hold the real information.

> <https://github.com/vmtk/SlicerExtension-VMTK/blob/995027f17f195d802134f799aab095ea31f3e6c5/ExtractCenterline/ExtractCenterline.py#L616-L633>

---

<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:** [July 17, 2021, 5:54am UTC](https://discourse.slicer.org/t/slicer-convert-centerlines-to-a-graph-v-e/18604/4 "2021-07-17T05:54:40Z")

</div>

> [@som1197](#):
>
> Moreover, I also wanted to know if scalar values of **‘GroupIDsarray’, ‘TortuosityArray’ , ‘BlankingArray’** etc. can be incorporated in the centerline model.

Yes, sure. These are cell data (one value for each branch) and the values are saved in the table where you can easily look them up (by cell ID) but feel free to add it to the centerline tree model as well. Send a pull request with the proposed changes.

> [@som1197](#):
>
> However, can we make any changes here in the base code to store values of other arrays as well?

Yes, just send a pull request if you have any suggestion.

> [@som1197](#):
>
> Also would it be possible to run this function independently in a separate python code given the vtkpolymesh surface data and the endpoint co ordinates?

You can use the module from anywhere (from a Python script, without a GUI; from Jupyter notebooks, etc.). See complete example here:

> <https://github.com/vmtk/SlicerExtension-VMTK/blob/master/ExtractCenterline/ExtractCenterline.py#L1157-L1186>

---

<div class="post-metadata">

**Author:** ![RomanStriker](https://avatars.discourse-cdn.com/v4/letter/r/c37758/32.png) [@RomanStriker](https://discourse.slicer.org/u/RomanStriker)\
**Post date:** [November 8, 2022, 6:56pm UTC](https://discourse.slicer.org/t/slicer-convert-centerlines-to-a-graph-v-e/18604/5 "2022-11-08T18:56:27Z")

</div>

Hi, can you share your code for generating centerline curves colored by centerlineIDs. I am trying to separate the centerlines into different segments (branches).

---

<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:** [November 12, 2022, 3:41am UTC](https://discourse.slicer.org/t/slicer-convert-centerlines-to-a-graph-v-e/18604/6 "2022-11-12T03:41:24Z")

</div>

The code for splitting the tree to branches is here:

> <https://github.com/vmtk/SlicerExtension-VMTK/blob/437526ad4a097483d25b9591ab74548ab9ae0176/ExtractCenterline/ExtractCenterline.py#L832-L1013>

---

<div class="post-metadata">

**Author:** ![RomanStriker](https://avatars.discourse-cdn.com/v4/letter/r/c37758/32.png) [@RomanStriker](https://discourse.slicer.org/u/RomanStriker)\
**Post date:** [November 14, 2022, 12:35pm UTC](https://discourse.slicer.org/t/slicer-convert-centerlines-to-a-graph-v-e/18604/7 "2022-11-14T12:35:07Z")

</div>

Thanks. I used the function to get the curve tree but I see that it is not very consistent with the centerline model near bifurcations. I was hoping I could just classify nodes and edges in the centerline model directly.

 ![Screenshot from 2022-11-14 13-30-38](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/c/2/c2c96ed48b9d7752d61603e727e537c604504612.jpeg)
