{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "e4ae0d20-94bc-4065-9fa6-a401da9db40b", "metadata": {}, "outputs": [], "source": [ "import stackview\n", "\n", "import pyclesperanto_prototype as cle\n", "from skimage.data import cells3d\n", "import matplotlib.pyplot as plt\n", "from napari_segment_blobs_and_things_with_membranes import seeded_watershed\n", "import numpy as np" ] }, { "cell_type": "code", "execution_count": 2, "id": "6ca3835f-66c4-4feb-84fb-5e9167f73927", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "
\n", "\n", "\n", "cle._ image
\n", "\n", "\n", "\n", "\n", "\n", "
shape(256, 256)
dtypefloat32
size256.0 kB
min1.6019913
max255.0
\n", "\n", "
" ], "text/plain": [ "cl.OCLArray([[26.001995 , 30.142883 , 39.691216 , ..., 16.870068 , 15.499411 ,\n", " 15.279642 ],\n", " [26.21598 , 29.76118 , 43.6933 , ..., 16.442099 , 16.523066 ,\n", " 15.892679 ],\n", " [26.8348 , 35.174408 , 48.880978 , ..., 19.501497 , 17.575638 ,\n", " 18.090357 ],\n", " ...,\n", " [ 7.7439218, 8.114057 , 7.859589 , ..., 25.782228 , 24.949425 ,\n", " 27.73122 ],\n", " [ 8.518892 , 9.022045 , 9.380613 , ..., 22.942596 , 26.204412 ,\n", " 24.313253 ],\n", " [ 7.9810395, 7.911639 , 9.536764 , ..., 17.876371 , 20.577202 ,\n", " 21.294338 ]], dtype=float32)" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# load image\n", "image = cells3d()\n", "membrane2d = cle.asarray(image[30,0])\n", "membrane2d = membrane2d * 255 / membrane2d.max()\n", "membrane2d" ] }, { "cell_type": "code", "execution_count": 3, "id": "505d8427-3c46-4bea-8a86-1d7db584cf39", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "36bddc17c3c6426c867354a7fe853fe1", "version_major": 2, "version_minor": 0 }, "text/plain": [ "VBox(children=(interactive(children=(FloatSlider(value=2.0, description='blur_sigma', max=25.0, step=1.0), Flo…" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def segment_image(image, \n", " blur_sigma:float = 2,\n", " threshold:float = 11,\n", " smooth_outline_radius:int=5, \n", " image_brightness:float=1.5, \n", " show_image:bool=True, \n", " show_border:bool=True):\n", " \n", " labels = seeded_watershed(membrane2d, \n", " cle.label(\n", " cle.gaussian_blur(\n", " membrane2d, \n", " sigma_x=blur_sigma, \n", " sigma_y=blur_sigma) \n", " < threshold\n", " )\n", " )\n", " \n", " processed = \\\n", " cle.detect_label_edges(\n", " cle.extend_labeling_via_voronoi(\n", " cle.opening_labels(labels, radius=smooth_outline_radius)\n", " )\n", " ) * 255\n", " \n", " if show_image and show_border:\n", " return stackview.merge_rgb(image * image_brightness, processed, image * image_brightness)\n", " elif show_image:\n", " return image * image_brightness\n", " elif show_border:\n", " return processed\n", " else:\n", " return np.zeros(processed.shape)\n", " \n", "stackview.interact(segment_image, membrane2d, continuous_update=True)" ] }, { "cell_type": "code", "execution_count": 5, "id": "de09512e-bbc1-4b82-9f2e-e0ee6885d915", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(256, 256, 3)" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "afe9f04a-7dcb-4d52-b535-de6af808adc6", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "aa5a1aea-ea5a-463a-9a86-849dc759c22b", "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" }, "widgets": { "application/vnd.jupyter.widget-state+json": { "state": { "1389924df8ef4bda96fd09719ceb66ec": { "model_module": "@jupyter-widgets/controls", "model_module_version": "2.0.0", "model_name": "VBoxModel", "state": { "children": [ "IPY_MODEL_e89c5b1416d74871b698bc81c2522286" ], "layout": "IPY_MODEL_6946070d284a48e0b2727da23f1ea988" } }, "182c0ff360a646fb9312b27124c016f0": { "model_module": "@jupyter-widgets/controls", "model_module_version": "2.0.0", "model_name": "IntSliderModel", "state": { "behavior": "drag-tap", "description": "Slice", "layout": "IPY_MODEL_2f3b6710b5064f29a6e141dfa878149b", "max": 255, "style": "IPY_MODEL_64be5feae4e6431c93693df0160eace1", "value": 128 } }, "2b54c9d6981a4804ac21293dfef3b458": { "model_module": "@jupyter-widgets/controls", "model_module_version": "2.0.0", "model_name": "LabelModel", "state": { "layout": "IPY_MODEL_7d9a7d93a88540df9cd5318cc77af402", "style": "IPY_MODEL_f6d370c8912844ee9d17c0021d060c57", "value": "segment_image(..., blur_sigma=2.0, threshold=11.0, smooth_outline_radius=5, image_brightness=1.5, show_image=True, show_border=True)" } }, "2f3b6710b5064f29a6e141dfa878149b": { "model_module": "@jupyter-widgets/base", "model_module_version": "2.0.0", "model_name": "LayoutModel", "state": {} }, "36bddc17c3c6426c867354a7fe853fe1": { "model_module": "@jupyter-widgets/controls", "model_module_version": "2.0.0", "model_name": "VBoxModel", "state": { "children": [ "IPY_MODEL_960191c6b1634f6cb8bb87a385a9a7b7", "IPY_MODEL_2b54c9d6981a4804ac21293dfef3b458", "IPY_MODEL_ca15fcf1ba4948f69b1b33cf36ac36e8", "IPY_MODEL_182c0ff360a646fb9312b27124c016f0" ], "layout": "IPY_MODEL_fc45060aa3534388a88a47c29a8e0409" } }, "48f57a03ab35445592085fbcfa74c235": { "model_module": "@jupyter-widgets/controls", "model_module_version": "2.0.0", "model_name": "IntSliderModel", "state": { "behavior": "drag-tap", "description": "smooth_outline_radius", "layout": "IPY_MODEL_da84570844614c9e8f1a4af9d43a5a82", "style": "IPY_MODEL_cfd5a113d1594c8eb865718c57d0a56b", "value": 5 } }, "4cfb048cfca847048d1763a29d014cd0": { "model_module": "@jupyter-widgets/controls", "model_module_version": "2.0.0", "model_name": "SliderStyleModel", "state": { "description_width": "" } }, "524414c0a942460d92a451213f026bf0": { "model_module": "@jupyter-widgets/controls", "model_module_version": "2.0.0", "model_name": "SliderStyleModel", "state": { "description_width": "" } }, "64be5feae4e6431c93693df0160eace1": { "model_module": "@jupyter-widgets/controls", "model_module_version": "2.0.0", "model_name": "SliderStyleModel", "state": { "description_width": "" } }, "68204d453c6e4dc284fa6939f5df8362": { "model_module": "@jupyter-widgets/controls", "model_module_version": "2.0.0", "model_name": "CheckboxStyleModel", "state": { "description_width": "" } }, "6946070d284a48e0b2727da23f1ea988": { "model_module": "@jupyter-widgets/base", "model_module_version": "2.0.0", "model_name": "LayoutModel", "state": {} }, "79d92662c37448e68020d86f0c2bf07d": { "model_module": "@jupyter-widgets/base", "model_module_version": "2.0.0", "model_name": "LayoutModel", "state": {} }, "7d9a7d93a88540df9cd5318cc77af402": { "model_module": "@jupyter-widgets/base", "model_module_version": "2.0.0", "model_name": "LayoutModel", "state": {} }, "8771f8052f2e447eb0adf984cd40c7b1": { "model_module": "@jupyter-widgets/controls", "model_module_version": "2.0.0", "model_name": "CheckboxStyleModel", "state": { "description_width": "" } }, "8b056cc448bb45a18974a89690e29010": { "model_module": "@jupyter-widgets/base", "model_module_version": "2.0.0", "model_name": "LayoutModel", "state": {} }, "960191c6b1634f6cb8bb87a385a9a7b7": { "model_module": "@jupyter-widgets/controls", "model_module_version": "2.0.0", "model_name": "VBoxModel", "state": { "_dom_classes": [ "widget-interact" ], "children": [ "IPY_MODEL_b2cfb23c6e4e481c91115ccb0138007f", "IPY_MODEL_e17c0efdce24464ab3c12bf2c56f9897", "IPY_MODEL_48f57a03ab35445592085fbcfa74c235", "IPY_MODEL_a2e71837546441ba9ac5d84210988cef", "IPY_MODEL_b775c0d946694229a81d5943ee645c06", "IPY_MODEL_b4a33bc84c5f470b919b95cc9275cace", "IPY_MODEL_ba3af2c610aa4c87822fae9b9084e70c" ], "layout": "IPY_MODEL_ce3da5c5751b47abbbf399b0dbc61287" } }, "a07bfec6c1254cb8a2befa74de9312c0": { "model_module": "@jupyter-widgets/base", "model_module_version": "2.0.0", "model_name": "LayoutModel", "state": {} }, "a2e71837546441ba9ac5d84210988cef": { "model_module": "@jupyter-widgets/controls", "model_module_version": "2.0.0", "model_name": "FloatSliderModel", "state": { "behavior": "drag-tap", "description": "image_brightness", "layout": "IPY_MODEL_8b056cc448bb45a18974a89690e29010", "step": null, "style": "IPY_MODEL_aa300c5c5bfe40b2ac91cc5379bee625", "value": 1.5 } }, "a9ea15fbc94640a38721c827a476930e": { "model_module": "@jupyter-widgets/base", "model_module_version": "2.0.0", "model_name": "LayoutModel", "state": {} }, "aa300c5c5bfe40b2ac91cc5379bee625": { "model_module": "@jupyter-widgets/controls", "model_module_version": "2.0.0", "model_name": "SliderStyleModel", "state": { "description_width": "" } }, "b2cfb23c6e4e481c91115ccb0138007f": { "model_module": "@jupyter-widgets/controls", "model_module_version": "2.0.0", "model_name": "FloatSliderModel", "state": { "behavior": "drag-tap", "description": "blur_sigma", "layout": "IPY_MODEL_a07bfec6c1254cb8a2befa74de9312c0", "max": 25, "style": "IPY_MODEL_4cfb048cfca847048d1763a29d014cd0", "value": 2 } }, "b4a33bc84c5f470b919b95cc9275cace": { "model_module": "@jupyter-widgets/controls", "model_module_version": "2.0.0", "model_name": "CheckboxModel", "state": { "description": "show_border", "disabled": false, "layout": "IPY_MODEL_f0cb5bd62f5b4e599834c6648873e505", "style": "IPY_MODEL_8771f8052f2e447eb0adf984cd40c7b1", "value": true } }, "b775c0d946694229a81d5943ee645c06": { "model_module": "@jupyter-widgets/controls", "model_module_version": "2.0.0", "model_name": "CheckboxModel", "state": { "description": "show_image", "disabled": false, "layout": "IPY_MODEL_d00c4d5b14fc4a3e97f6248746bbccb6", "style": "IPY_MODEL_68204d453c6e4dc284fa6939f5df8362", "value": true } }, "ba3af2c610aa4c87822fae9b9084e70c": { "model_module": "@jupyter-widgets/output", "model_module_version": "1.0.0", "model_name": "OutputModel", "state": { "layout": "IPY_MODEL_a9ea15fbc94640a38721c827a476930e" } }, "ca15fcf1ba4948f69b1b33cf36ac36e8": { "model_module": "@jupyter-widgets/controls", "model_module_version": "2.0.0", "model_name": "HBoxModel", "state": { "children": [ "IPY_MODEL_1389924df8ef4bda96fd09719ceb66ec" ], "layout": "IPY_MODEL_ed6776f4fab64594adce4e22a26dfab4" } }, "ce3da5c5751b47abbbf399b0dbc61287": { "model_module": "@jupyter-widgets/base", "model_module_version": "2.0.0", "model_name": "LayoutModel", "state": {} }, "cfd5a113d1594c8eb865718c57d0a56b": { "model_module": "@jupyter-widgets/controls", "model_module_version": "2.0.0", "model_name": "SliderStyleModel", "state": { "description_width": "" } }, "d00c4d5b14fc4a3e97f6248746bbccb6": { "model_module": "@jupyter-widgets/base", "model_module_version": "2.0.0", "model_name": "LayoutModel", "state": {} }, "da84570844614c9e8f1a4af9d43a5a82": { "model_module": "@jupyter-widgets/base", "model_module_version": "2.0.0", "model_name": "LayoutModel", "state": {} }, "e17c0efdce24464ab3c12bf2c56f9897": { "model_module": "@jupyter-widgets/controls", "model_module_version": "2.0.0", "model_name": "FloatSliderModel", "state": { "behavior": "drag-tap", "description": "threshold", "layout": "IPY_MODEL_f2cea26ccdcd410fb6fee6ce6287c394", "step": null, "style": "IPY_MODEL_524414c0a942460d92a451213f026bf0", "value": 11 } }, "e420c5caac40459391c752d536717405": { "model_module": "ipycanvas", "model_module_version": "^0.13", "model_name": "CanvasManagerModel", "state": { "_model_module_version": "^0.13", "_view_module": null, "_view_module_version": "" } }, "e89c5b1416d74871b698bc81c2522286": { "model_module": "ipycanvas", "model_module_version": "^0.13", "model_name": "CanvasModel", "state": { "_canvas_manager": "IPY_MODEL_e420c5caac40459391c752d536717405", "_model_module_version": "^0.13", "_view_module_version": "^0.13", "height": 3, "layout": "IPY_MODEL_79d92662c37448e68020d86f0c2bf07d", "width": 256 } }, "ed6776f4fab64594adce4e22a26dfab4": { "model_module": "@jupyter-widgets/base", "model_module_version": "2.0.0", "model_name": "LayoutModel", "state": {} }, "f0cb5bd62f5b4e599834c6648873e505": { "model_module": "@jupyter-widgets/base", "model_module_version": "2.0.0", "model_name": "LayoutModel", "state": {} }, "f2cea26ccdcd410fb6fee6ce6287c394": { "model_module": "@jupyter-widgets/base", "model_module_version": "2.0.0", "model_name": "LayoutModel", "state": {} }, "f6d370c8912844ee9d17c0021d060c57": { "model_module": "@jupyter-widgets/controls", "model_module_version": "2.0.0", "model_name": "LabelStyleModel", "state": { "description_width": "", "font_family": "Courier", "font_size": null, "font_style": null, "font_variant": null, "font_weight": null, "text_color": null, "text_decoration": null } }, "fc45060aa3534388a88a47c29a8e0409": { "model_module": "@jupyter-widgets/base", "model_module_version": "2.0.0", "model_name": "LayoutModel", "state": {} } }, "version_major": 2, "version_minor": 0 } } }, "nbformat": 4, "nbformat_minor": 5 }