{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Canny edge detector\n\nThe Canny filter is a multi-stage edge detector. It uses a filter based on the\nderivative of a Gaussian in order to compute the intensity of the gradients.The\nGaussian reduces the effect of noise present in the image. Then, potential\nedges are thinned down to 1-pixel curves by removing non-maximum pixels of the\ngradient magnitude. Finally, edge pixels are kept or removed using hysteresis\nthresholding on the gradient magnitude.\n\nThe Canny has three adjustable parameters: the width of the Gaussian (the\nnoisier the image, the greater the width), and the low and high threshold for\nthe hysteresis thresholding.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import numpy as np\nimport matplotlib.pyplot as plt\nfrom scipy import ndimage as ndi\nfrom skimage.util import random_noise\nfrom skimage import feature\n\n\n# Generate noisy image of a square\nimage = np.zeros((128, 128), dtype=float)\nimage[32:-32, 32:-32] = 1\n\nimage = ndi.rotate(image, 15, mode='constant')\nimage = ndi.gaussian_filter(image, 4)\nimage = random_noise(image, mode='speckle', mean=0.1)\n\n# Compute the Canny filter for two values of sigma\nedges1 = feature.canny(image)\nedges2 = feature.canny(image, sigma=3)\n\n# display results\nfig, ax = plt.subplots(nrows=1, ncols=3, figsize=(8, 3))\n\nax[0].imshow(image, cmap='gray')\nax[0].set_title('noisy image', fontsize=20)\n\nax[1].imshow(edges1, cmap='gray')\nax[1].set_title(r'Canny filter, $\\sigma=1$', fontsize=20)\n\nax[2].imshow(edges2, cmap='gray')\nax[2].set_title(r'Canny filter, $\\sigma=3$', fontsize=20)\n\nfor a in ax:\n    a.axis('off')\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
}