{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Image Segmentation" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%matplotlib ipympl\n", "import matplotlib.cbook as cbook\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", "\n", "from mpl_interactions import image_segmenter" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# load a sample image\n", "image = plt.imread(\"https://github.com/matplotlib/matplotlib/raw/v3.3.0/lib/matplotlib/mpl-data/sample_data/ada.png\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Single class\n", "\n", "Click and drag to select regions. You can also scroll to zoom and middle click to pan.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "gif": "segment.gif" }, "outputs": [], "source": [ "segmenter = image_segmenter(image, mask_colors=\"red\", mask_alpha=0.76, figsize=(7, 7))\n", "display(segmenter)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "segmenter.erasing = True" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Erasing\n", "\n", "To start erasing instead of selecting more simply set `segmenter.erasing = True`. Run the following cell then try to lasso an area you have already selected" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Looking at the mask.\n", "\n", "The mask is transparently accessible via `segmenter.mask`. `0` is used as the value of unselected regions." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "segmenter.mask" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.figure()\n", "plt.imshow(segmenter.mask)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Multiple classes\n", "\n", "To use multiple classes set nclasses to be larger than 1. By default the tab10 color set will be used, and if that is not enough the xkcd color named colors will be used. Or you can pass a list of colors." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "multi_class_segmenter = image_segmenter(image, nclasses=3, mask_alpha=0.76)\n", "display(multi_class_segmenter)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Change the class by changing the `current_class` variable\n", "\n", "run the below cell and then try selecting again" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "multi_class_segmenter.current_class = 3" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.figure()\n", "plt.imshow(multi_class_segmenter.mask)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### pre-seed a mask\n", "\n", "You can also load an existing mask. You will need to ensure that it does not have values greater than `nclasses` and that it has the same shape as the image. There are currently no safegaurds for this and when there are exceptions in a matplotlib callback they can be hard to see in the notebook - so be careful!\n", "\n", "\n", "If you run the below cell the image should show up with the premade mask already applied!" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "gif": "segment-preload-mask.png" }, "outputs": [], "source": [ "mask = np.load(\"ada-mask.npy\")\n", "preloaded = image_segmenter(image, nclasses=3, mask=mask)\n", "display(preloaded)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Styling\n", "\n", "\n", "### imshow parameters\n", "\n", "You can modify the display of the image using any kwargs accepted the [imshow](https://matplotlib.org/api/_as_gen/matplotlib.pyplot.imshow.html) command. For example if we convert our example image to gray-scale then we can choose the colormap with the `cmap` argument." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from ipywidgets import HBox\n", "\n", "grayscale_image = image.mean(axis=-1)\n", "gray = image_segmenter(grayscale_image, nclasses=3, mask=mask, figsize=(5, 5), cmap=\"gray\")\n", "display(gray)\n", "plasma = image_segmenter(grayscale_image, nclasses=3, mask=mask, figsize=(5, 5), cmap=\"plasma\")\n", "display(plasma)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## LassoSelector line\n", "\n", "You can change the appearance of the LassoSelector line using the `lineprops` kwarg. So to make the line very thick and red:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "lineprops = {\"color\":\"red\", \"linewidth\":10}\n", "gray = image_segmenter(grayscale_image, nclasses=3, mask=mask, figsize=(5, 5), cmap=\"gray\",lineprops = lineprops)\n", "display(gray)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.5" } }, "nbformat": 4, "nbformat_minor": 4 }