{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Shapes\n\nThis demo shows how you can add different shapes in the ``itom`` plot.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from itom import shape\nfrom itom import region\nfrom itom import dataObject\nfrom itom import plot" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create unrotated ``shapes``.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "point = shape(shape.Point, (0, 0))\nprint(point)\nline = shape(shape.Line, (0, 0), (100, 50))\nprint(line)\nrect = shape(shape.Rectangle, (20, 20), (70, 100))\nprint(rect)\ninnerRect = shape(shape.Rectangle, (25, 25), (60, 90))\nprint(innerRect)\nsquare = shape(shape.Square, (30, -50), 20)\nprint(square)\nellipse = shape(shape.Ellipse, (-50, -70), (-20, 0))\nprint(ellipse)\ncircle = shape(shape.Circle, (-30, 100), 40)\nprint(circle)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create a pixel-based ``region`` from all ``shapes`` (union of all ``shapes``).\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "area = region()\narea += point.region()\narea += line.region()\narea += rect.region()\narea -= innerRect.region()\narea += square.region()\narea += ellipse.region()\narea += circle.region()\nmask = area.createMask(region(-100, -100, 300, 300, region.RECTANGLE))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create the mask for a ``dataObject`` based on the ``shapes``.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "image = dataObject.zeros([400, 400], \"uint8\")\nimage.axisOffsets = (100, 100)\nmask = image.createMask(\n [point, line, rect, square, ellipse, circle]\n) & image.createMask([innerRect], True)\n\n# plot all contours inside of plot\narea = region()\ncontour_point = point.contour()\ncontour_line = line.contour()\ncontour_rect = rect.contour()\ncontour_square = square.contour()\ncontour_ellipse = ellipse.contour(tol=0.02)\ncontour_circle = circle.contour(tol=0.01)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Plot mask and contour points\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "[i, h] = plot(mask, \"2D\", properties={\"colorMap\": \"falseColorIR\"})\nh.call(\"plotMarkers\", contour_point, \"b+\")\nh.call(\"plotMarkers\", contour_line, \"rs\")\nh.call(\"plotMarkers\", contour_rect, \"g.\")\nh.call(\"plotMarkers\", contour_square, \"cd\")\nh.call(\"plotMarkers\", contour_ellipse, \"y>\")\nh.call(\"plotMarkers\", contour_circle, \"kx\")\n\n# plot shapes into plot\n# move them first by 50px each\nfor s in [point, line, rect, square, innerRect, ellipse, circle]:\n s.center = [s.center[0] + 50, s.center[1]]\n\n# don't allow the rectangle to be moved\nrect.flags = shape.MoveLock\n\n# don't allow the line to be resized\nline.flags = shape.ResizeLock\n\n# don't allow the line to be resized, moved and rotated\nline.flags = shape.MoveLock | shape.ResizeLock | shape.RotateLock\n\nh[\"geometricShapes\"] = [\n point,\n line,\n rect,\n innerRect,\n square,\n ellipse,\n circle,\n]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\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 }