Merging annotated labels#

In this notebook we demonstrate how a label-image can be post-processed by annotating labels that should be merged.

import apoc
from skimage.io import imread, imshow, imsave
import pyclesperanto_prototype as cle
import numpy as np

Our starting point is an oversegmented (synthetic) label image.

oversegmented = cle.asarray(imread('../../data/syntetic_cells.tif')).astype(np.uint32)
oversegmented
cle._ image
shape(512, 512)
dtypeuint32
size1024.0 kB
min0.0
max49.0

Furthermore, we need an annotation where pixel-intensity = 1 implies that labels should be merged.

annotation = cle.asarray(imread('../../data/syntetic_cells_merge_annotation.tif')).astype(np.uint32)

# binarize the image
annotation = annotation == 1

annotation
cle._ image
shape(512, 512)
dtypeuint8
size256.0 kB
min0.0
max1.0

For visualization purposes, we overlay both.

cle.imshow(oversegmented, labels=True, continue_drawing=True)
cle.imshow(annotation, alpha=0.5)
../_images/2f1880ffefc1b3184e3c73ac9a2031afdeb0d774ebde96a8cd77f3e78e8c3b1d.png

We can now merge all cells whose borders are annotated.

result = cle.merge_annotated_touching_labels(oversegmented, annotation)
result
cle._ image
shape(512, 512)
dtypeuint32
size1024.0 kB
min0.0
max18.0

How does it work?#

Under the hood, there is a function for generating a touch-matrix from the label image and the annotation and a function for merging labels according to a touch-matrix.

should_touch_matrix = cle.generate_should_touch_matrix(oversegmented, annotation)
should_touch_matrix
cle._ image
shape(50, 50)
dtypefloat32
size9.8 kB
min0.0
max1.0
result = cle.merge_labels_according_to_touch_matrix(oversegmented, should_touch_matrix)
result
cle._ image
shape(512, 512)
dtypeuint32
size1024.0 kB
min0.0
max18.0