elf.skeleton.thinning

 1from typing import Tuple
 2
 3import numpy as np
 4
 5# The skeletonize_3d function was removed in recent versions of scikit-image in favor
 6# of skeletonize, which now handles both 2d and 3d inputs. To keep compatability with older
 7# versions we do this try except here.
 8try:
 9    from skimage.morphology import skeletonize_3d
10except ImportError:
11    from skimage.morphology import skeletonize as skeletonize_3d
12
13from skan import csr
14
15
16def thinning(
17    obj: np.array,
18    resolution: Tuple[float, ...],
19    *args,
20    **kwargs,
21) -> Tuple[np.ndarray, np.ndarray]:
22    """Skeletonize object with thinning based method.
23
24    Wrapper around implementation from
25    https://scikit-image.org/docs/dev/api/skimage.morphology.html#skimage.morphology.skeletonize
26
27    Args:
28        obj: Binary object mask.
29        resolution: Size of the voxels in physical unit.
30        args: Additional positional arguments. For signature compatability, will be ignored.
31        kwargs: Additional keyword arguments. For signature compatability, will be ignored.
32
33    Returns:
34        The nodes of the skeleton.
35        The edges between skeleton nodes.
36    """
37
38    # Skeletonize with thinning.
39    vol = skeletonize_3d(obj)
40
41    # Use skan to extact skeleon node coordinates and edges.
42    adj_mat, nodes = csr.skeleton_to_csgraph(vol, spacing=resolution)
43
44    # Convert nodes from tuple to numpy array.
45    nodes = np.concatenate([n[:, None] for n in nodes], axis=1).astype("uint64")
46
47    # Convert graph to uv-list representation.
48    n_nodes = len(nodes)
49    graph = csr.csr_to_nbgraph(adj_mat)
50    edges = np.array(
51        [[u, v] for u in range(n_nodes) for v in graph.neighbors(u) if u < v], dtype="uint64"
52    )
53
54    # Return node coordinate list and edges.
55    return nodes, edges
def thinning( obj: <built-in function array>, resolution: Tuple[float, ...], *args, **kwargs) -> Tuple[numpy.ndarray, numpy.ndarray]:
17def thinning(
18    obj: np.array,
19    resolution: Tuple[float, ...],
20    *args,
21    **kwargs,
22) -> Tuple[np.ndarray, np.ndarray]:
23    """Skeletonize object with thinning based method.
24
25    Wrapper around implementation from
26    https://scikit-image.org/docs/dev/api/skimage.morphology.html#skimage.morphology.skeletonize
27
28    Args:
29        obj: Binary object mask.
30        resolution: Size of the voxels in physical unit.
31        args: Additional positional arguments. For signature compatability, will be ignored.
32        kwargs: Additional keyword arguments. For signature compatability, will be ignored.
33
34    Returns:
35        The nodes of the skeleton.
36        The edges between skeleton nodes.
37    """
38
39    # Skeletonize with thinning.
40    vol = skeletonize_3d(obj)
41
42    # Use skan to extact skeleon node coordinates and edges.
43    adj_mat, nodes = csr.skeleton_to_csgraph(vol, spacing=resolution)
44
45    # Convert nodes from tuple to numpy array.
46    nodes = np.concatenate([n[:, None] for n in nodes], axis=1).astype("uint64")
47
48    # Convert graph to uv-list representation.
49    n_nodes = len(nodes)
50    graph = csr.csr_to_nbgraph(adj_mat)
51    edges = np.array(
52        [[u, v] for u in range(n_nodes) for v in graph.neighbors(u) if u < v], dtype="uint64"
53    )
54
55    # Return node coordinate list and edges.
56    return nodes, edges

Skeletonize object with thinning based method.

Wrapper around implementation from https://scikit-image.org/docs/dev/api/skimage.morphology.html#skimage.morphology.skeletonize

Arguments:
  • obj: Binary object mask.
  • resolution: Size of the voxels in physical unit.
  • args: Additional positional arguments. For signature compatability, will be ignored.
  • kwargs: Additional keyword arguments. For signature compatability, will be ignored.
Returns:

The nodes of the skeleton. The edges between skeleton nodes.