Merging labels#

In principle, all segmentation algorithms are limited. In case results are sub-optimal and no better segmentation algorithm is available, post-processing labels may be an option. There are some functions available for merging labels according to their properties such as intensity along the edge where labels touch the pixel count of pair-wise combined labels.

import pyclesperanto_prototype as cle
from napari_segment_blobs_and_things_with_membranes import local_minima_seeded_watershed
import numpy as np
cle.select_device("TX")
<NVIDIA GeForce RTX 3050 Ti Laptop GPU on Platform: NVIDIA CUDA (1 refs)>

Merging touching labels#

The most trivial use-case might be merging labeled objects that touch.

blobs = cle.imread("../../data/blobs.tif")
blobs_labels = cle.voronoi_otsu_labeling(blobs, spot_sigma=3)
blobs_labels
cle._ image
shape(254, 256)
dtypeuint32
size254.0 kB
min0.0
max72.0
cle.merge_touching_labels(blobs_labels)
cle._ image
shape(254, 256)
dtypeuint32
size254.0 kB
min0.0
max61.0

Merging labels according to border intensity#

As an example we use a cropped slice of the cells3d example dataset in scikit-image.

image = cle.imread("../../data/membranes_2d.tif")[30:130, 0:100]
image
cle._ image
shape(100, 100)
dtypefloat32
size39.1 kB
min1062.0
max20614.0

In the following example, our cell in the center of the image was wrongly segmented as two cells:

labels = local_minima_seeded_watershed(image, spot_sigma=5, outline_sigma=0)
labels
nsbatwm made image
shape(100, 100)
dtypeint32
size39.1 kB
min1
max12

This can be corrected by merging cells with border intensity below a given threshold.

merged_labels = cle.merge_labels_with_border_intensity_within_range(image, labels, maximum_intensity=5000)
merged_labels
c:\structure\code\pyclesperanto_prototype\pyclesperanto_prototype\_tier3\_generate_touch_mean_intensity_matrix.py:30: UserWarning: generate_touch_mean_intensity_matrix is supposed to work with images of integer type only.
Loss of information is possible when passing non-integer images.
  warnings.warn("generate_touch_mean_intensity_matrix is supposed to work with images of integer type only.\n" +
cle._ image
shape(100, 100)
dtypeuint32
size39.1 kB
min1.0
max8.0