Select labels using machine learning#

apoc allows selecting labeled objects according to properties such as size, shape and intensity in a corresponding image. In this example, we will select elongated objects from an instance segmentation of blobs.

import apoc

from import imread
import pyclesperanto_prototype as cle
import numpy as np

<NVIDIA GeForce RTX 3050 Ti Laptop GPU on Platform: NVIDIA CUDA (1 refs)>
image = imread('../../data/blobs.tif')
labels = cle.label(cle.threshold_otsu(image))
annotation = imread('../../data/label_annotation.tif')
cle.imshow(labels, labels=True)
cle.imshow(annotation, labels=True)
../_images/074888b68c318aac4eaadaf285f2b3805ee8f6a5d77939ccafc418406a2d6628.png ../_images/5ef923f8cd4e5b2b95049217b531aafef6a031da122db41be7fa6914462733b6.png ../_images/d62174c6ca905e29f8799378346f96368531856b5ba0d7af2c0fa12f88908d81.png


For training the classifier, you need to specify features. In the following we use mean and standard deviation intensity within the labeled objects and the object size and shape.

features = 'area,mean_max_distance_to_centroid_ratio,standard_deviation_intensity'

cl_filename = ""

# Create an object classifier
apoc.erase_classifier(cl_filename) # delete it if it was existing before
classifier = apoc.ObjectSelector(cl_filename, positive_class_identifier=1)

# train it
classifier.train(features, labels, annotation, image)


After the classifier was trained, we can use it to select objects.

result = classifier.predict(labels, image)


cle.imshow(result, labels=True)

One can also load the classifier from disc and apply it to another dataset. We demonstrate that by applying the classifier to a rotated version of the image and label image from above.

image1 = image.T
labels1 = cle.label(cle.threshold_otsu(image1))

classifier = apoc.ObjectSelector(cl_filename)

result = classifier.predict(labels1, image1)


cle.imshow(result, labels=True)

After training, we can ask the classifier how important features were while doing the prediction.

{'area': 0.29573084473661354,
 'mean_max_distance_to_centroid_ratio': 0.4264564597125618,
 'standard_deviation_intensity': 0.27781269555082466}


Use the code and example images above to train a classifier that selects all small objects in the label image.