{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Filling holes and finding peaks\n\nWe fill holes (i.e. isolated, dark spots) in an image using morphological\nreconstruction by erosion. Erosion expands the minimal values of the seed image\nuntil it encounters a mask image. Thus, the seed image and mask image represent\nthe maximum and minimum possible values of the reconstructed image.\n\nWe start with an image containing both peaks and holes:\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\n\nfrom skimage import data\nfrom skimage.exposure import rescale_intensity\n\nimage = data.moon()\n# Rescale image intensity so that we can see dim features.\nimage = rescale_intensity(image, in_range=(50, 200))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Now we need to create the seed image, where the minima represent the\nstarting points for erosion.  To fill holes, we initialize the seed image\nto the maximum value of the original image. Along the borders, however, we\nuse the original values of the image. These border pixels will be the\nstarting points for the erosion process. We then limit the erosion by\nsetting the mask to the values of the original image.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import numpy as np\nfrom skimage.morphology import reconstruction\n\nseed = np.copy(image)\nseed[1:-1, 1:-1] = image.max()\nmask = image\n\nfilled = reconstruction(seed, mask, method='erosion')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "As shown above, eroding inward from the edges removes holes, since (by\ndefinition) holes are surrounded by pixels of brighter value. Finally, we\ncan isolate the dark regions by subtracting the reconstructed image from\nthe original image.\n\nAlternatively, we can find bright spots in an image using morphological\nreconstruction by dilation. Dilation is the inverse of erosion and expands\nthe *maximal* values of the seed image until it encounters a mask image.\nSince this is an inverse operation, we initialize the seed image to the\nminimum image intensity instead of the maximum. The remainder of the\nprocess is the same.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "seed = np.copy(image)\nseed[1:-1, 1:-1] = image.min()\nrec = reconstruction(seed, mask, method='dilation')\n\nfig, ax = plt.subplots(2, 2, figsize=(5, 4), sharex=True, sharey=True)\nax = ax.ravel()\n\nax[0].imshow(image, cmap='gray')\nax[0].set_title('Original image')\nax[0].axis('off')\n\nax[1].imshow(filled, cmap='gray')\nax[1].set_title('after filling holes')\nax[1].axis('off')\n\nax[2].imshow(image - filled, cmap='gray')\nax[2].set_title('holes')\nax[2].axis('off')\n\nax[3].imshow(image - rec, cmap='gray')\nax[3].set_title('peaks')\nax[3].axis('off')\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
}