{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Select labels based on their size\n", "After an image has been segmented and objects are labeled, we may want to remove objects that are either to small or to large to be considered as objects, _nuclei_ for example. " ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "c:\\structure\\code\\pyclesperanto_prototype\\pyclesperanto_prototype\\_tier0\\_device.py:77: UserWarning: No OpenCL device found with GTX in their name. Using gfx1035 instead.\n", " warnings.warn(f\"No OpenCL device found with {name} in their name. Using {device.name} instead.\")\n" ] }, { "data": { "text/plain": [ "" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pyclesperanto_prototype as cle\n", "\n", "from skimage.io import imread\n", "import matplotlib\n", "import numpy as np\n", "import stackview\n", "\n", "# initialize GPU\n", "cle.select_device(\"GTX\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We start from a labeled version of the blobs image." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
shape(254, 256)
dtypeuint32
size254.0 kB
min0
max63
\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]], dtype=uint32)" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# load data\n", "label_image = imread('../../data/blobs_labeled.tif')\n", "\n", "stackview.insight(label_image)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's assume we're not interested in the very small objects as they might be result of a false segmentation of some noise. We do know that the objects we imaged have a certain minimum size. From this physical guess, we need to estimate a number of pixels (in 2D) or voxels (in 3D) object are large. We can then use this number as `size_threshold` in pixels or voxels." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "
\n", "\n", "\n", "cle._ image
\n", "\n", "\n", "\n", "\n", "\n", "
shape(254, 256)
dtypeuint32
size254.0 kB
min0.0
max52.0
\n", "\n", "
" ], "text/plain": [ "cl.OCLArray([[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]], dtype=uint32)" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "size_threshold = 200 # pixels\n", "\n", "large_labels_only = cle.exclude_small_labels(label_image, maximum_size=size_threshold)\n", "\n", "large_labels_only" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can use a similar function to visualize the objects that have been removed above in a separate label image." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "
\n", "\n", "\n", "cle._ image
\n", "\n", "\n", "\n", "\n", "\n", "
shape(254, 256)
dtypeuint32
size254.0 kB
min0.0
max11.0
\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": [ "small_labels_only = cle.exclude_large_labels(label_image, minimum_size=size_threshold)\n", "\n", "small_labels_only" ] }, { "cell_type": "code", "execution_count": null, "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": 4 }