Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skan returns pixel 0,0 under all conditions #108

Open
chrisdonlan opened this issue Apr 9, 2021 · 1 comment
Open

Skan returns pixel 0,0 under all conditions #108

chrisdonlan opened this issue Apr 9, 2021 · 1 comment

Comments

@chrisdonlan
Copy link

chrisdonlan commented Apr 9, 2021

Perhaps this is not a bug. However, it was a trip wire.

>>> foo = numpy.zeros((50,50))
>>> skeleton_to_csgraph(foo)
ValueError: ...
>>> foo[0,0] = 1
>>> skeleton_to_csgraph(foo)
ValueError: ...
>>> foo[-1,:] = 1
>>> adj, coords, degs = skeleton_to_csgraph(foo)
>>> coords
[[0. 0.], [0. 0.], ...

This issue summarizes as:

  • Two errors above are for all zeros, and an array with only isolated points.
  • The final call has two [ 0. 0. ] coords. At least 1 [0. 0.] is always present
  • The coords are all floating point and must be cast to integers for indexing

To fix it, I wrapped it like:

def get_cs_graph(skeleton: numpy.ndarray):
    """
    Get compressed sparse adjacency matrix of a skeleton
    :param skeleton: a skeletonized image
    :return: adj:csv_matrix, nonzero_pixel_coords: ndarray, degree_mask:ndarray
    """
    # Skan: https://jni.github.io/skan/getting_started.html

    # Skan, the source of 'skeleton_to_csgraph' always returns the origin in the pixel list, even if the
    # matrix is mostly zeros, so calculate this component with numpy
    coords = numpy.vstack(numpy.where(skeleton > 0)).T

    # Skan yields errors under some numerical conditions related to all zero arrays, and arrays with isolated pixels
    try:
        adj, _, degrees = skeleton_to_csgraph(skeleton)
    except ValueError as e:
        warnings.warn(str(e))
        adj = csr_matrix()
        degrees = numpy.zeros_like(skeleton)
    return adj, coords, degrees
@jni
Copy link
Owner

jni commented Apr 11, 2021

Thanks for raising @chrisdonlan! It's definitely a trip wire and you're not the first to raise it (see #107 and #93). I might be able to set some time aside next week to address this. I think the ideal fix is to add a layer of indirection between the pixel ids and the coordinates array.

Note that the float coordinates are by design, because some junction pixels get collapsed to their centroid. Perhaps a utility function that does tuple(np.round(coords).astype(int))) would be a useful addition also. It might even belong in scikit-image itself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants