{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Counting blobs with pyclesperanto\n", "A common use case for image processing in the biology context is counting blobs of high signal intensity surrounded by a low intensity background. Thresholding is the method of choice in this scenario. We demonstrate it with Otsu's thresholding method (Otsu et al., IEEE Transactions on Systems, Man, and Cybernetics, Vol. 9 (1), 1979)" ] }, { "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, imsave, imshow\n", "import matplotlib\n", "import numpy as np\n", "\n", "# initialize GPU\n", "cle.select_device(\"GTX\")" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Image size: (254, 256)\n" ] } ], "source": [ "# load data\n", "image = imread('https://samples.fiji.sc/blobs.png')\n", "print(\"Image size: \" + str(image.shape))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Image processing using pyclesperanto\n", "\n", "After initializing GPU and pushing images there, workflows are constructed like this:" ] }, { "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)
dtypefloat32
size254.0 kB
min10.285456
max248.0
\n", "\n", "
" ], "text/plain": [ "cl.OCLArray([[ 41.428753, 34.696438, 27.733936, ..., 220.92714 , 211.21164 ,\n", " 206.71573 ],\n", " [ 47.421425, 38.878723, 30.323011, ..., 228.32323 , 220.60194 ,\n", " 216.83534 ],\n", " [ 48.121437, 40.610855, 33.357384, ..., 235.32935 , 229.7049 ,\n", " 226.88821 ],\n", " ...,\n", " [ 74.4386 , 76.32904 , 77.03725 , ..., 48.000324, 48.00075 ,\n", " 48.001007],\n", " [ 81.793655, 81.17787 , 79.80763 , ..., 48. , 48. ,\n", " 48. ],\n", " [ 88.816925, 85.382095, 81.478806, ..., 48. , 48. ,\n", " 48. ]], dtype=float32)" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "blurred = cle.gaussian_blur(image, sigma_x=1, sigma_y=1)\n", "blurred" ] }, { "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)
dtypeuint8
size63.5 kB
min0.0
max1.0
\n", "\n", "
" ], "text/plain": [ "cl.OCLArray([[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]], dtype=uint8)" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "binary = cle.threshold_otsu(blurred)\n", "binary" ] }, { "cell_type": "code", "execution_count": 5, "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
max62.0
\n", "\n", "
" ], "text/plain": [ "cl.OCLArray([[ 0, 0, 0, ..., 59, 59, 59],\n", " [ 0, 0, 0, ..., 59, 59, 59],\n", " [ 0, 0, 0, ..., 59, 59, 59],\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": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "labeled = cle.connected_components_labeling_box(binary)\n", "labeled" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Number of objects in the image: 62.0\n" ] } ], "source": [ "# The maximium intensity in a label image corresponds to the number of objects\n", "num_labels = cle.maximum_of_all_pixels(labeled)\n", "print(\"Number of objects in the image: \" + str(num_labels))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Addendum: Remove labels on edges\n", "In case the size of the objects is relevant, one should exclude the object which were not fully imaged and thus, touch the image border." ] }, { "cell_type": "code", "execution_count": 7, "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
max44.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": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Exclude Labels On Edges\n", "labels_excl_edges = cle.exclude_labels_on_edges(labeled)\n", "labels_excl_edges" ] } ], "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 }