{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Voronoi diagrams\n", "This notebook was translated from [ImageJ Macro](https://clij.github.io/clij2-docs/md/voronoi/).\n", "\n", "It shows how to create a Voronoi diagram out of a binary image in the GPU.\n", "\n", "## Initialize GPU" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "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": "markdown", "metadata": {}, "source": [ "## Get example data and push it to the GPU" ] }, { "cell_type": "code", "execution_count": 2, "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
min8.0
max248.0
\n", "\n", "
" ], "text/plain": [ "cl.OCLArray([[ 40., 32., 24., ..., 216., 200., 200.],\n", " [ 56., 40., 24., ..., 232., 216., 216.],\n", " [ 64., 48., 24., ..., 240., 232., 232.],\n", " ...,\n", " [ 72., 80., 80., ..., 48., 48., 48.],\n", " [ 80., 80., 80., ..., 48., 48., 48.],\n", " [ 96., 88., 80., ..., 48., 48., 48.]], dtype=float32)" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# load data\n", "image = cle.asarray(imread('https://samples.fiji.sc/blobs.png'))\n", "image" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "## Binarze image first" ] }, { "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.greater_constant(image, constant=128)\n", "binary" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "## Label objects and fill gaps between them" ] }, { "cell_type": "code", "execution_count": 6, "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
min1.0
max63.0
\n", "\n", "
" ], "text/plain": [ "cl.OCLArray([[ 7, 7, 7, ..., 60, 60, 60],\n", " [ 7, 7, 7, ..., 60, 60, 60],\n", " [ 7, 7, 7, ..., 60, 60, 60],\n", " ...,\n", " [ 4, 4, 4, ..., 57, 57, 57],\n", " [ 4, 4, 4, ..., 57, 57, 57],\n", " [ 4, 4, 4, ..., 57, 57, 57]], dtype=uint32)" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "labels = cle.voronoi_labeling(binary)\n", "labels" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Make binary voronoi diagram edge image" ] }, { "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)
dtypeuint8
size63.5 kB
min0.0
max1.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=uint8)" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "voronoi = cle.detect_label_edges(labels)\n", "voronoi" ] }, { "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.13" } }, "nbformat": 4, "nbformat_minor": 4 }