.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "11_demos\itom\basics\demo_GaussianSpotCentroidDetection.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_11_demos_itom_basics_demo_GaussianSpotCentroidDetection.py: Gaussian spot centroid detection ================================= This demo shows how the ``itom.algorithms`` can be used. In this example a camera ``DummyGrabber`` is used to acquire a moving gaussian spot. For the centroid detection the ``itom.algorithms.centroidxy`` function is used. Finally, this examples shows how a centroid position marker is added to the gaussian spot image. .. note:: For older ``itom`` versions the ``itom.filter`` function is used for the centroidXY algorithm. By using the itom.algorithms you will get the algorithms information by a pop-up during typing. .. GENERATED FROM PYTHON SOURCE LINES 13-32 .. code-block:: Python from itom import dataIO from itom import dataObject from itom import plot2 from itom import timer try: from itom import algorithms hasItomAlgo = True except ImportError: from itom import filter hasItomAlgo = False print( "The itom.algorithms module cannot be used because" "of an older itom version. Use the itom.filter functions." ) .. GENERATED FROM PYTHON SOURCE LINES 34-35 Constructor of the live gaussianSpot centroid detection. .. GENERATED FROM PYTHON SOURCE LINES 35-148 .. code-block:: Python class GaussianSpotCentroidDetection: def __init__(self): """Constructor function of the class.""" # first initialize the DummyGrabber with a gaussianSpot as private member self.__cam = dataIO("DummyGrabber", imageType="gaussianSpot") self.__cam.startDevice() # define a dataobject for the gaussienSpot image as a public member self.dObj = dataObject() self.__cam.acquire() self.__cam.getVal(self.dObj) self.dObj = self.__defineDObj(self.dObj) # plot index and handle as private member self.__plotHandle = None self.__plotIndex = None # define timer to update live image self.__liveTimer = timer(50, self.__updateTimerCallBack, name="liveTimer") return ############################################################################### # Callback function to update the live image. def __updateTimerCallBack(self): """Callback function to update the live image.""" # stop live time if plot window is closed if self.__plotHandle: if not self.__plotHandle.exists(): self.__liveTimer.stop() return # acquire and get image self.__cam.acquire() self.__cam.getVal(self.dObj) # calculate the centroid of the gaussianSpot if ( hasItomAlgo ): # new itom.algorithms with auto completion and docstring in pop-up [intensityX, intensityY, centroidX, centroidY] = algorithms.centroidXY( self.dObj ) else: # old itom.filter [intensityX, intensityY, centroidX, centroidY] = filter( "centroidXY", self.dObj ) print( "Centroid (x, y) position: {:.3f} {}, {:.3f} {}".format( centroidX, self.dObj.axisUnits[1], centroidY, self.dObj.axisUnits[0] ) ) # create the centroid xy marker centroidMarker = dataObject([2, 1], "float32") centroidMarker[0, :] = centroidY centroidMarker[1, :] = centroidX # plot the camera image with marked centroid position if not self.__plotHandle: # open figure window by first time [self.__plotIndex, self.__plotHandle] = plot2( self.dObj, properties={ "keepAspectRatio": True, "colorBarVisible": True, "colorMap": "viridis", }, ) else: # replot the figure self.__plotHandle.call("replot") # delete markers and plot the new one if self.__plotHandle: self.__plotHandle.call("deleteMarkers") self.__plotHandle.call( "plotMarkers", centroidMarker, "r+25;2", "centroid", 0 ) return ############################################################################### # Function to defines the dataObject by meta information. def __defineDObj(self, dObj: dataObject) -> dataObject: """Private function to define the meta information of the image dataObject. Args: image (dataObject): 2D image Returns: dataObject: 2D image with define meta information """ # define dataobject dObj.setAxisDescription(0, "y axis") dObj.setAxisDescription(1, "x axis") dObj.setAxisUnit(0, "\xb5m") dObj.setAxisUnit(1, "\xb5m") dObj.setAxisScale(0, 10e-3) # pixel pitch of 10 \xb5m dObj.setAxisScale(1, 10e-3) # pixel pitch of 10 \xb5m dObj.valueDescription = "intensity" dObj.valueUnit = "counts" return dObj if __name__ == "__main__": gaussianSpotCentroid = GaussianSpotCentroidDetection() .. GENERATED FROM PYTHON SOURCE LINES 149-151 .. image:: ../../_static/demoGaussianSpotCentroidDetection_1.png :width: 100% .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.010 seconds) .. _sphx_glr_download_11_demos_itom_basics_demo_GaussianSpotCentroidDetection.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: demo_GaussianSpotCentroidDetection.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: demo_GaussianSpotCentroidDetection.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: demo_GaussianSpotCentroidDetection.zip `