Skip to content
Snippets Groups Projects

Annotation tool 4 0

6 files
+ 351
391
Compare changes
  • Side-by-side
  • Inline

Files

+ 9
111
{
"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,
@@ -15,113 +7,19 @@
"outputs": [],
"source": [
"import qim3d\n",
"from scipy import ndimage\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",
"qim3d.io.logger.level(\"info\")\n",
"\n",
"# Load example image\n",
"img = qim3d.examples.bone_128x128x128\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()"
"#interface.launch()\n",
"interface.launch(ndimage.zoom(img[0], 3, order=0))"
]
}
],
@@ -141,9 +39,9 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.11"
"version": "3.11.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
"nbformat_minor": 4
}
Loading