{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Extrema\n\nWe detect local maxima in a galaxy image. The image is corrupted by noise,\ngenerating many local maxima. To keep only those maxima with sufficient\nlocal contrast, we use h-maxima.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\n\nfrom skimage.measure import label\nfrom skimage import data\nfrom skimage import color\nfrom skimage.morphology import extrema\nfrom skimage import exposure\n\n\ncolor_image = data.hubble_deep_field()\n\n# for illustration purposes, we work on a crop of the image.\nx_0 = 70\ny_0 = 354\nwidth = 100\nheight = 100\n\nimg = color.rgb2gray(color_image)[y_0 : (y_0 + height), x_0 : (x_0 + width)]\n\n# the rescaling is done only for visualization purpose.\n# the algorithms would work identically in an unscaled version of the\n# image. However, the parameter h needs to be adapted to the scale.\nimg = exposure.rescale_intensity(img)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "MAXIMA DETECTION\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Maxima in the galaxy image are detected by mathematical morphology.\n# There is no a priori constraint on the density.\n\n# We find all local maxima\nlocal_maxima = extrema.local_maxima(img)\nlabel_maxima = label(local_maxima)\noverlay = color.label2rgb(\n    label_maxima, img, alpha=0.7, bg_label=0, bg_color=None, colors=[(1, 0, 0)]\n)\n\n# We observed in the previous image, that there are many local maxima\n# that are caused by the noise in the image.\n# For this, we find all local maxima with a height of h.\n# This height is the gray level value by which we need to descent\n# in order to reach a higher maximum and it can be seen as a local\n# contrast measurement.\n# The value of h scales with the dynamic range of the image, i.e.\n# if we multiply the image with a constant, we need to multiply\n# the value of h with the same constant in order to achieve the same result.\nh = 0.05\nh_maxima = extrema.h_maxima(img, h)\nlabel_h_maxima = label(h_maxima)\noverlay_h = color.label2rgb(\n    label_h_maxima, img, alpha=0.7, bg_label=0, bg_color=None, colors=[(1, 0, 0)]\n)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "GRAPHICAL OUTPUT\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# a new figure with 3 subplots\nfig, ax = plt.subplots(1, 3, figsize=(15, 5))\n\nax[0].imshow(img, cmap='gray')\nax[0].set_title('Original image')\nax[0].axis('off')\n\nax[1].imshow(overlay)\nax[1].set_title('Local Maxima')\nax[1].axis('off')\n\nax[2].imshow(overlay_h)\nax[2].set_title(f'h maxima for h = {h:.2f}')\nax[2].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
}