{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Cameras and images\n\nThis demo shows with the example of the ``DummyGrabber``\nhow you acquire an image and apply some filters.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from itom import dataIO\nfrom itom import dataObject\nfrom itom import algorithms\nfrom itom import liveImage\nfrom itom import saveIDC\nfrom itom import loadIDC\nfrom itom import plot" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Initialize a ``DummyGrabber`` camera\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "cam = dataIO(\"DummyGrabber\")\ncam.setParam(\"bpp\", 8)\n# start camera (only once)\ncam.startDevice()\n\n# show live image of camera\nliveImage(cam)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ ".. hint:: The live images tries to acquire and get up to 50 images per second\n from the camera. If you want to acquire images by yourself in a script, you need\n to stop the timer of the live images for a certain amount of time. After you are\n done with your manual acquisition, you can restart the timer again.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "currentStatus = cam.getAutoGrabbing()\nprint(\"Current value of auto grabbing property of the camera:\", currentStatus)\n\ncam.setAutoGrabbing(False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Acquire 10 images in a list of dataObjects\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "result = []\nd = dataObject() # empty data object where the image should be put in\n\nfor i in range(0, 10):\n cam.acquire()\n cam.getVal(d) # d is a shallow copy of the camera image\n result.append(d.copy())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Save the list of images to the **image1.idc** file (idc is a file format for the python pickle module)\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "saveIDC(\"image1.idc\", {\"result\": result, \"description\": \"sample 1\"})" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Load the list of images\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "loaded_objects = loadIDC(\"image1.idc\")\nresult2 = loaded_objects[\"result\"]\n\n# plot the 3rd image from the list\nplot(result2[2])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Acquire 10 images in an image stack\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "num = 10\nsizex = cam.getParam(\"sizex\")\nsizey = cam.getParam(\"sizey\")\nbpp = cam.getParam(\"bpp\")\n\nif bpp == 8:\n d = dataObject([num, sizey, sizex], \"uint8\")\nelse:\n d = dataObject([num, sizey, sizex], \"uint16\")\n\nfor idx in range(num):\n cam.acquire()\n cam.copyVal(d[idx, :, :]) # partial deep copy into one part of the 3d object d\n\nplot(d)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Calculate mean value of image stack in z-direction.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "result_mean = dataObject()\n\nalgorithms.calcMeanZ(d, result_mean, ignoreInf=0, calcStd=0)\n# result_mean is a 3d Object with [1 x sizey x sizex] dimensions.\n# We squeeze it to get a 2D Object\nresult_mean = result_mean.squeeze()\n\nresult_mean.setTag(\"title\", \"mean value of {} acquisitions\".format(num))\nresult_mean.axisUnits = (\"px\", \"px\")\nresult_mean.axisDescriptions = (\"y\", \"x\")\nplot(result_mean)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Apply Gaussian filter onto the mean image.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "result_filter = dataObject()\nkernelVal = 9\nalgorithms.gaussianFilter(result_mean, result_filter, kernelx=kernelVal, kernely=kernelVal)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Copy meta information from source ``dataObject``.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "result_filter.copyMetaInfo(result_mean)\nresult_filter.setTag(\"title\", \"Gaussian filter with kernel {}\".format(kernelVal))\nplot(result_filter)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# reset the auto grabbing functionality of possibly connected live images\ncam.setAutoGrabbing(currentStatus)\n\n# end camera\ncam.stopDevice()" ] } ], "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 }