Merging labels according to edge-to-edge-distances#

In this notebook we will merge labels in a label image according to their edge-to-edge distances to each other. Labels close-by will be merged.

See also

import pyclesperanto_prototype as cle
from skimage.io import imread
import numpy as np

For demonstration purposes, we use a modified version of the labels derived from the blobs example-image. We artificially introduce gaps between them.

image = imread("../../data/blobs.tif")
image[:, 80:150] = 0
image[80:130, 100:] = 0

image = cle.asarray(image)
labels = cle.voronoi_otsu_labeling(image, spot_sigma=4, outline_sigma=3)
labels
cle._ image
shape(254, 256)
dtypeuint32
size254.0 kB
min0.0
max47.0

First, we dilate the labels by half of the maximum distance the edges are allowed to have.

maximum_distance = 12

dilated_labels = cle.dilate_labels(labels, radius=maximum_distance/2)
dilated_labels
cle._ image
shape(254, 256)
dtypeuint32
size254.0 kB
min0.0
max47.0

We then merge the labels if the touch.

merged_dilated_labels = cle.merge_touching_labels(dilated_labels)
merged_dilated_labels
cle._ image
shape(254, 256)
dtypeuint32
size254.0 kB
min0.0
max5.0

Afterwards, we mask the merged labels with the original label’s shape. We also convert the result of this operation to 32-bit integer, so that the visualization as label-image works.

merged_labels = (merged_dilated_labels * (labels > 0)).astype(np.uint32)
merged_labels
cle._ image
shape(254, 256)
dtypeuint32
size254.0 kB
min0.0
max5.0