Shape descriptors based on neighborhood graphs#

This notebook demonstrates how to determine shape descriptors of cells in case they cannot be segmented exactly but their centers can be detected.

from skimage.measure import regionprops
import numpy as np
import pyclesperanto_prototype as cle

cle.get_device()
<gfx90c on Platform: AMD Accelerated Parallel Processing (2 refs)>

We generate a label image of cells with given sizes in x and y and a size ratio of 1:1.5.

Assume this is the result of some cell segmentation algorithm.

cell_size_x = 10
cell_size_y = 15

# generate and show tissue
tissue_labels = cle.artificial_tissue_2d(width=100, height=100, delta_x=cell_size_x, delta_y=cell_size_y, random_sigma_x=1, random_sigma_y=1)
cle.imshow(tissue_labels, labels=True)
../_images/1b87b429e0fae45eab599a4b9b5b0849be14cc2aa11a9ea3a7ce7aeca407e218.png

Classical shape descriptors: minor and major axis#

We can measure the minor and major axis of those cells using scikit-image

label_image = cle.pull_zyx(tissue_labels).astype(int)

stats = regionprops(label_image)

avg_minor_axis_length = np.mean([s.minor_axis_length for s in stats])
print("Average minor axis length", avg_minor_axis_length)

avg_major_axis_length = np.mean([s.major_axis_length for s in stats])
print("Average major axis length", avg_major_axis_length)
Average minor axis length 11.094738115993875
Average major axis length 16.966309163176998

We now generate an image where the cells might be hard to segment from, e.g. a membrane image.

cell_borders = cle.detect_label_edges(label_image)

convolved_membranes = cle.gaussian_blur(cell_borders, sigma_x=1, sigma_y=2)

noise_level = 3
noise = (np.random.random(convolved_membranes.shape) - 0.5) * cle.mean_of_all_pixels(convolved_membranes) * noise_level
noise = noise.astype(np.float32)

artifical_membrane_image = (convolved_membranes + noise)

cle.imshow(artifical_membrane_image)
../_images/968d082c8563d0f712179c2225e601f4b4a53a385556e50b0bac4057e7ff0199.png

Shape descriptors based on neighbor meshes#

In some cases, we can’t segment the cells properly, we can just do spot detection and visulize the centers of the cells.

blurred = cle.gaussian_blur(artifical_membrane_image, sigma_x=2, sigma_y=2)

local_minima = cle.detect_minima_box(blurred)

spot_image = cle.label_spots(local_minima)

# we extend the spots a little bit for visualization purposes
spot_image = cle.maximum_sphere(spot_image, radius_x=1, radius_y=1)

cle.imshow(spot_image, labels=True)
../_images/84f2a6845b5946fc488675451f9cdf9cd05e7c4036486d5209e4e04ed0e65363.png

From such an image of labelled spots, we can make a voronoi diagram, were we can analyse wich cells (expanded spots) are close by each other.

The result is an approximation of cell segmentation.

voronoi_diagram_all = cle.extend_labeling_via_voronoi(spot_image)

# exclude labels on image edges
voronoi_diagram = cle.exclude_labels_on_edges(voronoi_diagram_all)

cle.imshow(voronoi_diagram, labels=True)
../_images/0648ee284ad03e428ca35046c3db4390af0a47fee5992755aa117ef0210fd59f.png

From such a pair of spots-image and voronoi diagram, we can dermine to matrices, a touch-matrix (also known as adjaceny-graph matrix) and a distance matrix.

touch_matrix = cle.generate_touch_matrix(voronoi_diagram)

# igonore touching the background
cle.set_column(touch_matrix, 0, 0)


centroids = cle.centroids_of_labels(voronoi_diagram)

distance_matrix = cle.generate_distance_matrix(centroids, centroids)


cle.imshow(touch_matrix)
cle.imshow(distance_matrix)
../_images/e94d32ba4f07cd72bf6d07af35ed39fd8e588f6df5e5c683c9c3ffc6a569c814.png ../_images/73ca1d3a32fa44465d582480ed6ca0801fdb88bda393f51335b58dc63409dc38.png

From these two matrices, we can determine the minimum and maximum distance between centroids of touching objects (cells) in the voronoi image. These are estimated minor and major axis of the segmented objects.

min_distance = cle.minimum_distance_of_touching_neighbors(distance_matrix, touch_matrix)
max_distance = cle.maximum_distance_of_touching_neighbors(distance_matrix, touch_matrix)

print("minimum distance of touching neihbors", cle.mean_of_all_pixels(min_distance))
print("maximum distance of touching neihbors", cle.mean_of_all_pixels(max_distance))
minimum distance of touching neihbors 10.007136855014535
maximum distance of touching neihbors 16.39732325354288

Distance visualisation#

Finally, let’s visualize distances between neighbors in a colored mesh.

mesh = cle.draw_distance_mesh_between_touching_labels(voronoi_diagram)
cle.imshow(mesh, colorbar=True, colormap="jet")
../_images/9f0553ecae43a28e174bf29f26dabf330f153733de0b57c707510571b6fe41e4.png