{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Active Contour Model\n\nThe active contour model is a method to fit open or closed splines to lines or\nedges in an image [1]_. It works by minimising an energy that is in part\ndefined by the image and part by the spline's shape: length and smoothness. The\nminimization is done implicitly in the shape energy and explicitly in the\nimage energy.\n\nIn the following two examples the active contour model is used (1) to segment\nthe face of a person from the rest of an image by fitting a closed curve\nto the edges of the face and (2) to find the darkest curve between two fixed\npoints while obeying smoothness considerations. Typically it is a good idea to\nsmooth images a bit before analyzing, as done in the following examples.\n\nWe initialize a circle around the astronaut's face and use the default boundary\ncondition ``boundary_condition='periodic'`` to fit a closed curve. The default\nparameters ``w_line=0, w_edge=1`` will make the curve search towards edges,\nsuch as the boundaries of the face.\n\n.. [1] *Snakes: Active contour models*. Kass, M.; Witkin, A.; Terzopoulos, D.\n       International Journal of Computer Vision 1 (4): 321 (1988).\n       :DOI:`10.1007/BF00133570`\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import numpy as np\nimport matplotlib.pyplot as plt\nfrom skimage.color import rgb2gray\nfrom skimage import data\nfrom skimage.filters import gaussian\nfrom skimage.segmentation import active_contour\n\n\nimg = data.astronaut()\nimg = rgb2gray(img)\n\ns = np.linspace(0, 2 * np.pi, 400)\nr = 100 + 100 * np.sin(s)\nc = 220 + 100 * np.cos(s)\ninit = np.array([r, c]).T\n\nsnake = active_contour(\n    gaussian(img, sigma=3, preserve_range=False),\n    init,\n    alpha=0.015,\n    beta=10,\n    gamma=0.001,\n)\n\nfig, ax = plt.subplots(figsize=(7, 7))\nax.imshow(img, cmap=plt.cm.gray)\nax.plot(init[:, 1], init[:, 0], '--r', lw=3)\nax.plot(snake[:, 1], snake[:, 0], '-b', lw=3)\nax.set_xticks([]), ax.set_yticks([])\nax.axis([0, img.shape[1], img.shape[0], 0])\n\nplt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Here we initialize a straight line between two points, `(5, 136)` and\n`(424, 50)`, and require that the spline has its end points there by giving\nthe boundary condition `boundary_condition='fixed'`. We furthermore\nmake the algorithm search for dark lines by giving a negative `w_line` value.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "img = data.text()\n\nr = np.linspace(136, 50, 100)\nc = np.linspace(5, 424, 100)\ninit = np.array([r, c]).T\n\nsnake = active_contour(\n    gaussian(img, sigma=1, preserve_range=False),\n    init,\n    boundary_condition='fixed',\n    alpha=0.1,\n    beta=1.0,\n    w_line=-5,\n    w_edge=0,\n    gamma=0.1,\n)\n\nfig, ax = plt.subplots(figsize=(9, 5))\nax.imshow(img, cmap=plt.cm.gray)\nax.plot(init[:, 1], init[:, 0], '--r', lw=3)\nax.plot(snake[:, 1], snake[:, 0], '-b', lw=3)\nax.set_xticks([]), ax.set_yticks([])\nax.axis([0, img.shape[1], img.shape[0], 0])\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
}