{
"cells": [
{
"cell_type": "markdown",
"id": "72153cb7-115a-4e62-8b13-58f1cb309944",
"metadata": {},
"source": [
"# Verbundene Komponenten-Kennzeichnung auf Oberfl\u00e4chen\n",
"Dieses Notebook demonstriert, wie man Objekte anhand ihrer Konnektivit\u00e4t unterscheidet."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "effd8f07-5c88-4649-8ba0-d134fbf68017",
"metadata": {},
"outputs": [],
"source": [
"import napari_process_points_and_surfaces as nppas\n",
"import pyclesperanto_prototype as cle\n",
"import napari_simpleitk_image_processing as nsitk\n",
"from skimage.data import cells3d\n",
"import stackview"
]
},
{
"cell_type": "markdown",
"id": "f4a30689-caec-4348-a77f-01503fe23441",
"metadata": {},
"source": [
"Wir verwenden ein 3D-Bild von Zellkernen..."
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "c106f052-c82e-4718-b629-eb5d3a02a7b2",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"\n",
" \n",
" | \n",
"\n",
"\n",
"\n",
"shape | (60, 256, 256) | \n",
"dtype | uint16 | \n",
"size | 7.5 MB | \n",
"min | 0 | max | 65535 | \n",
" \n",
" \n",
" | \n",
"
\n",
"
"
],
"text/plain": [
"StackViewNDArray([[[5311, 4458, 5880, ..., 4220, 6497, 4932],\n",
" [4078, 4552, 3557, ..., 4552, 4884, 5169],\n",
" [3414, 5074, 4363, ..., 4078, 6117, 5406],\n",
" ...,\n",
" [3983, 3983, 2750, ..., 4410, 4600, 5880],\n",
" [3604, 4173, 4600, ..., 5548, 5690, 4268],\n",
" [4078, 4268, 4220, ..., 5359, 6686, 7492]],\n",
"\n",
" [[4220, 4932, 5074, ..., 6449, 6117, 4932],\n",
" [3082, 4979, 4505, ..., 4505, 6971, 5880],\n",
" [4695, 4315, 4695, ..., 4742, 5785, 5074],\n",
" ...,\n",
" [3414, 3841, 3462, ..., 5169, 4742, 5027],\n",
" [4173, 3319, 2798, ..., 5406, 4600, 4837],\n",
" [3225, 3557, 3651, ..., 5453, 5406, 4837]],\n",
"\n",
" [[4552, 4505, 4742, ..., 4458, 4979, 4647],\n",
" [3367, 4268, 4647, ..., 6402, 6022, 5738],\n",
" [3509, 3272, 4647, ..., 5264, 4220, 6259],\n",
" ...,\n",
" [3841, 3936, 3888, ..., 4884, 5169, 4789],\n",
" [3604, 3225, 2798, ..., 4552, 6117, 5690],\n",
" [4126, 4458, 4837, ..., 5928, 6734, 5027]],\n",
"\n",
" ...,\n",
"\n",
" [[5121, 5833, 5738, ..., 5406, 3983, 4220],\n",
" [5311, 5453, 5453, ..., 5169, 5027, 4884],\n",
" [6544, 5121, 5264, ..., 4363, 5406, 4695],\n",
" ...,\n",
" [4884, 2324, 3130, ..., 4552, 4979, 6070],\n",
" [3225, 2513, 2513, ..., 4742, 3035, 2418],\n",
" [3272, 2798, 3888, ..., 3604, 4268, 3746]],\n",
"\n",
" [[5359, 5928, 5738, ..., 4268, 4505, 4031],\n",
" [7445, 6829, 4268, ..., 4884, 5596, 3082],\n",
" [6497, 6544, 4932, ..., 3604, 5359, 4410],\n",
" ...,\n",
" [5833, 2703, 3367, ..., 3177, 4220, 4220],\n",
" [4031, 3509, 5216, ..., 4268, 3604, 4932],\n",
" [3557, 2987, 3936, ..., 3604, 3462, 5264]],\n",
"\n",
" [[6402, 6781, 4789, ..., 5928, 6022, 3651],\n",
" [4363, 4837, 6307, ..., 4315, 3651, 4126],\n",
" [4979, 4695, 4742, ..., 4789, 4837, 4552],\n",
" ...,\n",
" [4078, 2513, 3035, ..., 1849, 3888, 4363],\n",
" [3936, 3888, 4315, ..., 5216, 4031, 5643],\n",
" [4173, 4505, 3794, ..., 4979, 6781, 4031]]],\n",
" dtype=uint16)"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"image = cells3d()[:,1]\n",
"stackview.insight(image)"
]
},
{
"cell_type": "markdown",
"id": "b5fadf5f-9021-4276-b747-adc74c513f73",
"metadata": {},
"source": [
"... und segmentieren die Zellkerne, was zu einem 3D-Bin\u00e4rbild f\u00fchrt."
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "86f69768-b651-41df-b412-9dc658794d61",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"\n",
" \n",
" | \n",
"\n",
"\n",
"\n",
"shape | (60, 256, 256) | \n",
"dtype | uint8 | \n",
"size | 3.8 MB | \n",
"min | 0 | max | 1 | \n",
" \n",
" \n",
" | \n",
"
\n",
"
"
],
"text/plain": [
"StackViewNDArray([[[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]],\n",
"\n",
" [[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]],\n",
"\n",
" [[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]],\n",
"\n",
" ...,\n",
"\n",
" [[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]],\n",
"\n",
" [[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]],\n",
"\n",
" [[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=uint8)"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"labels = cle.voronoi_otsu_labeling(image, spot_sigma=9)\n",
"binary = cle.erode_labels(labels) > 0\n",
"\n",
"stackview.insight(binary)"
]
},
{
"cell_type": "markdown",
"id": "3e204746-5e2c-4f2b-81f2-b865414de79c",
"metadata": {},
"source": [
"Wir konvertieren dieses Bin\u00e4rbild in einen Oberfl\u00e4chendatensatz."
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "a70aa6f5-0aea-496f-888f-212a1fec6151",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"\n",
" \n",
" | \n",
"\n",
"nppas.SurfaceTuple \n",
"\n",
"origin (z/y/x) | [0. 0. 0.] | \n",
"center of mass(z/y/x) | 34.703,124.973,131.513 | \n",
"scale(z/y/x) | 1.000,1.000,1.000 | \n",
"bounds (z/y/x) | 16.500...59.000 0.000...255.000 0.000...255.000 | \n",
"average size | 97.003 | \n",
"number of vertices | 151354 | \n",
"number of faces | 301006 | \n",
"\n",
" \n",
"\n",
" | \n",
"
\n",
"
"
],
"text/plain": [
"(array([[ 16.5, 79. , 54. ],\n",
" [ 17. , 78.5, 54. ],\n",
" [ 17. , 79. , 53.5],\n",
" ...,\n",
" [ 58.5, 101. , 46. ],\n",
" [ 59. , 100.5, 47. ],\n",
" [ 58.5, 101. , 47. ]], dtype=float32),\n",
" array([[ 2, 1, 0],\n",
" [ 4, 3, 0],\n",
" [ 4, 0, 1],\n",
" ...,\n",
" [151351, 151184, 151186],\n",
" [151351, 151186, 151353],\n",
" [151353, 151186, 151177]], dtype=int64))"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"surface = nppas.all_labels_to_surface(binary)\n",
"surface"
]
},
{
"cell_type": "markdown",
"id": "66448a87-e0bd-4d3a-9f19-75383eda642a",
"metadata": {},
"source": [
"Durch Anwendung der [Verbundene Komponenten-Kennzeichnung](https://en.wikipedia.org/wiki/Connected-component_labeling) auf die Oberfl\u00e4che k\u00f6nnen wir Scheitelpunkte/Fl\u00e4chen identifizieren, die verbunden sind, und diejenigen unterscheiden, die es nicht sind. Das Ergebnis ist ebenfalls ein Oberfl\u00e4chendatensatz, bei dem die Scheitelpunktwerte dem `n`-ten Label entsprechen, zu dem diese Objekte geh\u00f6ren. Daraus k\u00f6nnen Sie schlie\u00dfen, dass es in diesem Bild 38 Zellkerne gibt, basierend auf der maximalen Anzahl dieser Oberfl\u00e4che."
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "c4ece9bf-20f7-49fe-b2b5-3cba8afdfe70",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"\n",
" \n",
" | \n",
"\n",
"nppas.SurfaceTuple \n",
"\n",
"origin (z/y/x) | [0. 0. 0.] | \n",
"center of mass(z/y/x) | 34.703,124.973,131.513 | \n",
"scale(z/y/x) | 1.000,1.000,1.000 | \n",
"bounds (z/y/x) | 16.500...59.000 0.000...255.000 0.000...255.000 | \n",
"average size | 97.003 | \n",
"number of vertices | 151354 | \n",
"number of faces | 301006 | \n",
"min | 0 | max | 38 | \n",
" \n",
" \n",
" | \n",
"
\n",
"
"
],
"text/plain": [
"(array([[ 17. , 79. , 53.5],\n",
" [ 17. , 78.5, 54. ],\n",
" [ 16.5, 79. , 54. ],\n",
" ...,\n",
" [ 42. , 67.5, 115. ],\n",
" [ 41. , 68. , 115.5],\n",
" [ 43. , 66.5, 115. ]], dtype=float32),\n",
" array([[ 0, 1, 2],\n",
" [ 6, 8, 2],\n",
" [ 6, 2, 1],\n",
" ...,\n",
" [8961, 8953, 8957],\n",
" [8961, 8957, 8956],\n",
" [8956, 8957, 8924]], dtype=int64),\n",
" array([ 0, 0, 0, ..., 38, 38, 38], dtype=int64))"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"surface_connected_components = nppas.connected_component_labeling(surface)\n",
"surface_connected_components.cmap = 'hsv'\n",
"surface_connected_components"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d15cd639-914c-4250-b355-cc86c9f8060f",
"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
}