{
"cells": [
{
"cell_type": "markdown",
"id": "ca4f7690-018d-4442-9037-d96de2ebd3aa",
"metadata": {},
"source": [
"# Voronoi-Otsu-Labeling on binary images\n",
"The Voronoi-Otsu-Labeling algorithm can also be used on binary images, leading to similar results like the binary watershed in ImageJ. It splits roundish objects in case they stick together."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "f2e65070-7047-43e0-8cb5-798dc2dad5ec",
"metadata": {},
"outputs": [],
"source": [
"from skimage.io import imread\n",
"from napari_segment_blobs_and_things_with_membranes import voronoi_otsu_labeling, threshold_otsu\n",
"import stackview"
]
},
{
"cell_type": "markdown",
"id": "71c1a312-26a4-4cf1-8ece-f621f2e08331",
"metadata": {},
"source": [
"To demonstrate this, we start wit a binary image."
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "488052cc-7a36-4797-9d19-6741a4a31635",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"\n",
" \n",
" | \n",
"\n",
"nsbatwm made image \n",
"\n",
"shape | (254, 256) | \n",
"dtype | int32 | \n",
"size | 254.0 kB | \n",
"min | 0 | max | 1 | \n",
" \n",
"\n",
" | \n",
"
\n",
"
"
],
"text/plain": [
"StackViewNDArray([[0, 0, 0, ..., 1, 1, 1],\n",
" [0, 0, 0, ..., 1, 1, 1],\n",
" [0, 0, 0, ..., 1, 1, 1],\n",
" ...,\n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0]])"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"blobs_image = imread(\"../../data/blobs.tif\")\n",
"binary_image = threshold_otsu(blobs_image)\n",
"\n",
"binary_image"
]
},
{
"cell_type": "markdown",
"id": "29ea255c-6c01-465c-8413-55729d4b3223",
"metadata": {},
"source": [
"We now apply Voronoi-Otsu-Labeling to the binary image."
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "40a18c69-f042-4efc-bb62-64fa0fa1a60d",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"\n",
" \n",
" | \n",
"\n",
"nsbatwm made image \n",
"\n",
"shape | (254, 256) | \n",
"dtype | int32 | \n",
"size | 254.0 kB | \n",
"min | 0 | max | 67 | \n",
" \n",
"\n",
" | \n",
"
\n",
"
"
],
"text/plain": [
"StackViewNDArray([[0, 0, 0, ..., 5, 5, 5],\n",
" [0, 0, 0, ..., 5, 5, 5],\n",
" [0, 0, 0, ..., 5, 5, 5],\n",
" ...,\n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0]])"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"label_image = voronoi_otsu_labeling(binary_image, spot_sigma=3.5)\n",
"\n",
"label_image"
]
},
{
"cell_type": "markdown",
"id": "a85451b3-5572-4186-bc95-ce03f422d8e0",
"metadata": {},
"source": [
"## Exercise\n",
"Below you see the result of the Watershed in ImageJ. How do you need to modify the `label_image` result above to turn it again into a binary image like the result of ImageJ?"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "36ad7baf-7d26-473f-a428-6f2096341c8f",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"\n",
" \n",
" | \n",
"\n",
"\n",
"\n",
"shape | (254, 256) | \n",
"dtype | uint8 | \n",
"size | 63.5 kB | \n",
"min | 0 | max | 255 | \n",
" \n",
" \n",
" | \n",
"
\n",
"
"
],
"text/plain": [
"StackViewNDArray([[ 0, 0, 0, ..., 255, 255, 255],\n",
" [ 0, 0, 0, ..., 255, 255, 255],\n",
" [ 0, 0, 0, ..., 255, 255, 255],\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": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"binary_watershed_imagej = imread(\"../../data/blobs_otsu_watershed.tif\")\n",
"\n",
"stackview.insight(binary_watershed_imagej)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "74ce16b1-98ae-4e5a-b774-8c3a6e91c0fd",
"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.13"
}
},
"nbformat": 4,
"nbformat_minor": 5
}