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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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.

    """
    import pyclesperanto_prototype as cle

    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)

convert_float_to_int #

convert_float_to_int(img)

Convert an image from float to integer.

Parameters:

  • img #

    (ArrayLike) –

    Label image.

Returns:

  • ArrayLike

    Label image as integer.

Source code in src/napari_ndev/morphology.py
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
def convert_float_to_int(img: ArrayLike) -> ArrayLike:
    """
    Convert an image from float to integer.

    Parameters
    ----------
    img : ArrayLike
        Label image.

    Returns
    -------
    ArrayLike
        Label image as integer.

    """
    return img.astype(np.uint32)

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
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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.

    """
    import pyclesperanto_prototype as cle

    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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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.

    """
    import pyclesperanto_prototype as cle
    from skimage.morphology import skeletonize

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