I need a python code to get center line of segmentations

i wanna write a code to get center of seg and dont know how do it. please help

You can use Extract Centerline module logic. See the source code and how it is used from the widget class here.

thank you lassoan. i have a problem to install slicer on vs code and test my code and use the widgets class.

Do you have any solution to help me?

As a first step, we suggest you follow the Tutorials for software developers available on tutorial pages referenced at https://slicer.readthedocs.io/en/latest/user_guide/getting_started.html#tutorials.

These should help you understand how to create a new module and edit its code.

thank you jean.

But it is not my problem. I could not run my code in vscode. slicer not recognized true and their mudules not loaded in vscode.

Considering Slicer modules written in python are expected to be discovered and loaded by the Slicer application, you can not “run” the corresponding python code in an editor like vscode.

Such code editor is helpful to write the code by leveraging syntax coloring, linting, …

The execution of the corresponding code happen in Slicer itself.

To further understand these concept, I still recommend you review the tutorials I referenced above. More may start by reviewing this tutorial.


i think that i could not specify my problem correctly.

as an example when i import slicer there is not any module in it. there are nothings to use as modules.

Assuming you are running the code in the python interpreter available in the application, modules are available in silcer.modules.

For example, see Script repository — 3D Slicer documentation

To be able to help you more effectively, consider including screenshot to illustrate your problem.

Exactly my problem is here. silcer.modules not working.

Since PythonSlicer is a launcher starting regular python interpreter, the Slicer modules are not available. These are loaded by the Slicer application.

Instead, you should consider:

  • copy/past the code in the Slicer python console
  • writing Slicer module (see tutorial above) and then click on a Reload & Test
1 Like

You can also attach Visual Studio code to the Slicer application once the application is running and set up its Python environments. See instructions here. You can then see all modules, have auto-complete, method documentation, etc.

You can also use Jupyter notebooks, using Slicer as the kernel (using SlicerJupyter extension).

1 Like

thank you Andras

it is very useful for me.