{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Finding local maxima\n\nThe ``peak_local_max`` function returns the coordinates of local peaks (maxima)\nin an image. Internally, a maximum filter is used for finding local maxima. This\noperation dilates the original image and merges neighboring local maxima closer\nthan the size of the dilation. Locations where the original image is equal to the\ndilated image are returned as local maxima.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from scipy import ndimage as ndi\nimport matplotlib.pyplot as plt\nfrom skimage.feature import peak_local_max\nfrom skimage import data, img_as_float\n\nim = img_as_float(data.coins())\n\n# image_max is the dilation of im with a 20*20 structuring element\n# It is used within peak_local_max function\nimage_max = ndi.maximum_filter(im, size=20, mode='constant')\n\n# Comparison between image_max and im to find the coordinates of local maxima\ncoordinates = peak_local_max(im, min_distance=20)\n\n# display results\nfig, axes = plt.subplots(1, 3, figsize=(8, 3), sharex=True, sharey=True)\nax = axes.ravel()\nax[0].imshow(im, cmap=plt.cm.gray)\nax[0].axis('off')\nax[0].set_title('Original')\n\nax[1].imshow(image_max, cmap=plt.cm.gray)\nax[1].axis('off')\nax[1].set_title('Maximum filter')\n\nax[2].imshow(im, cmap=plt.cm.gray)\nax[2].autoscale(False)\nax[2].plot(coordinates[:, 1], coordinates[:, 0], 'r.')\nax[2].axis('off')\nax[2].set_title('Peak local max')\n\nfig.tight_layout()\n\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
}