A common use case for image processing in the biology context is counting blobs of high signal intensity surrounded by a low intensity background. Thresholding is the method of choice in this scenario. We demonstrate it with Otsu's thresholding method (Otsu et al., IEEE Transactions on Systems, Man, and Cybernetics, Vol. 9 (1), 1979)
import pyclesperanto_prototype as cle
from skimage.io import imread, imsave, imshow
import matplotlib
import numpy as np
# initialize GPU
cle.select_device("GTX")
c:\structure\code\pyclesperanto_prototype\pyclesperanto_prototype\_tier0\_device.py:77: UserWarning: No OpenCL device found with GTX in their name. Using gfx1035 instead. warnings.warn(f"No OpenCL device found with {name} in their name. Using {device.name} instead.")
<gfx1035 on Platform: AMD Accelerated Parallel Processing (2 refs)>
# load data
image = imread('https://samples.fiji.sc/blobs.png')
print("Image size: " + str(image.shape))
Image size: (254, 256)
After initializing GPU and pushing images there, workflows are constructed like this:
blurred = cle.gaussian_blur(image, sigma_x=1, sigma_y=1)
blurred
cle._ image
|
binary = cle.threshold_otsu(blurred)
binary
cle._ image
|
labeled = cle.connected_components_labeling_box(binary)
labeled
cle._ image
|
# The maximium intensity in a label image corresponds to the number of objects
num_labels = cle.maximum_of_all_pixels(labeled)
print("Number of objects in the image: " + str(num_labels))
Number of objects in the image: 62.0
In case the size of the objects is relevant, one should exclude the object which were not fully imaged and thus, touch the image border.
# Exclude Labels On Edges
labels_excl_edges = cle.exclude_labels_on_edges(labeled)
labels_excl_edges
cle._ image
|