# Centerline extraction

**URL:** https://discourse.itk.org/t/centerline-extraction/4885
**Category:** Beginner Questions
**Tags:** python
**Created:** [March 24, 2022, 4:36pm UTC](https://discourse.itk.org/t/centerline-extraction/4885 "2022-03-24T16:36:38Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![ekmungi](https://discourse.itk.org/user_avatar/discourse.itk.org/ekmungi/32/2626_2.png) [@ekmungi](https://discourse.itk.org/u/ekmungi)
#### Post date: [March 24, 2022, 4:36pm UTC](https://discourse.itk.org/t/centerline-extraction/4885/1 "2022-03-24T16:36:38Z")

</div>

I am new to ITK. I have a 3D model of a tubular structure and I am looking for a way to extract the centerline for it. Could someone help?

So far, I have tried using VMTK but it requires a closed model (end points are closed), but in my case I have a model with open ends.

 ![model](https://discourse.itk.org/uploads/default/original/2X/8/88db00252eebd0e683a939f35e5ca938e7c55827.png)

I am looking for suggestions on how I can extract the centerline for this model. Thank you.

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [March 24, 2022, 4:39pm UTC](https://discourse.itk.org/t/centerline-extraction/4885/2 "2022-03-24T16:39:27Z")

</div>

@Stephen_Aylward might have some suggestions.

---

<div class="post-metadata">

### Author: ![Stephen\_Aylward](https://discourse.itk.org/user_avatar/discourse.itk.org/stephen_aylward/32/654_2.png) [@Stephen\_Aylward](https://discourse.itk.org/u/Stephen_Aylward)
#### Post date: [March 24, 2022, 5:02pm UTC](https://discourse.itk.org/t/centerline-extraction/4885/3 "2022-03-24T17:02:23Z")

</div>

Hi,

There are a few options.

1. Render the object to a binary image and then extract the centerline that way. The downside is the lack of precision. An example of this is in the jupyter notebook: [ITKTubeTK/Demo-SegmentVesselsFromBinaryImage.ipynb at master · InsightSoftwareConsortium/ITKTubeTK · GitHub](https://github.com/InsightSoftwareConsortium/ITKTubeTK/blob/master/examples/BinaryImage/Demo-SegmentVesselsFromBinaryImage.ipynb)

2. See if the topology toolkit has a solution: [https://topology-tool-kit.github.io/](https://topology-tool-kit.github.io/)

3. Check with the VTK forum, to see if there is a polydata-based solution.

---

<div class="post-metadata">

### Author: ![ekmungi](https://discourse.itk.org/user_avatar/discourse.itk.org/ekmungi/32/2626_2.png) [@ekmungi](https://discourse.itk.org/u/ekmungi)
#### Post date: [March 24, 2022, 5:34pm UTC](https://discourse.itk.org/t/centerline-extraction/4885/4 "2022-03-24T17:34:29Z")

</div>

Thanks for the suggestions, @Stephen_Aylward . For your first option,

> [@Stephen\_Aylward](#):
>
> Render the object to a binary image and then extract the centerline that way.

I get the following error:

```auto
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
Input In [4], in <module>
----> 1 resample = ttk.ResampleImage.New(Input=im1, MakeIsotropic=True)
      2 resample.Update()
      3 im1Iso = resample.GetOutput()

File ~/anaconda3/envs/PT2/lib/python3.9/site-packages/itk/support/lazy.py:137, in LazyITKModule. __getattribute__ (self, attr)
    135 module = self.__belong_lazy_attributes[attr]
    136 namespace = {}
--> 137 base.itk_load_swig_module(module, namespace)
    138 self.loaded_lazy_modules.add(module)
    139 for k, v in namespace.items():

File ~/anaconda3/envs/PT2/lib/python3.9/site-packages/itk/support/base.py:102, in itk_load_swig_module(name, namespace)
    100 deps = l_data.get_module_dependencies()
    101 for dep in deps:
--> 102 itk_load_swig_module(dep, namespace)
    104 if itkConfig.ImportCallback:
    105 itkConfig.ImportCallback(name, 0)

File ~/anaconda3/envs/PT2/lib/python3.9/site-packages/itk/support/base.py:102, in itk_load_swig_module(name, namespace)
    100 deps = l_data.get_module_dependencies()
    101 for dep in deps:
--> 102 itk_load_swig_module(dep, namespace)
    104 if itkConfig.ImportCallback:
    105 itkConfig.ImportCallback(name, 0)

    [... skipping similar frames: itk_load_swig_module at line 102 (3 times)]

File ~/anaconda3/envs/PT2/lib/python3.9/site-packages/itk/support/base.py:102, in itk_load_swig_module(name, namespace)
    100 deps = l_data.get_module_dependencies()
    101 for dep in deps:
--> 102 itk_load_swig_module(dep, namespace)
    104 if itkConfig.ImportCallback:
    105 itkConfig.ImportCallback(name, 0)

File ~/anaconda3/envs/PT2/lib/python3.9/site-packages/itk/support/base.py:98, in itk_load_swig_module(name, namespace)
     86 this_module.__templates_loaded = True
     88 # Now, we definitely need to load the template_feature instantiations from the
     89 # named module, and possibly also load the underlying SWIG module. Before
     90 # we can load the template_feature instantiations of this module, we need to load
   (...)
     96 # via the itk_base_global_module_data instance defined at the bottom of this file, which
     97 # knows how to find those configuration files.
---> 98 l_data = itk_base_global_module_data[name]
     99 if l_data:
    100 deps = l_data.get_module_dependencies()

KeyError: 'ITKImageGrid'

```

Is this because I am using

```auto
In [5]: itk. __version__
Out[5]: '5.3.0'

```

---

<div class="post-metadata">

### Author: ![Stephen\_Aylward](https://discourse.itk.org/user_avatar/discourse.itk.org/stephen_aylward/32/654_2.png) [@Stephen\_Aylward](https://discourse.itk.org/u/Stephen_Aylward)
#### Post date: [March 24, 2022, 6:28pm UTC](https://discourse.itk.org/t/centerline-extraction/4885/5 "2022-03-24T18:28:10Z")

</div>

Did you install ITK via pip?

If so, could you please show me the output of pip list? It seems like there might be a version mis-match of itk and tubetk wheels.

Ideally the follow should work:  
pip install --pre itk (to get itk v5.3rc03)  
pip install itk-tubetk (to get itk-tubetk 1.1)

I suspect you’re using the (should be correct but might not be working) following  
pip install itk  
pip install itk-tubetk

The current version of tubetk is meant to work with the soon to be released v5.3…but I see that it still hasn’t been released on pypi yet, so that might be the source of your troubles. It is weird, however, that your itk. **version** \_ reports 5.3.0, so I really need to learn more about your setup and then check with ITK gurus.

If you are compiling, then please let me know what OS, compiler, and ITK source code git tag you’re using.

Sorry for the troubles.

---

<div class="post-metadata">

### Author: ![ekmungi](https://discourse.itk.org/user_avatar/discourse.itk.org/ekmungi/32/2626_2.png) [@ekmungi](https://discourse.itk.org/u/ekmungi)
#### Post date: [March 25, 2022, 9:52am UTC](https://discourse.itk.org/t/centerline-extraction/4885/6 "2022-03-25T09:52:36Z")

</div>

You are correct. With the first command it installed ITK 5.2.1, but when I use the second command, it uninstalled the previous version and installed 5.3rc3.

> [@Stephen\_Aylward](#):
>
> pip install itk  
> pip install itk-tubetk

Based on your explanation this should work.

I tried a clean install in a different python environment and still the same result.

> [@Stephen\_Aylward](#):
>
> pip install --pre itk (to get itk v5.3rc03)  
> pip install itk-tubetk (to get itk-tubetk 1.1)

---

<div class="post-metadata">

### Author: ![ekmungi](https://discourse.itk.org/user_avatar/discourse.itk.org/ekmungi/32/2626_2.png) [@ekmungi](https://discourse.itk.org/u/ekmungi)
#### Post date: [March 25, 2022, 5:12pm UTC](https://discourse.itk.org/t/centerline-extraction/4885/7 "2022-03-25T17:12:50Z")

</div>

@Stephen_Aylward, just to update; I managed to convert my model to a volume and then use VMTK.

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.itk.org/user_avatar/discourse.itk.org/lassoan/32/27_2.png) [@lassoan](https://discourse.itk.org/u/lassoan)
#### Post date: [March 25, 2022, 5:46pm UTC](https://discourse.itk.org/t/centerline-extraction/4885/8 "2022-03-25T17:46:42Z")

</div>

> [@ekmungi](#):
>
> just to update; I managed to convert my model to a volume and then use VMTK

Great! Just in case someone else wants to replicate this, here is a video that shows a workflow that automatically closes open ends of the vessel tree using shrink-wrapping:

[![](https://discourse.itk.org/uploads/default/original/2X/a/a817e8601a0ca6d9d511cb66fb2c9f495bf5c945.jpeg "Get vessel centerline from vessel wall model") ](https://www.youtube.com/watch?v=kXtpuv7NO-g)
