Skip to content
Snippets Groups Projects
annotation_tool.ipynb 3.41 KiB
Newer Older
  • Learn to ignore specific revisions
  • {
     "cells": [
      {
       "cell_type": "markdown",
       "metadata": {},
       "source": [
        "# Image annotation tool\n",
        "This notebook shows how the annotation interface can be used to create masks for images"
       ]
      },
      {
       "cell_type": "code",
       "execution_count": null,
       "metadata": {},
       "outputs": [],
       "source": [
        "import qim3d\n",
        "import matplotlib.pyplot as plt\n",
        "import matplotlib as mpl\n",
        "import numpy as np\n",
        "%matplotlib inline"
       ]
      },
      {
       "cell_type": "code",
       "execution_count": null,
       "metadata": {},
       "outputs": [],
       "source": [
        "# Load 2D example image\n",
        "img = qim3d.examples.blobs_256x256\n",
        "\n",
        "# Display image\n",
        "plt.imshow(img)\n",
        "plt.show()"
       ]
      },
      {
       "cell_type": "code",
       "execution_count": null,
       "metadata": {},
       "outputs": [],
       "source": [
        "# Start annotation tool\n",
        "interface = qim3d.gui.annotation_tool.Interface()\n",
        "interface.max_masks = 4\n",
        "\n",
        "# We can directly pass the image we loaded to the interface\n",
        "interface.launch(img=img)"
       ]
      },
      {
       "cell_type": "code",
       "execution_count": null,
       "metadata": {},
       "outputs": [],
       "source": [
        "# When 'prepare mask for download' is pressed once, the mask can be retrieved with the get_result() method\n",
        "mask = interface.get_result()"
       ]
      },
      {
       "cell_type": "markdown",
       "metadata": {},
       "source": [
        "## Check the obtained mask"
       ]
      },
      {
       "cell_type": "code",
       "execution_count": null,
       "metadata": {},
       "outputs": [],
       "source": [
        "print (f\"Original image shape..: {img.shape}\")\n",
        "print (f\"Mask image shape......: {mask.shape}\")\n",
        "print (f\"\\nNumber of masks: {np.max(mask)}\")"
       ]
      },
      {
       "cell_type": "markdown",
       "metadata": {},
       "source": [
        "## Show the masked regions"
       ]
      },
      {
       "cell_type": "code",
       "execution_count": null,
       "metadata": {},
       "outputs": [],
       "source": [
        "%matplotlib inline\n",
        "\n",
        "nmasks = np.max(mask)\n",
        "fig, axs = plt.subplots(nrows=1, ncols=nmasks+2, figsize=(12,3))\n",
        "\n",
        "# Show original image\n",
        "axs[0].imshow(img)\n",
        "axs[0].set_title(\"Original\")\n",
        "axs[0].axis('off')\n",
        "\n",
        "\n",
        "# Show masks\n",
        "cmap = mpl.colormaps[\"rainbow\"].copy()\n",
        "cmap.set_under(color='black') # Sets the background to black\n",
        "axs[1].imshow(mask, interpolation='none', cmap=cmap, vmin=1, vmax=nmasks+1)\n",
        "axs[1].set_title(\"Masks\")\n",
        "axs[1].axis('off')\n",
        "\n",
        "# Show masked regions\n",
        "for idx in np.arange(2, nmasks+2):\n",
        "    mask_id = idx-1\n",
        "    submask = mask.copy()\n",
        "    submask[submask != mask_id] = 0\n",
        "    \n",
        "    masked_img = img.copy()\n",
        "    masked_img[submask==0] = 0\n",
        "    axs[idx].imshow(masked_img)\n",
        "    axs[idx].set_title(f\"Mask {mask_id}\")\n",
        "\n",
        "    axs[idx].axis('off')\n",
        "\n",
        "plt.show()"
       ]
      }
     ],
     "metadata": {
      "kernelspec": {
       "display_name": "Python 3 (ipykernel)",
       "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.10.11"
      }
     },
     "nbformat": 4,
     "nbformat_minor": 2
    }