{ "cells": [ { "cell_type": "markdown", "id": "bdddcc9c-1542-4a75-8e79-3a001858b5ed", "metadata": {}, "source": [ "# Regional properties of label\n", "In this notebook we charcterize labels according to the mean and standard deviation of their propteries, such as size. If similarly sized objects are neighbors, the standard deviation of their size is low. If labels of different size are neighbors the standard deviation of their size is higher. This could be used to identify regions in tissues where cells of different size meet." ] }, { "cell_type": "code", "execution_count": 1, "id": "99d59c92-609e-417b-8918-a48b731087e3", "metadata": {}, "outputs": [], "source": [ "from skimage.io import imread\n", "import pyclesperanto_prototype as cle\n", "import stackview" ] }, { "cell_type": "markdown", "id": "e764b9c5-bfba-4e17-8bb3-10ee4b3697f2", "metadata": {}, "source": [ "The data we use here was derived from maize_clsm.tif was taken from [here](https://github.com/dlegland/mathematical_morphology_with_MorphoLibJ/blob/master/sampleImages/maize_clsm.tif), an image shared by David Legland under [CC-BY 4.0 license](\n", "https://github.com/dlegland/mathematical_morphology_with_MorphoLibJ/blob/master/LICENSE)" ] }, { "cell_type": "code", "execution_count": 2, "id": "3257c9f4-3794-4615-9c3f-cf29fc5a6df4", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
shape(640, 639)
dtypeuint8
size399.4 kB
min0
max255
\n", "\n", "
" ], "text/plain": [ "StackViewNDArray([[50, 27, 27, ..., 26, 26, 26],\n", " [26, 1, 2, ..., 1, 1, 1],\n", " [27, 1, 1, ..., 1, 1, 1],\n", " ...,\n", " [33, 5, 4, ..., 1, 1, 1],\n", " [36, 9, 7, ..., 1, 1, 1],\n", " [57, 34, 34, ..., 25, 24, 25]], dtype=uint8)" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "image = imread(\"../../data/maize_clsm.tif\")\n", "\n", "stackview.insight(image)" ] }, { "cell_type": "markdown", "id": "22f5a387-1934-4d57-b683-9914444285e0", "metadata": {}, "source": [ "A corresponding label image looks like this:" ] }, { "cell_type": "code", "execution_count": 3, "id": "db05867f-95a8-4758-813d-69c5f2a5622a", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "
\n", "\n", "\n", "cle._ image
\n", "\n", "\n", "\n", "\n", "\n", "
shape(640, 639)
dtypeuint32
size1.6 MB
min1.0
max254.0
\n", "\n", "
" ], "text/plain": [ "cl.OCLArray([[ 1, 1, 1, ..., 244, 244, 244],\n", " [ 1, 1, 1, ..., 244, 244, 244],\n", " [ 1, 1, 1, ..., 244, 244, 244],\n", " ...,\n", " [ 6, 6, 6, ..., 234, 234, 234],\n", " [ 6, 6, 6, ..., 234, 234, 234],\n", " [ 6, 6, 6, ..., 234, 234, 234]], dtype=uint32)" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "labels = imread(\"../../data/maize_clsm_labels.tif\")\n", "labels = cle.exclude_small_labels(labels, maximum_size=200)\n", "labels = cle.extend_labeling_via_voronoi(labels)\n", "labels" ] }, { "cell_type": "markdown", "id": "d9d80ea3-0b90-4d60-9d0e-d5558e4e32f5", "metadata": {}, "source": [ "## Measuring size\n", "First we need to quantify size of the objects. We can immediately visualize these measurements as parametric image." ] }, { "cell_type": "code", "execution_count": 4, "id": "8c2449b4-8093-4af8-ad29-cceed245c391", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "
\n", "\n", "\n", "cle._ image
\n", "\n", "\n", "\n", "\n", "\n", "
shape(640, 639)
dtypefloat32
size1.6 MB
min201.0
max14283.0
\n", "\n", "
" ], "text/plain": [ "cl.OCLArray([[10411., 10411., 10411., ..., 5352., 5352., 5352.],\n", " [10411., 10411., 10411., ..., 5352., 5352., 5352.],\n", " [10411., 10411., 10411., ..., 5352., 5352., 5352.],\n", " ...,\n", " [14283., 14283., 14283., ..., 12378., 12378., 12378.],\n", " [14283., 14283., 14283., ..., 12378., 12378., 12378.],\n", " [14283., 14283., 14283., ..., 12378., 12378., 12378.]],\n", " dtype=float32)" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "size_map_image = cle.pixel_count_map(labels)\n", "\n", "size_map_image" ] }, { "cell_type": "markdown", "id": "18204045-8f50-4a19-85ca-697aa46f5eca", "metadata": {}, "source": [ "## Regional properties\n", "We can now summarize those measurments locally, e.g. by measuring the mean size of every cell an its corresponding touching neighbors." ] }, { "cell_type": "code", "execution_count": 5, "id": "034f080c-4578-4b0b-953a-926701b61596", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "
\n", "\n", "\n", "cle._ image
\n", "\n", "\n", "\n", "\n", "\n", "
shape(640, 639)
dtypefloat32
size1.6 MB
min369.33334
max7611.5
\n", "\n", "
" ], "text/plain": [ "cl.OCLArray([[6178.3335, 6178.3335, 6178.3335, ..., 3341.2 , 3341.2 ,\n", " 3341.2 ],\n", " [6178.3335, 6178.3335, 6178.3335, ..., 3341.2 , 3341.2 ,\n", " 3341.2 ],\n", " [6178.3335, 6178.3335, 6178.3335, ..., 3341.2 , 3341.2 ,\n", " 3341.2 ],\n", " ...,\n", " [4809.5 , 4809.5 , 4809.5 , ..., 5657. , 5657. ,\n", " 5657. ],\n", " [4809.5 , 4809.5 , 4809.5 , ..., 5657. , 5657. ,\n", " 5657. ],\n", " [4809.5 , 4809.5 , 4809.5 , ..., 5657. , 5657. ,\n", " 5657. ]], dtype=float32)" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cle.mean_of_touching_neighbors_map(size_map_image, labels)" ] }, { "cell_type": "markdown", "id": "041a720a-12cf-4a80-b4b2-9ef99b0d4719", "metadata": {}, "source": [ "We can also compute the standard deviation of size, which highlights the borders between the regions with cells of different size." ] }, { "cell_type": "code", "execution_count": 6, "id": "333a86d3-2917-4df9-bf69-5b42fa5bd7a5", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "
\n", "\n", "\n", "cle._ image
\n", "\n", "\n", "\n", "\n", "\n", "
shape(640, 639)
dtypefloat32
size1.6 MB
min43.538486
max3917.1946
\n", "\n", "
" ], "text/plain": [ "cl.OCLArray([[3169.5137, 3169.5137, 3169.5137, ..., 2035.8579, 2035.8579,\n", " 2035.8579],\n", " [3169.5137, 3169.5137, 3169.5137, ..., 2035.8579, 2035.8579,\n", " 2035.8579],\n", " [3169.5137, 3169.5137, 3169.5137, ..., 2035.8579, 2035.8579,\n", " 2035.8579],\n", " ...,\n", " [3431.5947, 3431.5947, 3431.5947, ..., 3472.4436, 3472.4436,\n", " 3472.4436],\n", " [3431.5947, 3431.5947, 3431.5947, ..., 3472.4436, 3472.4436,\n", " 3472.4436],\n", " [3431.5947, 3431.5947, 3431.5947, ..., 3472.4436, 3472.4436,\n", " 3472.4436]], dtype=float32)" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cle.standard_deviation_of_touching_neighbors_map(size_map_image, labels)" ] }, { "cell_type": "code", "execution_count": null, "id": "fde3eb53-81dd-4738-b5bb-344d1bbc7dce", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.16" } }, "nbformat": 4, "nbformat_minor": 5 }