{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Gaussian spot centroid detection\n\nThis demo shows how the ``itom.algorithms`` can be used.\nIn this example a camera ``DummyGrabber`` is used to acquire a moving gaussian spot.\nFor the centroid detection the ``itom.algorithms.centroidxy`` function is used. \nFinally, this examples shows how a centroid position marker is added to the gaussian spot image.\n\n

Note

For older ``itom`` versions the ``itom.filter`` function is used for the centroidXY algorithm.\n By using the itom.algorithms you will get the algorithms information by a pop-up during typing.

\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from itom import dataIO\nfrom itom import dataObject\nfrom itom import plot2\nfrom itom import timer\n\ntry:\n from itom import algorithms\n\n hasItomAlgo = True\nexcept ImportError:\n from itom import filter\n\n hasItomAlgo = False\n print(\n \"The itom.algorithms module cannot be used because\" \"of an older itom version. Use the itom.filter functions.\"\n )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Constructor of the live gaussianSpot centroid detection.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "class GaussianSpotCentroidDetection:\n def __init__(self):\n \"\"\"Constructor function of the class.\"\"\"\n # first initialize the DummyGrabber with a gaussianSpot as private member\n self.__cam = dataIO(\"DummyGrabber\", imageType=\"gaussianSpot\")\n self.__cam.startDevice()\n\n # define a dataobject for the gaussienSpot image as a public member\n self.dObj = dataObject()\n self.__cam.acquire()\n self.__cam.getVal(self.dObj)\n self.dObj = self.__defineDObj(self.dObj)\n\n # plot index and handle as private member\n self.__plotHandle = None\n self.__plotIndex = None\n\n # define timer to update live image\n self.__liveTimer = timer(50, self.__updateTimerCallBack, name=\"liveTimer\")\n\n return\n\n ###############################################################################\n # Callback function to update the live image.\n\n def __updateTimerCallBack(self):\n \"\"\"Callback function to update the live image.\"\"\"\n # stop live time if plot window is closed\n if self.__plotHandle:\n if not self.__plotHandle.exists():\n self.__liveTimer.stop()\n return\n\n # acquire and get image\n self.__cam.acquire()\n self.__cam.getVal(self.dObj)\n\n # calculate the centroid of the gaussianSpot\n if hasItomAlgo: # new itom.algorithms with auto completion and docstring in pop-up\n [intensityX, intensityY, centroidX, centroidY] = algorithms.centroidXY(self.dObj)\n else: # old itom.filter\n [intensityX, intensityY, centroidX, centroidY] = filter(\"centroidXY\", self.dObj)\n\n print(\n \"Centroid (x, y) position: {:.3f} {}, {:.3f} {}\".format(\n centroidX, self.dObj.axisUnits[1], centroidY, self.dObj.axisUnits[0]\n )\n )\n\n # create the centroid xy marker\n centroidMarker = dataObject([2, 1], \"float32\")\n centroidMarker[0, :] = centroidY\n centroidMarker[1, :] = centroidX\n\n # plot the camera image with marked centroid position\n if not self.__plotHandle: # open figure window by first time\n [self.__plotIndex, self.__plotHandle] = plot2(\n self.dObj, properties={\"keepAspectRatio\": True, \"colorBarVisible\": True, \"colorMap\": \"viridis\"}\n )\n\n else: # replot the figure\n self.__plotHandle.call(\"replot\")\n\n # delete markers and plot the new one\n if self.__plotHandle:\n self.__plotHandle.call(\"deleteMarkers\")\n self.__plotHandle.call(\"plotMarkers\", centroidMarker, \"r+25;2\", \"centroid\", 0)\n\n return\n\n ###############################################################################\n # Function to defines the dataObject by meta information.\n\n def __defineDObj(self, dObj: dataObject) -> dataObject:\n \"\"\"Private function to define the meta information of the image dataObject.\n\n Args:\n image (dataObject): 2D image\n\n Returns:\n dataObject: 2D image with define meta information\n \"\"\"\n # define dataobject\n dObj.setAxisDescription(0, \"y axis\")\n dObj.setAxisDescription(1, \"x axis\")\n dObj.setAxisUnit(0, \"\\xb5m\")\n dObj.setAxisUnit(1, \"\\xb5m\")\n dObj.setAxisScale(0, 10e-3) # pixel pitch of 10 \\xb5m\n dObj.setAxisScale(1, 10e-3) # pixel pitch of 10 \\xb5m\n dObj.valueDescription = \"intensity\"\n dObj.valueUnit = \"counts\"\n\n return dObj\n\n\nif __name__ == \"__main__\":\n gaussianSpotCentroid = GaussianSpotCentroidDetection()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.8.10" } }, "nbformat": 4, "nbformat_minor": 0 }