Error with coordinates of skeleton_to_csgraph (python)

543 Views Asked by At

I am using skeleton-to-csgraph() to analyse the skeleton I have obtained and this function should send me back some data and among them, all the coordinates of all the points of the skeleton in a matrix [(N+1) x 2] with N the number of points. However there is one situation where this function gives me a coordinates matrix with some error for no reasons. Does someone knows from where the error could come from ?

More information about the function and the package here: https://jni.github.io/skan/

Lines of the coordinates matrix given by skeleton-to-csgraph function. At the line 326 an unexplained error, it should be an int and not a float value because it is a pixel index

with grid, a ndarray of int32 [200,100] with value of 1 or 0. File in this link: https://drive.google.com/drive/folders/12xwZBv3RKQ4Q802jXPAhtYZXHrsMytMl?usp=sharing

skeleton, distance = medial_axis(grid, return_distance=True)
graph, coordinates, degrees = skeleton_to_csgraph(skeleton)

Like on the picture, I obtained none integer value for one coordinate

3

There are 3 best solutions below

6
Juan On BEST ANSWER

The issue you're encountering is because skan needs to collapse junction pixels into their centroid in order to produce a coherent graph. See https://doi.org/10.7717/peerj.4312/supp-2 for details. Apologies that this is not better documented. I've made an issue to improve this in the future:

https://github.com/jni/skan/issues/121

This issue is also related: the coordinates array contains all node coordinates but also some junk coordinates:

https://github.com/jni/skan/issues/108

Feel free to continue the discussion on github!

5
simpleApp On

The following steps followed: Note: I ran this on jupyter notebook, so you will see! at start of installation

!pip install git+https://github.com/jni/skan # install latest version github

load the data, and process it.

import numpy as np
from skimage.morphology import medial_axis
from skan import skeleton_to_csgraph
from skan import _testdata
from skan import draw
from matplotlib import pyplot as plt
grid = np.load('grid.npy') # this is the file you shared through google drive

skeleton, distance = medial_axis(grid, return_distance=True)
graph, coordinates, degrees = skeleton_to_csgraph(skeleton)
fig, ax = plt.subplots()
draw.overlay_skeleton_networkx(graph,coordinates, image=skeleton,axis=ax)

fig, ax = plt.subplots()
draw.overlay_skeleton_2d(grid, skeleton, dilate=1, axes=ax);

enter image description here

Complete code screen snap

0
Eloi Schlegel On

It seams there is a problem with the function skeleton-to-csgraph() in some specific cases. But to overcome this error, the following code provides the result you should have:

a = np.where(skeleton == 1 )
b = np.array([a[0], a[1]]).transpose()
coordinates = np.vstack(([0,0],b))

with skeleton, the numpy array with the image of your skeleton and coordinates, the result you should obtain with skeleton-to-csgraph() which contains all the coordinates of the skeleton points.