Label image refinement#

Similar to morphological operations on binary imagges, it is also possible to refine label images. This notebook shows how to do this.

See also

import pyclesperanto_prototype as cle
import numpy as np
from skimage.io import imread
label_image = cle.gauss_otsu_labeling(imread("../../data/mitosis_mod.tif"), outline_sigma=0)
label_image
cle._ image
shape(70, 70)
dtypeuint32
size19.1 kB
min0.0
max13.0

Eroding labels#

When eroding labels, we need to be careful that objects might split into two. This could be intentional, e.g. to differentiate touching nuclei like in the example above.

eroded_label_image = cle.erode_labels(label_image,
                                      radius=2,
                                      relabel_islands=False)
eroded_label_image
cle._ image
shape(70, 70)
dtypeuint32
size19.1 kB
min0.0
max9.0
eroded_label_image2 = cle.erode_labels(label_image,
                                      radius=2,
                                      relabel_islands=True)
eroded_label_image2
cle._ image
shape(70, 70)
dtypeuint32
size19.1 kB
min0.0
max10.0

Dilating labels#

We can then dilate the labels again to come back to their original size approximately. This might also be useful in case segmented objects are too small in general.

dilated_label_image = cle.dilate_labels(eroded_label_image2, 
                                        radius=2)
dilated_label_image
cle._ image
shape(70, 70)
dtypeuint32
size19.1 kB
min0.0
max10.0

Opening and closing labels#

Opening and closing for label images is similar like for binary images. The only difference is that when labels touch, they cannot expand anymore.

Note that opening labels may make small labels disappear.

opened_label_image = cle.opening_labels(label_image,
                                        radius=2)
opened_label_image
cle._ image
shape(70, 70)
dtypeuint32
size19.1 kB
min0.0
max9.0
closed_label_image = cle.closing_labels(label_image,
                                        radius=2)
closed_label_image
cle._ image
shape(70, 70)
dtypeuint32
size19.1 kB
min0.0
max13.0

Exercise#

Use the operations introduced above to make small objects disappear in this label image.

label_blobs = cle.asarray(imread("../../data/blobs_labeled.tif")).astype(np.uint32)
label_blobs
cle._ image
shape(254, 256)
dtypeuint32
size254.0 kB
min0.0
max63.0