#!/usr/bin/env python # coding: utf-8 # # Bead segmentation and measurements # This notebook shows how to detect hot spots / local maxima / beads in an image and how to measure their FWHM. To make the algorithm work, the beads should be sufficiently sparse. # In[1]: import pyclesperanto_prototype as cle cle.get_device() # Let's start by making an image showing local maxima. # In[10]: import numpy as np from skimage.io import imshow random_image = np.random.random([512,512]) binary_image = random_image > 0.9995 # push to GPU input_image = cle.push(binary_image * random_image) # blur the image sigma = 3 starting_point = cle.gaussian_blur(input_image, sigma_x=sigma, sigma_y=sigma) # show input image cle.imshow(starting_point) # ## Local maxima detection # In[11]: maxima = cle.detect_maxima_box(starting_point) cle.imshow(maxima) # ## Local threshold determination # In[12]: # Label maxima labeled_maxima = cle.label_spots(maxima) # read out intensities at the maxima max_intensities = cle.read_intensities_from_map(labeled_maxima, starting_point) print(max_intensities) # calculate thresholds thresholds = cle.multiply_image_and_scalar(max_intensities, scalar=0.5) # ## Make a local threshold image # In[13]: # Extend labeled maxima until they touch voronoi_label_image = cle.extend_labeling_via_voronoi(labeled_maxima) cle.imshow(voronoi_label_image, labels=True) # In[14]: # Replace labels with thresholds threshold_image = cle.replace_intensities(voronoi_label_image, thresholds) cle.imshow(threshold_image) # In[15]: # Apply threshold binary_segmented = cle.greater(starting_point, threshold_image) cle.imshow(binary_segmented) # ## Measure bounding boxes # In[16]: # Label objects labels = cle.connected_components_labeling_box(binary_segmented) cle.imshow(labels, labels=True) # In[17]: # Derive statistics stats = cle.statistics_of_labelled_pixels(label_image=labels) print('Bounding box widths', stats['bbox_width']) print('Bounding box heights', stats['bbox_height']) # The bounding box width and height should be approximately twice as large as the sigma used for bluring the image at the very beginning. # In[18]: print("Sigma", sigma) # In[ ]: