{ "cells": [ { "cell_type": "markdown", "id": "8bfa86fc-d854-419e-a91f-da1dbc91e390", "metadata": { "tags": [] }, "source": [ "# Scaling coordinate lists\n", "\n", "Presume you have a list of coordinates derived from an image. In case the image has a specified pixel size, you can convert pixel coordinates in physical coordinates." ] }, { "cell_type": "code", "execution_count": 1, "id": "b2f0de47-ecf1-413e-922e-acbe01b8fc19", "metadata": { "tags": [] }, "outputs": [], "source": [ "from skimage.io import imread\n", "import pyclesperanto_prototype as cle\n", "import stackview" ] }, { "cell_type": "raw", "id": "abfa82f9-6a82-4367-af0f-53991061642e", "metadata": {}, "source": [ "Or starting point for this demonstration is a label image." ] }, { "cell_type": "code", "execution_count": 2, "id": "d6680458-dbc7-4967-824e-761224c5bad0", "metadata": { "tags": [] }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "
\n", "\n", "\n", "cle._ image
\n", "\n", "\n", "\n", "\n", "\n", "
shape(50, 50)
dtypeuint32
size9.8 kB
min0.0
max6.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": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "image = imread(\"../../data/blobs.tif\")[:50,:50]\n", "\n", "label_image = cle.voronoi_otsu_labeling(image, spot_sigma=4)\n", "\n", "label_image" ] }, { "cell_type": "markdown", "id": "8b223465-39d0-43b1-bc3c-43df92601503", "metadata": {}, "source": [ "From the objects in this label image, we can derive centroid coordinates." ] }, { "cell_type": "code", "execution_count": 3, "id": "6d35fad4-7c0a-4eb5-89cb-358302adcd8b", "metadata": { "tags": [] }, "outputs": [ { "data": { "text/html": [ "
cle.array([[ 0.7586207  7.7894735 17.849672  24.59091   29.01266   43.57143  ]\n",
       " [26.275862  44.63158   17.79085    4.071429  47.02532   26.266666 ]], dtype=float32)
" ], "text/plain": [ "cl.OCLArray([[ 0.7586207, 7.7894735, 17.849672 , 24.59091 , 29.01266 ,\n", " 43.57143 ],\n", " [26.275862 , 44.63158 , 17.79085 , 4.071429 , 47.02532 ,\n", " 26.266666 ]], dtype=float32)" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "centroids = cle.centroids_of_labels(label_image)\n", "centroids" ] }, { "cell_type": "markdown", "id": "00f4ca87-0593-45e7-a7f3-70590256cd48", "metadata": { "tags": [] }, "source": [ "Such a point list has d times n numbers for d dimensions (commonly 2 or 3) and n points." ] }, { "cell_type": "code", "execution_count": 4, "id": "3b1979ab-36e9-42fb-865a-4253b004f6fa", "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "(2, 6)" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "centroids.shape" ] }, { "cell_type": "markdown", "id": "f3b3541c-ed8f-4095-8c51-a3ca9c4c96fc", "metadata": { "tags": [] }, "source": [ "In case pixel size is known, we can compute the centroid coordinates in physical units." ] }, { "cell_type": "code", "execution_count": 5, "id": "a1ba4332-a51a-45c4-a81d-64e6376e2eed", "metadata": { "tags": [] }, "outputs": [], "source": [ "pixel_size_in_microns = [0.1, 0.1]" ] }, { "cell_type": "code", "execution_count": 6, "id": "b1db7b9a-352b-4d75-afe3-2c5e2359317b", "metadata": { "tags": [] }, "outputs": [ { "data": { "text/html": [ "
cle.array([[0.07586207 0.77894735 1.7849673  2.459091   2.9012659  4.357143  ]\n",
       " [2.6275861  4.463158   1.779085   0.40714288 4.702532   2.6266668 ]], dtype=float32)
" ], "text/plain": [ "cl.OCLArray([[0.07586207, 0.77894735, 1.7849673 , 2.459091 , 2.9012659 ,\n", " 4.357143 ],\n", " [2.6275861 , 4.463158 , 1.779085 , 0.40714288, 4.702532 ,\n", " 2.6266668 ]], dtype=float32)" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "centroids_in_microns = centroids * cle.asarray([pixel_size_in_microns]).T\n", "\n", "centroids_in_microns" ] }, { "cell_type": "code", "execution_count": null, "id": "ffe27c58-9219-4edd-a99d-9151514d98f7", "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.16" } }, "nbformat": 4, "nbformat_minor": 5 }