{
"cells": [
{
"cell_type": "markdown",
"id": "cef9a48c-33df-4296-9380-c0c9a1dbad95",
"metadata": {},
"source": [
"# Identifying labels which touch the background\n",
"In developmental biology it is a common task to differentiate layers of cells, for example in epithelial tissue. Therefore it might be useful to know if a cell is part of an outer layer, if it touches the background. In this notebook we measure and visualize this.\n",
"\n",
"For demonstrating it we simulate a clumb of cells."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "5c01c538-6ad9-47b7-93bd-c58fbcf81006",
"metadata": {},
"outputs": [],
"source": [
"import pyclesperanto_prototype as cle\n",
"\n",
"# import a function from a file in the same folder\n",
"from simulated_cell_clumb import simulate_data"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "66703104-00a4-4a86-ba1c-bf25a2dacd96",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"\n",
" \n",
" | \n",
"\n",
"cle._ image \n",
"\n",
"shape | (200, 200) | \n",
"dtype | uint32 | \n",
"size | 156.2 kB | \n",
"min | 0.0 | max | 41.0 | \n",
" \n",
"\n",
" | \n",
"
\n",
"
"
],
"text/plain": [
"cl.OCLArray([[0, 0, 0, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" ...,\n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0]], dtype=uint32)"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cells = simulate_data()\n",
"cells"
]
},
{
"cell_type": "markdown",
"id": "bf18c763-9eee-409f-aa18-a8efa4a21d8a",
"metadata": {},
"source": [
"## Determining which cells touch the background\n",
"To determine which cells touch the background, we need to produce a touch matrix which tells us which objects touch which others."
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "dfdb2c9b-71db-475a-8861-6b235352746b",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"\n",
" \n",
" | \n",
"\n",
"cle._ image \n",
"\n",
"shape | (42, 42) | \n",
"dtype | float32 | \n",
"size | 6.9 kB | \n",
"min | 0.0 | max | 1.0 | \n",
" \n",
" \n",
" | \n",
"
\n",
"
"
],
"text/plain": [
"cl.OCLArray([[0., 1., 1., ..., 1., 1., 1.],\n",
" [1., 0., 1., ..., 0., 0., 0.],\n",
" [1., 1., 0., ..., 0., 0., 0.],\n",
" ...,\n",
" [1., 0., 0., ..., 0., 0., 1.],\n",
" [1., 0., 0., ..., 0., 0., 1.],\n",
" [1., 0., 0., ..., 1., 1., 0.]], dtype=float32)"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"touch_matrix = cle.generate_touch_matrix(cells)\n",
"touch_matrix"
]
},
{
"cell_type": "markdown",
"id": "8b5d8105-c5e2-4388-a3cd-13f118feecb7",
"metadata": {},
"source": [
"The first row and column in this image represent objects touching the background. We can read out this first row or column like this:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "e1005bb2-5399-42bc-bf1e-f554a2519c10",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"cle.array([0. 1. 1. 1. 0. 1. 1. 1. 0. 1. 0. 0. 1. 0. 0. 0. 0. 1. 0. 0. 0. 0. 1. 1.\n",
" 0. 0. 1. 0. 0. 1. 0. 1. 1. 0. 0. 1. 1. 0. 0. 1. 1. 1.], dtype=float32)
"
],
"text/plain": [
"cl.OCLArray([0., 1., 1., 1., 0., 1., 1., 1., 0., 1., 0., 0., 1., 0., 0., 0., 0.,\n",
" 1., 0., 0., 0., 0., 1., 1., 0., 0., 1., 0., 0., 1., 0., 1., 1., 0.,\n",
" 0., 1., 1., 0., 0., 1., 1., 1.], dtype=float32)"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"touching_background = touch_matrix[0]\n",
"touching_background"
]
},
{
"cell_type": "markdown",
"id": "5c3ee386-78fc-46d3-96ce-26da761990d7",
"metadata": {},
"source": [
"And we can visualized it in the original image coordinates."
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "f4b208ac-fe4c-4cb8-b03b-2ad7835738f1",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"\n",
" \n",
" | \n",
"\n",
"cle._ image \n",
"\n",
"shape | (200, 200) | \n",
"dtype | float32 | \n",
"size | 156.2 kB | \n",
"min | 0.0 | max | 1.0 | \n",
" \n",
" \n",
" | \n",
"
\n",
"
"
],
"text/plain": [
"cl.OCLArray([[0., 0., 0., ..., 0., 0., 0.],\n",
" [0., 0., 0., ..., 0., 0., 0.],\n",
" [0., 0., 0., ..., 0., 0., 0.],\n",
" ...,\n",
" [0., 0., 0., ..., 0., 0., 0.],\n",
" [0., 0., 0., ..., 0., 0., 0.],\n",
" [0., 0., 0., ..., 0., 0., 0.]], dtype=float32)"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cle.replace_intensities(cells, touching_background)"
]
},
{
"cell_type": "markdown",
"id": "da0904ae-369b-41eb-9a7b-05ff688e955e",
"metadata": {},
"source": [
"Or we can get a label image representing those objects."
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "1737ff2a-3d2a-45fe-a1cf-8d86780a51c4",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"\n",
" \n",
" | \n",
"\n",
"cle._ image \n",
"\n",
"shape | (200, 200) | \n",
"dtype | uint32 | \n",
"size | 156.2 kB | \n",
"min | 0.0 | max | 20.0 | \n",
" \n",
"\n",
" | \n",
"
\n",
"
"
],
"text/plain": [
"cl.OCLArray([[0, 0, 0, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" ...,\n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0]], dtype=uint32)"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cle.exclude_labels(cle.binary_not([touching_background]), cells)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a3f8ca5c-f4fb-456b-8e73-11f4d87f7166",
"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.15"
}
},
"nbformat": 4,
"nbformat_minor": 5
}