Skip to content

napari_ndev.morphology #

Functions for processing label morphology.

Process labels with various functions using pyclesperanto and scikit-image. Intended to be compatible with workflow.yaml files, and will be incorporated into napari-workflows and napari-assistant in the future. Should accept both OCLArray and other ArrayLike types.

Functions:

  • skeletonize_labels : Create skeletons with label identities from a label image.

connect_breaks_between_labels #

connect_breaks_between_labels(label, connect_distance)

Connect breaks between labels in a label image.

Return the input label image with new label identities connecting breaks between the original labels. The new labels have the original label dimensions, so this is intended to keep the overall morphology the same, just with new labels connecting the original labels if under the specified distance.

Parameters:

  • label (ArrayLike) –

    Label image.

  • connect_distance (float) –

    Maximum distance to connect labels, in pixels.

Returns:

  • ArrayLike

    Label image with new labels connecting breaks between original labels.

Source code in src/napari_ndev/morphology.py
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
def connect_breaks_between_labels(label: ArrayLike, connect_distance: float) -> ArrayLike: # pragma: no cover
    """
    Connect breaks between labels in a label image.

    Return the input label image with new label identities connecting breaks
    between the original labels. The new labels have the original label
    dimensions, so this is intended to keep the overall morphology the same,
    just with new labels connecting the original labels if under the specified
    distance.

    Parameters
    ----------
    label : ArrayLike
        Label image.
    connect_distance : float
        Maximum distance to connect labels, in pixels.

    Returns
    -------
    ArrayLike
        Label image with new labels connecting breaks between original labels.

    """
    label_dilated = cle.dilate_labels(label, radius=connect_distance/2)
    label_merged = cle.merge_touching_labels(label_dilated)
    # relabel original labels based on the merged labels
    return (label_merged * (label > 0)).astype(np.uint16)

label_voronoi_based_on_intensity #

label_voronoi_based_on_intensity(label, intensity_image)

Create a voronoi label masks of labels based on an intensity image.

Return a label image with Voronoi regions based on the intensity image. The intensity image should be the same shape as the label image, and the labels will be assigned to the Voronoi regions based on the intensity values.

Parameters:

  • label (ArrayLike) –

    Label image.

  • intensity_image (ArrayLike) –

    Intensity image.

Returns:

  • ArrayLike

    Label image with Voronoi regions based on the intensity image.

Source code in src/napari_ndev/morphology.py
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
def label_voronoi_based_on_intensity(label: ArrayLike, intensity_image: ArrayLike) -> ArrayLike: # pragma: no cover
    """
    Create a voronoi label masks of labels based on an intensity image.

    Return a label image with Voronoi regions based on the intensity image.
    The intensity image should be the same shape as the label image, and the
    labels will be assigned to the Voronoi regions based on the intensity
    values.

    Parameters
    ----------
    label : ArrayLike
        Label image.
    intensity_image : ArrayLike
        Intensity image.

    Returns
    -------
    ArrayLike
        Label image with Voronoi regions based on the intensity image.

    """
    label_binary = cle.greater_constant(label, constant=0) # binarize
    intensity_blur = cle.gaussian_blur(intensity_image, sigma_x=1, sigma_y=1)
    intensity_peaks = cle.detect_maxima_box(intensity_blur, radius_x=0, radius_y=0)
    select_peaks_on_binary = cle.binary_and(intensity_peaks, label_binary)
    return cle.masked_voronoi_labeling(select_peaks_on_binary, label_binary)

skeletonize_labels #

skeletonize_labels(label)

Create skeletons and maintains label identities from a label image.

Parameters:

  • label (ArrayLike) –

    Label image.

Returns:

  • ndarray

    Skeletonized label image.

Source code in src/napari_ndev/morphology.py
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
def skeletonize_labels(label: ArrayLike) -> np.ndarray:
    """
    Create skeletons and maintains label identities from a label image.

    Parameters
    ----------
    label : ArrayLike
        Label image.

    Returns
    -------
    np.ndarray
        Skeletonized label image.

    """
    from skimage.morphology import skeletonize

    skeleton = skeletonize(cle.pull(label))
    return (label * skeleton).astype(np.uint16)