{ "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "code", "language": "python", "outputs": [], "collapsed": false, "input": [ "%matplotlib inline" ], "metadata": {} }, { "source": "
\n

# Canny edge detector

\n

The 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

The 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
\n", "cell_type": "markdown", "metadata": {} }, { "cell_type": "code", "language": "python", "outputs": [], "collapsed": false, "input": "import numpy as np\nimport matplotlib.pyplot as plt\nfrom scipy import ndimage\n\nfrom skimage import filter\n\n\n# Generate noisy image of a square\nim = np.zeros((128, 128))\nim[32:-32, 32:-32] = 1\n\nim = ndimage.rotate(im, 15, mode='constant')\nim = ndimage.gaussian_filter(im, 4)\nim += 0.2 * np.random.random(im.shape)\n\n# Compute the Canny filter for two values of sigma\nedges1 = filter.canny(im)\nedges2 = filter.canny(im, sigma=3)\n\n# display results\nfig, (ax1, ax2, ax3) = plt.subplots(nrows=1, ncols=3, figsize=(8, 3))\n\nax1.imshow(im, cmap=plt.cm.jet)\nax1.axis('off')\nax1.set_title('noisy image', fontsize=20)\n\nax2.imshow(edges1, cmap=plt.cm.gray)\nax2.axis('off')\nax2.set_title('Canny filter, $\\sigma=1$', fontsize=20)\n\nax3.imshow(edges2, cmap=plt.cm.gray)\nax3.axis('off')\nax3.set_title('Canny filter, $\\sigma=3$', fontsize=20)\n\nfig.subplots_adjust(wspace=0.02, hspace=0.02, top=0.9,\n bottom=0.02, left=0.02, right=0.98)\n\nplt.show()", "metadata": {} } ], "metadata": {} } ], "metadata": { "name": "" } }