{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Apply maskSLIC vs SLIC\n\nThis example is about comparing the segmentations obtained using the\nplain SLIC method [1]_ and its masked version maskSLIC [2]_.\n\nTo illustrate these segmentation methods, we use an image of biological tissue\nwith immunohistochemical (IHC) staining. The same biomedical image is used in\nthe example on how to\n`sphx_glr_auto_examples_color_exposure_plot_ihc_color_separation.py`.\n\nThe maskSLIC method is an extension of the SLIC method for the\ngeneration of superpixels in a region of interest. maskSLIC is able to\novercome border problems that affects SLIC method, particularely in\ncase of irregular mask.\n\n.. [1] Radhakrishna Achanta, Appu Shaji, Kevin Smith, Aurelien Lucchi,\n    Pascal Fua, and Sabine S\u00fcsstrunk, \"SLIC Superpixels Compared to\n    State-of-the-Art Superpixel Methods,\" IEEE TPAMI, 2012,\n    :DOI:`10.1109/TPAMI.2012.120`\n\n.. [2] Irving, Benjamin. \"maskSLIC: regional superpixel generation\n    with application to local pathology characterisation in medical\n    images,\" 2016, :arXiv:`1606.09518`\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\n\nfrom skimage import data\nfrom skimage import color\nfrom skimage import morphology\nfrom skimage import segmentation\n\n# Input data\nimg = data.immunohistochemistry()\n\n# Compute a mask\nlum = color.rgb2gray(img)\nmask = lum < 0.7\nmask = morphology.remove_small_objects(mask, max_size=499)\nmask = morphology.remove_small_holes(mask, max_size=499)\nmask = morphology.opening(mask, morphology.disk(3))\n\n# SLIC result\nslic = segmentation.slic(img, n_segments=200, start_label=1)\n\n# maskSLIC result\nm_slic = segmentation.slic(img, n_segments=100, mask=mask, start_label=1)\n\n# Display result\nfig, ax_arr = plt.subplots(2, 2, sharex=True, sharey=True, figsize=(10, 10))\nax1, ax2, ax3, ax4 = ax_arr.ravel()\n\nax1.imshow(img)\nax1.set_title('Original image')\n\nax2.imshow(mask, cmap='gray')\nax2.set_title('Mask')\n\nax3.imshow(segmentation.mark_boundaries(img, slic))\nax3.contour(mask, colors='red', linewidths=1)\nax3.set_title('SLIC')\n\nax4.imshow(segmentation.mark_boundaries(img, m_slic))\nax4.contour(mask, colors='red', linewidths=1)\nax4.set_title('maskSLIC')\n\nfor ax in ax_arr.ravel():\n    ax.set_axis_off()\n\nplt.tight_layout()\nplt.show()"
      ]
    }
  ],
  "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.12.12"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}