#!/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[2]: 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 starting_point # ## Local maxima detection # In[3]: maxima = cle.detect_maxima_box(starting_point) maxima # ## Local threshold determination # In[4]: # 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) print(thresholds) # ## Make a local threshold image # In[5]: # Extend labeled maxima until they touch voronoi_label_image = cle.extend_labeling_via_voronoi(labeled_maxima) voronoi_label_image # In[6]: # Replace labels with thresholds threshold_image = cle.replace_intensities(voronoi_label_image, thresholds) threshold_image # In[7]: # Apply threshold binary_segmented = cle.greater(starting_point, threshold_image) binary_segmented # ## Measure bounding boxes # In[8]: # Label objects labels = cle.connected_components_labeling_box(binary_segmented) labels # In[9]: # 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[10]: print("Sigma", sigma) # In[ ]: