{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Chan-Vese Segmentation\n\nThe Chan-Vese segmentation algorithm is designed to segment objects without\nclearly defined boundaries. This algorithm is based on level sets that are\nevolved iteratively to minimize an energy, which is defined by\nweighted values corresponding to the sum of differences intensity\nfrom the average value outside the segmented region, the sum of\ndifferences from the average value inside the segmented region, and a\nterm which is dependent on the length of the boundary of the segmented\nregion.\n\nThis algorithm was first proposed by Tony Chan and Luminita Vese, in\na publication entitled \"An Active Contour Model Without Edges\" [1]_. See also\n[2]_, [3]_.\n\nThis implementation of the algorithm is somewhat simplified in the\nsense that the area factor 'nu' described in the original paper is not\nimplemented, and is only suitable for grayscale images.\n\nTypical values for ``lambda1`` and ``lambda2`` are 1. If the 'background' is\nvery different from the segmented object in terms of distribution (for\nexample, a uniform black image with figures of varying intensity), then\nthese values should be different from each other.\n\nTypical values for ``mu`` are between 0 and 1, though higher values can be\nused when dealing with shapes with very ill-defined contours.\n\nThe algorithm also returns a list of values that corresponds to the\nenergy at each iteration. This can be used to adjust the various\nparameters described above.\n\n## References\n.. [1] An Active Contour Model without Edges, Tony Chan and\n       Luminita Vese, Scale-Space Theories in Computer Vision, 1999,\n       :DOI:`10.1007/3-540-48236-9_13`\n.. [2] Chan-Vese Segmentation, Pascal Getreuer, Image Processing On\n       Line, 2 (2012), pp. 214-224,\n       :DOI:`10.5201/ipol.2012.g-cv`\n.. [3] The Chan-Vese Algorithm - Project Report, Rami Cohen, 2011\n       :arXiv:`1107.2782`\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\nfrom skimage import data, img_as_float\nfrom skimage.segmentation import chan_vese\n\nimage = img_as_float(data.camera())\n# Feel free to play around with the parameters to see how they impact the result\ncv = chan_vese(\n    image,\n    mu=0.25,\n    lambda1=1,\n    lambda2=1,\n    tol=1e-3,\n    max_num_iter=200,\n    dt=0.5,\n    init_level_set=\"checkerboard\",\n    extended_output=True,\n)\n\nfig, axes = plt.subplots(2, 2, figsize=(8, 8))\nax = axes.flatten()\n\nax[0].imshow(image, cmap=\"gray\")\nax[0].set_axis_off()\nax[0].set_title(\"Original Image\", fontsize=12)\n\nax[1].imshow(cv[0], cmap=\"gray\")\nax[1].set_axis_off()\ntitle = f'Chan-Vese segmentation - {len(cv[2])} iterations'\nax[1].set_title(title, fontsize=12)\n\nax[2].imshow(cv[1], cmap=\"gray\")\nax[2].set_axis_off()\nax[2].set_title(\"Final Level Set\", fontsize=12)\n\nax[3].plot(cv[2])\nax[3].set_title(\"Evolution of energy over iterations\", fontsize=12)\n\nfig.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
}