{
"cells": [
{
"cell_type": "markdown",
"id": "20746a3f-2a1a-4c94-ab88-d7b0572ccab4",
"metadata": {},
"source": [
"# Modifying borders of tissues\n",
"When segmenting clumbs of objects, such as cells in an organoid, it may happen that the segmented cells at the tissue border, touching the background are not segmented properly. In order to modify only those, it may make sense to post-process a label image by only modifying the object borders close by the background."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "efe83c42-9303-43d0-9084-cd2f511ed83a",
"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": "markdown",
"id": "c17126e1-5731-4cde-942d-84df26dd1850",
"metadata": {},
"source": [
"For demonstrating it we simulate a clumb of cells."
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "8b964cbb-0f1b-489d-bbf8-6096614cd00c",
"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 | 40.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": "6a89d831-4dca-45d3-9238-f4105ba11f89",
"metadata": {},
"source": [
"We can erode the entire field of labels as if it was a binary image, while keeping the pixels labeled. With this, only the labels at the border to the background are modified."
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "5f966526-8f00-4316-bb3a-b6fa41b48633",
"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 | 40.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": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"eroded_cells = cle.erode_connected_labels(cells, radius=5)\n",
"eroded_cells"
]
},
{
"cell_type": "markdown",
"id": "385ea1b7-d861-45b4-ac35-efd5038166a2",
"metadata": {},
"source": [
"Just to compare, how this is different from label erosion:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "7f69b7ab-1641-43dc-9aed-4bd47229395a",
"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 | 40.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": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"eroded_cells2 = cle.erode_labels(eroded_cells, radius=5)\n",
"eroded_cells2"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2d4908b5-f238-48d1-bee4-c4095ba1924d",
"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
}