{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Mask editor\n\nThis demo shows a mask editor where a masked ``dataObject`` can be\ncreated from shapes that are drawn on the plot canvas. The result\nof such shapes is a list of itom.shape objects. These are sub-pixel\nprecise geometric shapes. They can then be converted to pixel-precise\nmasks. For more information about shapes see the shapes demo in the main\nfolder of the demo scripts.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from itom import ui\nfrom itom import dataObject\nfrom itom import algorithms\nfrom itom import shape" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Controls if the user can add or modify shapes by the actions in the toolbar\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def checkEnableDrawingClicked(checked: bool):\n gui.plot[\"geometricShapesDrawingEnabled\"] = checked" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Clear all existing shapes by calling the slot ``clearGeometricShapes`` \n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def clearShapes():\n gui.plot.call(\"clearGeometricShapes\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Creates a mask where all values within the mask are set to ``255`` and\nall other values to ``0``. The mask has the same size and axes information\nthan the displayed ``dataObject``.\nThe mask object is then set to the variable ``mask`` in the global workspace\nand displayed in a new plot window.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def exportMask():\n mask = gui.plot[\"source\"].createMask(gui.plot[\"geometricShapes\"])\n globals()[\"mask\"] = mask\n plot(mask, properties={\"title\": \"exported mask\"})" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Exports all current shapes as a list of itom.shape objects.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def exportShapes():\n globals()[\"shapes\"] = gui.plot[\"geometricShapes\"]\n ui.msgInformation(\n \"shapes exported\",\n \"shapes exported to the workspace under the variable 'shapes': \\n\"\n + str(globals()[\"shapes\"]),\n )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Show the mask object as overlay image. Use the slider in the toolbox\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# of the plot to change the transparency of the overlay image.\ndef showMaskAsOverlay():\n mask = gui.plot[\"source\"].createMask(gui.plot[\"geometricShapes\"])\n gui.plot[\"overlayImage\"] = mask" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Sets all values within any shapes to a given gray value.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def setColorUnderMask():\n [val, ok] = ui.getInt(\n \"value\", \"set the value for all values within the mask:\", 128, 0, 255\n )\n if ok:\n mask = gui.plot[\"source\"].createMask(gui.plot[\"geometricShapes\"])\n # the mask can be inverted using ~mask\n gui.plot[\"source\"][\n mask\n ] = val # this single command can be used to change values in the mask\n gui.plot.call(\n \"replot\"\n ) # if only the source object is changed, you need to call\n # ``replot`` such that the plot is updated, too." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This method is always called if any shape is added or modified and displays some\ninformation in the status bar of the window.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def shapeModified(index: int, shape: shape):\n gui.call(\"statusBar\").call(\n \"showMessage\", \"Shape %i modified: %s\" % (index, str(shape)), 1000\n )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This method is called if the user changes the selection of allowed operations.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def listModificationChanged():\n sel = gui.listModificationTypes.call(\"selectedRows\")\n sel2 = []\n if 0 in sel:\n sel2.append(\"Move\")\n if 1 in sel:\n sel2.append(\"Resize\")\n gui.plot[\"geometryModificationModes\"] = \";\".join(sel2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create demo data with axis scales and offsets to\nshow that the mask will also work in this special case.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "image = dataObject.randN([1024, 1024])\nimage.axisScales = (1e-3, 1e-3)\nimage.axisOffsets = (512, 512)\nimage.axisUnits = (\"mm\", \"mm\")\nimage.axisDescriptions = (\"y\", \"x\")\nimage.valueUnit = \"a.u.\"\nimage.valueDescription = \"intensity\"\nalgorithms.lowPassFilter(image, image, 7, 7)\n\ngui = ui(\"demo_MaskEditor.ui\", ui.TYPEWINDOW)\n# connect signal-slots\ngui.checkEnableDrawing.connect(\"toggled(bool)\", checkEnableDrawingClicked)\ngui.btnExportMask.connect(\"clicked()\", exportMask)\ngui.btnExportShape.connect(\"clicked()\", exportShapes)\ngui.btnShowMaskOverlay.connect(\"clicked()\", showMaskAsOverlay)\ngui.btnSetColorUnderMask.connect(\"clicked()\", setColorUnderMask)\ngui.btnClearShapes.connect(\"clicked()\", clearShapes)\ngui.plot.connect(\"geometricShapeChanged(int,ito::Shape)\", shapeModified)\ngui.listModificationTypes.connect(\n \"itemSelectionChanged()\", listModificationChanged\n)\n\ngui.plot[\"source\"] = image\ngui.plot[\"colorMap\"] = \"hotIron\"\ngui.listModificationTypes.call(\"selectRows\", (0, 1))\ngui.show()" ] }, { "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 }