|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "0f90fd53", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "# Machine Vision Toolbox for Python — Interactive Demo\n", |
| 9 | + "\n", |
| 10 | + "This notebook runs entirely in your browser using [JupyterLite](https://jupyterlite.readthedocs.io) and [Pyodide](https://pyodide.org).\n", |
| 11 | + "No installation required. The cell below installs the toolbox the first time it is run (takes ~30 s)." |
| 12 | + ] |
| 13 | + }, |
| 14 | + { |
| 15 | + "cell_type": "code", |
| 16 | + "execution_count": null, |
| 17 | + "id": "c663dd60", |
| 18 | + "metadata": {}, |
| 19 | + "outputs": [], |
| 20 | + "source": [ |
| 21 | + "import sys\n", |
| 22 | + "\n", |
| 23 | + "if sys.platform == \"emscripten\":\n", |
| 24 | + " import micropip\n", |
| 25 | + "\n", |
| 26 | + " # Install all dependencies explicitly.\n", |
| 27 | + " # opencv-python, numpy, scipy and matplotlib are in the Pyodide package\n", |
| 28 | + " # index but must be loaded explicitly; they are NOT auto-loaded.\n", |
| 29 | + " await micropip.install([\n", |
| 30 | + " \"opencv-python\",\n", |
| 31 | + " \"spatialmath-python\",\n", |
| 32 | + " \"pgraph-python\",\n", |
| 33 | + " \"ansitable\",\n", |
| 34 | + " \"mvtb-data\",\n", |
| 35 | + " ])\n", |
| 36 | + "\n", |
| 37 | + " # Install the toolbox without re-resolving compiled deps already loaded above.\n", |
| 38 | + " await micropip.install(\"machinevision-toolbox-python\", deps=False)\n", |
| 39 | + "\n", |
| 40 | + "print(\"Ready.\")" |
| 41 | + ] |
| 42 | + }, |
| 43 | + { |
| 44 | + "cell_type": "markdown", |
| 45 | + "id": "4bd104d5", |
| 46 | + "metadata": {}, |
| 47 | + "source": [ |
| 48 | + "## Images and pixels\n", |
| 49 | + "\n", |
| 50 | + "The core class is `Image`. Let's read one of the bundled images and inspect it." |
| 51 | + ] |
| 52 | + }, |
| 53 | + { |
| 54 | + "cell_type": "code", |
| 55 | + "execution_count": null, |
| 56 | + "id": "4fc8b259", |
| 57 | + "metadata": {}, |
| 58 | + "outputs": [], |
| 59 | + "source": [ |
| 60 | + "import matplotlib\n", |
| 61 | + "matplotlib.use(\"module://matplotlib_pyodide.html5_canvas_backend\")\n", |
| 62 | + "import matplotlib.pyplot as plt\n", |
| 63 | + "\n", |
| 64 | + "from machinevisiontoolbox import Image\n", |
| 65 | + "\n", |
| 66 | + "mona = Image.Read(\"monalisa.png\")\n", |
| 67 | + "print(f\"size: {mona.width} x {mona.height}, dtype: {mona.dtype}, colour: {mona.iscolor}\")" |
| 68 | + ] |
| 69 | + }, |
| 70 | + { |
| 71 | + "cell_type": "markdown", |
| 72 | + "id": "fe77ee73", |
| 73 | + "metadata": {}, |
| 74 | + "source": [ |
| 75 | + "Display the image inline with `disp()`, which uses matplotlib." |
| 76 | + ] |
| 77 | + }, |
| 78 | + { |
| 79 | + "cell_type": "code", |
| 80 | + "execution_count": null, |
| 81 | + "id": "c342305d", |
| 82 | + "metadata": {}, |
| 83 | + "outputs": [], |
| 84 | + "source": [ |
| 85 | + "mona.disp()" |
| 86 | + ] |
| 87 | + }, |
| 88 | + { |
| 89 | + "cell_type": "markdown", |
| 90 | + "id": "43219088", |
| 91 | + "metadata": {}, |
| 92 | + "source": [ |
| 93 | + "## Smoothing\n", |
| 94 | + "\n", |
| 95 | + "Apply a Gaussian blur and display original and result side-by-side." |
| 96 | + ] |
| 97 | + }, |
| 98 | + { |
| 99 | + "cell_type": "code", |
| 100 | + "execution_count": null, |
| 101 | + "id": "964566d0", |
| 102 | + "metadata": {}, |
| 103 | + "outputs": [], |
| 104 | + "source": [ |
| 105 | + "smooth = mona.smooth(sigma=3)\n", |
| 106 | + "\n", |
| 107 | + "fig, axes = plt.subplots(1, 2, figsize=(10, 5))\n", |
| 108 | + "mona.disp(ax=axes[0], title=\"Original\")\n", |
| 109 | + "smooth.disp(ax=axes[1], title=\"Smoothed (sigma=3)\")\n", |
| 110 | + "plt.tight_layout()" |
| 111 | + ] |
| 112 | + }, |
| 113 | + { |
| 114 | + "cell_type": "markdown", |
| 115 | + "id": "88224949", |
| 116 | + "metadata": {}, |
| 117 | + "source": [ |
| 118 | + "## Greyscale and histograms" |
| 119 | + ] |
| 120 | + }, |
| 121 | + { |
| 122 | + "cell_type": "code", |
| 123 | + "execution_count": null, |
| 124 | + "id": "6ebccd0a", |
| 125 | + "metadata": {}, |
| 126 | + "outputs": [], |
| 127 | + "source": [ |
| 128 | + "grey = mona.mono()\n", |
| 129 | + "print(f\"grey: {grey.width} x {grey.height}, planes: {grey.nplanes}\")\n", |
| 130 | + "\n", |
| 131 | + "hist, x = grey.hist()\n", |
| 132 | + "plt.figure()\n", |
| 133 | + "plt.bar(x, hist, width=1, color=\"steelblue\")\n", |
| 134 | + "plt.xlabel(\"Pixel value\")\n", |
| 135 | + "plt.ylabel(\"Count\")\n", |
| 136 | + "plt.title(\"Greyscale histogram\")\n", |
| 137 | + "plt.tight_layout()" |
| 138 | + ] |
| 139 | + }, |
| 140 | + { |
| 141 | + "cell_type": "markdown", |
| 142 | + "id": "60a85be2", |
| 143 | + "metadata": {}, |
| 144 | + "source": [ |
| 145 | + "## Colour planes\n", |
| 146 | + "\n", |
| 147 | + "Access individual colour planes by name — the toolbox tracks the colour order so\n", |
| 148 | + "you never need to worry about BGR vs RGB." |
| 149 | + ] |
| 150 | + }, |
| 151 | + { |
| 152 | + "cell_type": "code", |
| 153 | + "execution_count": null, |
| 154 | + "id": "3af193e5", |
| 155 | + "metadata": {}, |
| 156 | + "outputs": [], |
| 157 | + "source": [ |
| 158 | + "flowers = Image.Read(\"flowers1.png\")\n", |
| 159 | + "print(f\"colour order: {flowers.colororder_str}\")\n", |
| 160 | + "\n", |
| 161 | + "fig, axes = plt.subplots(1, 3, figsize=(12, 4))\n", |
| 162 | + "for ax, plane in zip(axes, [\"R\", \"G\", \"B\"]):\n", |
| 163 | + " flowers.plane(plane).disp(ax=ax, title=plane, colormap=\"gray\")\n", |
| 164 | + "plt.tight_layout()" |
| 165 | + ] |
| 166 | + }, |
| 167 | + { |
| 168 | + "cell_type": "markdown", |
| 169 | + "id": "01cd03d5", |
| 170 | + "metadata": {}, |
| 171 | + "source": [ |
| 172 | + "## Binary blobs\n", |
| 173 | + "\n", |
| 174 | + "Load a binary image and find the blobs." |
| 175 | + ] |
| 176 | + }, |
| 177 | + { |
| 178 | + "cell_type": "code", |
| 179 | + "execution_count": null, |
| 180 | + "id": "fb808a02", |
| 181 | + "metadata": {}, |
| 182 | + "outputs": [], |
| 183 | + "source": [ |
| 184 | + "sharks = Image.Read(\"shark2.png\")\n", |
| 185 | + "blobs = sharks.blobs()\n", |
| 186 | + "print(blobs)\n", |
| 187 | + "\n", |
| 188 | + "fig, ax = plt.subplots()\n", |
| 189 | + "sharks.disp(ax=ax)\n", |
| 190 | + "blobs.plot_box(ax, color=\"g\")\n", |
| 191 | + "blobs.plot_centroid(ax, \"o\", color=\"y\")\n", |
| 192 | + "plt.tight_layout()" |
| 193 | + ] |
| 194 | + }, |
| 195 | + { |
| 196 | + "cell_type": "markdown", |
| 197 | + "id": "829820e5", |
| 198 | + "metadata": {}, |
| 199 | + "source": [ |
| 200 | + "## Camera model\n", |
| 201 | + "\n", |
| 202 | + "Create a central perspective camera and project a 3-D point into the image plane." |
| 203 | + ] |
| 204 | + }, |
| 205 | + { |
| 206 | + "cell_type": "code", |
| 207 | + "execution_count": null, |
| 208 | + "id": "de23946b", |
| 209 | + "metadata": {}, |
| 210 | + "outputs": [], |
| 211 | + "source": [ |
| 212 | + "from machinevisiontoolbox import CentralCamera\n", |
| 213 | + "from spatialmath import SE3\n", |
| 214 | + "\n", |
| 215 | + "cam = CentralCamera(f=0.015, rho=10e-6, imagesize=[1280, 1024],\n", |
| 216 | + " pp=[640, 512], name=\"mycamera\")\n", |
| 217 | + "print(cam)\n", |
| 218 | + "\n", |
| 219 | + "P = [0.3, 0.4, 3.0]\n", |
| 220 | + "p = cam.project_point(P)\n", |
| 221 | + "print(f\"Projected pixel: {p}\")\n", |
| 222 | + "\n", |
| 223 | + "# Shift camera 100 mm to the right\n", |
| 224 | + "p2 = cam.project_point(P, pose=SE3(0.1, 0, 0))\n", |
| 225 | + "print(f\"Projected pixel (camera shifted): {p2}\")" |
| 226 | + ] |
| 227 | + } |
| 228 | + ], |
| 229 | + "metadata": { |
| 230 | + "language_info": { |
| 231 | + "name": "python" |
| 232 | + } |
| 233 | + }, |
| 234 | + "nbformat": 4, |
| 235 | + "nbformat_minor": 5 |
| 236 | +} |
0 commit comments