navis-org / skeletor

Extraction of 3D skeletons from meshes.
https://navis-org.github.io/skeletor/
GNU General Public License v3.0
209 stars 26 forks source link

How to get simpler skeleton on the skeletonize result? #38

Open xiruiYan opened 1 year ago

xiruiYan commented 1 year ago

Hi, thanks for your work! I am a new at this , and I am trying to get the skeleton for animation from a mesh. I've run this code and get result like the left one in this picture, which have much more joints than I need. How can I get the right result in this picture?

image

Can you give some advice on this? thank you very much!

schlegelp commented 1 year ago

Simplifying or downsampling the skeleton is not terribly difficult but there is currently no convenience method built directly into skeletor. I will have a think about whether to add one.

For now, you could use navis:

pip3 install navis

Then:

# Run the example skeletonization

>>> import skeletor as sk
>>> mesh = sk.example_mesh()
>>> fixed = sk.pre.fix_mesh(mesh, remove_disconnected=5, inplace=False)
>>> skel = sk.skeletonize.by_wavefront(fixed, waves=1, step_size=1)

# Turn the skeletor object into navis 

>>> import navis 
>>> n = navis.TreeNeuron(skel, soma=None)
>>> n.n_nodes  # Number of nodes/vertices
1103

# Option 1: downsample by given factor
# Note that his will never remove branch or end points which means 
# there is a limit to how much you can downsample

>>> ds = navis.downsample_neuron(n, downsampling_factor=10)
>>> ds.n_nodes  # Check that we now have fewer nodes/vertices
500

# Option 2: resample to given segment length
# This also works to "upsample" the skeleton

>>> res = navis.resample_skeleton(n, resample_to=1000)
>>> res.n_nodes  # Check that we now have fewer nodes/vertices
517

# Turn navis object to skeletor Skeleton

>> skel2 = sk.Skeleton(ds.nodes[['node_id', 'parent_id', 'x', 'y', 'z', 'radius']].copy())

A potential catch here is that by downsampling you break the correspondence between the vertices in your original mesh and the nodes in the skeleton.