{"worksheets": [{"cells": [{"cell_type": "markdown", "metadata": {}, "source": ["Edge Detection", "==============", "", "*Important:* Please read the [installation page](http://gpeyre.github.io/numerical-tours/installation_python/) for details about how to install the toolboxes.", "$\\newcommand{\\dotp}[2]{\\langle #1, #2 \\rangle}$", "$\\newcommand{\\enscond}[2]{\\lbrace #1, #2 \\rbrace}$", "$\\newcommand{\\pd}[2]{ \\frac{ \\partial #1}{\\partial #2} }$", "$\\newcommand{\\umin}[1]{\\underset{#1}{\\min}\\;}$", "$\\newcommand{\\umax}[1]{\\underset{#1}{\\max}\\;}$", "$\\newcommand{\\umin}[1]{\\underset{#1}{\\min}\\;}$", "$\\newcommand{\\uargmin}[1]{\\underset{#1}{argmin}\\;}$", "$\\newcommand{\\norm}[1]{\\|#1\\|}$", "$\\newcommand{\\abs}[1]{\\left|#1\\right|}$", "$\\newcommand{\\choice}[1]{ \\left\\{ \\begin{array}{l} #1 \\end{array} \\right. }$", "$\\newcommand{\\pa}[1]{\\left(#1\\right)}$", "$\\newcommand{\\diag}[1]{{diag}\\left( #1 \\right)}$", "$\\newcommand{\\qandq}{\\quad\\text{and}\\quad}$", "$\\newcommand{\\qwhereq}{\\quad\\text{where}\\quad}$", "$\\newcommand{\\qifq}{ \\quad \\text{if} \\quad }$", "$\\newcommand{\\qarrq}{ \\quad \\Longrightarrow \\quad }$", "$\\newcommand{\\ZZ}{\\mathbb{Z}}$", "$\\newcommand{\\CC}{\\mathbb{C}}$", "$\\newcommand{\\RR}{\\mathbb{R}}$", "$\\newcommand{\\EE}{\\mathbb{E}}$", "$\\newcommand{\\Zz}{\\mathcal{Z}}$", "$\\newcommand{\\Ww}{\\mathcal{W}}$", "$\\newcommand{\\Vv}{\\mathcal{V}}$", "$\\newcommand{\\Nn}{\\mathcal{N}}$", "$\\newcommand{\\NN}{\\mathcal{N}}$", "$\\newcommand{\\Hh}{\\mathcal{H}}$", "$\\newcommand{\\Bb}{\\mathcal{B}}$", "$\\newcommand{\\Ee}{\\mathcal{E}}$", "$\\newcommand{\\Cc}{\\mathcal{C}}$", "$\\newcommand{\\Gg}{\\mathcal{G}}$", "$\\newcommand{\\Ss}{\\mathcal{S}}$", "$\\newcommand{\\Pp}{\\mathcal{P}}$", "$\\newcommand{\\Ff}{\\mathcal{F}}$", "$\\newcommand{\\Xx}{\\mathcal{X}}$", "$\\newcommand{\\Mm}{\\mathcal{M}}$", "$\\newcommand{\\Ii}{\\mathcal{I}}$", "$\\newcommand{\\Dd}{\\mathcal{D}}$", "$\\newcommand{\\Ll}{\\mathcal{L}}$", "$\\newcommand{\\Tt}{\\mathcal{T}}$", "$\\newcommand{\\si}{\\sigma}$", "$\\newcommand{\\al}{\\alpha}$", "$\\newcommand{\\la}{\\lambda}$", "$\\newcommand{\\ga}{\\gamma}$", "$\\newcommand{\\Ga}{\\Gamma}$", "$\\newcommand{\\La}{\\Lambda}$", "$\\newcommand{\\si}{\\sigma}$", "$\\newcommand{\\Si}{\\Sigma}$", "$\\newcommand{\\be}{\\beta}$", "$\\newcommand{\\de}{\\delta}$", "$\\newcommand{\\De}{\\Delta}$", "$\\newcommand{\\phi}{\\varphi}$", "$\\newcommand{\\th}{\\theta}$", "$\\newcommand{\\om}{\\omega}$", "$\\newcommand{\\Om}{\\Omega}$"]}, {"cell_type": "markdown", "metadata": {}, "source": ["This numerical tour explores local differential operators (grad, div, laplacian) and their use to", "perform edge detection."]}, {"cell_type": "code", "language": "python", "metadata": {}, "outputs": [], "collapsed": false, "input": ["from __future__ import division", "import nt_toolbox as nt", "from nt_solutions import segmentation_1_edge_detection as solutions", "%matplotlib inline", "%load_ext autoreload", "%autoreload 2"]}, {"cell_type": "markdown", "metadata": {}, "source": ["Diffusion and Convolution", "-------------------------", "To obtain robust edge detection method, it is required to first remove", "the noise and small scale features in the image. This can be achieved", "using a linear blurring kernel.", "", "", "Size of the image."]}, {"cell_type": "code", "language": "python", "metadata": {}, "outputs": [], "collapsed": false, "input": ["n = 256*2"]}, {"cell_type": "markdown", "metadata": {}, "source": ["Load an image $f_0$ of $N=n \\times n$ pixels."]}, {"cell_type": "code", "language": "python", "metadata": {}, "outputs": [], "collapsed": false, "input": ["f0 = load_image('hibiscus', n)", "f0 = rescale(sum(f0, 3))"]}, {"cell_type": "markdown", "metadata": {}, "source": ["Display it."]}, {"cell_type": "code", "language": "python", "metadata": {}, "outputs": [], "collapsed": false, "input": ["imageplot(f0)"]}, {"cell_type": "markdown", "metadata": {}, "source": ["Blurring is achieved using convolution:", "$$ f \\star h(x) = \\sum_y f(y-x) h(x) $$", "where we assume periodic boundary condition.", "", "", "This can be computed in $O(N\\log(N))$ operations using the FFT, since", "$$ g = f \\star h \\qarrq \\forall \\om, \\quad \\hat g(\\om) = \\hat f(\\om) \\hat h(\\om). $$"]}, {"cell_type": "code", "language": "python", "metadata": {}, "outputs": [], "collapsed": false, "input": ["cconv = lambda f, h: real(ifft2(fft2(f).*fft2(h)))"]}, {"cell_type": "markdown", "metadata": {}, "source": ["Define a Gaussian blurring kernel of width $\\si$:", "$$ h_\\si(x) = \\frac{1}{Z} e^{ -\\frac{x_1^2+x_2^2}{2\\si^2} }$$", "where $Z$ ensure that $\\hat h(0)=1$."]}, {"cell_type": "code", "language": "python", "metadata": {}, "outputs": [], "collapsed": false, "input": ["t = [0: n/ 2 -n/ 2 + 1: -1]", "[X2, X1] = meshgrid(t, t)", "normalize = lambda h: h/ sum(h(: ))", "h = lambda sigma: normalize(exp(-(X1.^2 + X2.^2)/ (2*sigma^2)))"]}, {"cell_type": "markdown", "metadata": {}, "source": ["Define blurring operator."]}, {"cell_type": "code", "language": "python", "metadata": {}, "outputs": [], "collapsed": false, "input": ["blur = lambda f, sigma: cconv(f, h(sigma))"]}, {"cell_type": "markdown", "metadata": {}, "source": ["__Exercise 1__", "", "Test blurring with several blurring size $\\si$."]}, {"cell_type": "code", "language": "python", "metadata": {}, "outputs": [], "collapsed": false, "input": ["solutions.exo1()"]}, {"cell_type": "code", "language": "python", "metadata": {}, "outputs": [], "collapsed": false, "input": ["## Insert your code here."]}, {"cell_type": "markdown", "metadata": {}, "source": ["Gradient Based Edge Detectiors", "------------------------------", "The simplest edge detectors only make use of the first order derivatives.", "", "", "For continuous functions, the gradient reads", "$$ \\nabla f(x) = \\pa{ \\pd{f(x)}{x_1}, \\pd{f(x)}{x_2} } \\in \\RR^2. $$", "", "", "We discretize this differential operator using first order finite", "differences.", "$$ (\\nabla f)_i = ( f_{i_1,i_2}-f_{i_1-1,i_2}, f_{i_1,i_2}-f_{i_1,i_2-1} ) \\in \\RR^2. $$", "Note that for simplity we use periodic boundary conditions.", "", "", "Compute its gradient, using (here decentered) finite differences."]}, {"cell_type": "code", "language": "python", "metadata": {}, "outputs": [], "collapsed": false, "input": ["s = [n 1: n-1]", "nabla = lambda f: cat(3, f-f(s, : ), f-f(: , s))"]}, {"cell_type": "markdown", "metadata": {}, "source": ["One thus has $ \\nabla : \\RR^N \\mapsto \\RR^{N \\times 2}. $"]}, {"cell_type": "code", "language": "python", "metadata": {}, "outputs": [], "collapsed": false, "input": ["v = nabla(f0)"]}, {"cell_type": "markdown", "metadata": {}, "source": ["One can display each of its components."]}, {"cell_type": "code", "language": "python", "metadata": {}, "outputs": [], "collapsed": false, "input": ["imageplot(v(: , : , 1), 'd/ dx', 1, 2, 1)", "imageplot(v(: , : , 2), 'd/ dy', 1, 2, 2)"]}, {"cell_type": "markdown", "metadata": {}, "source": ["A simple edge detector is simply obtained by obtained the gradient", "magnitude of a smoothed image.", "", "", "", "A very simple edge detector is obtained by simply thresholding the", "gradient magnitude above some $t>0$. The set $\\Ee$ of edges is then", "$$ \\Ee = \\enscond{x}{ d_\\si(x) \\geq t } $$", "where we have defined", "$$ d_\\si(x) = \\norm{\\nabla f_\\si(x)}, \\qwhereq f_\\si = f_0 \\star h_\\si. $$", "", "", "Compute $d_\\si$ for $\\si=1$."]}, {"cell_type": "code", "language": "python", "metadata": {}, "outputs": [], "collapsed": false, "input": ["sigma = 1", "d = sqrt(sum(nabla(blur(f0, sigma)).^2, 3))"]}, {"cell_type": "markdown", "metadata": {}, "source": ["Display it."]}, {"cell_type": "code", "language": "python", "metadata": {}, "outputs": [], "collapsed": false, "input": ["imageplot(d)"]}, {"cell_type": "markdown", "metadata": {}, "source": ["__Exercise 2__", "", "For $\\si=1$, study the influence of the threshold value $t$."]}, {"cell_type": "code", "language": "python", "metadata": {}, "outputs": [], "collapsed": false, "input": ["solutions.exo2()"]}, {"cell_type": "code", "language": "python", "metadata": {}, "outputs": [], "collapsed": false, "input": ["## Insert your code here."]}, {"cell_type": "markdown", "metadata": {}, "source": ["__Exercise 3__", "", "Study the influence of $\\si$."]}, {"cell_type": "code", "language": "python", "metadata": {}, "outputs": [], "collapsed": false, "input": ["solutions.exo3()"]}, {"cell_type": "code", "language": "python", "metadata": {}, "outputs": [], "collapsed": false, "input": ["## Insert your code here."]}, {"cell_type": "markdown", "metadata": {}, "source": ["Zero-crossing of the Laplacian", "------------------------------", "Defining a Laplacian requires to define a divergence operator.", "The divergence operator maps vector field to images.", "For continuous vector fields $v(x) \\in \\RR^2$, it is defined as", "$$ \\text{div}(v)(x) = \\pd{v_1(x)}{x_1} + \\pd{v_2(x)}{x_2} \\in \\RR. $$", "It is minus the adjoint of the gadient, i.e. $\\text{div} = - \\nabla^*$.", "", "", "It is discretized, for $v=(v^1,v^2)$ as", "$$ \\text{div}(v)_i = v^1_{i_1+1,i_2} + v^2_{i_1,i_2+1}. $$"]}, {"cell_type": "code", "language": "python", "metadata": {}, "outputs": [], "collapsed": false, "input": ["t = [2: n 1]", "div = lambda v: v(t, : , 1)-v(: , : , 1) + v(: , t, 2)-v(: , : , 2)"]}, {"cell_type": "markdown", "metadata": {}, "source": ["The Laplacian operatore is defined as $\\Delta=\\text{div} \\circ \\nabla =", "-\\nabla^* \\circ \\nabla$. It is thus a negative symmetric operator."]}, {"cell_type": "code", "language": "python", "metadata": {}, "outputs": [], "collapsed": false, "input": ["delta = lambda f: div(nabla(f))"]}, {"cell_type": "markdown", "metadata": {}, "source": ["Display $\\Delta f_0$."]}, {"cell_type": "code", "language": "python", "metadata": {}, "outputs": [], "collapsed": false, "input": ["imageplot(delta(f0))"]}, {"cell_type": "markdown", "metadata": {}, "source": ["Check that the relation $ \\norm{\\nabla f} = - \\dotp{\\Delta f}{f}. $"]}, {"cell_type": "code", "language": "python", "metadata": {}, "outputs": [], "collapsed": false, "input": ["dotp = lambda a, b: sum(a(: ).*b(: ))", "fprintf('Should be 0: %.3i\\n', dotp(nabla(f0), nabla(f0)) + dotp(delta(f0), f0))"]}, {"cell_type": "markdown", "metadata": {}, "source": ["The zero crossing of the Laplacian is a well known edge detector.", "This requires first blurring the image (which is equivalent to blurring", "the laplacian). The set $\\Ee$ of edges is defined as:", "$$ \\Ee = \\enscond{x}{ \\Delta f_\\si(x) = 0 }", " \\qwhereq f_\\si = f_0 \\star h_\\si . $$", "", "", "It was proposed by Marr and Hildreth:", "", "", "Marr, D. and Hildreth, E.,", "_Theory of edge detection,_", "In Proc. of the Royal Society London B, 207:187-217, 1980.", "", "", "Display the zero crossing."]}, {"cell_type": "code", "language": "python", "metadata": {}, "outputs": [], "collapsed": false, "input": ["sigma = 4", "", "plot_levelset(delta(blur(f0, sigma)) , 0, f0)"]}, {"cell_type": "markdown", "metadata": {}, "source": ["__Exercise 4__", "", "Study the influence of $\\si$."]}, {"cell_type": "code", "language": "python", "metadata": {}, "outputs": [], "collapsed": false, "input": ["solutions.exo4()"]}, {"cell_type": "code", "language": "python", "metadata": {}, "outputs": [], "collapsed": false, "input": ["## Insert your code here."]}, {"cell_type": "markdown", "metadata": {}, "source": ["Hessian Based Edge Detectors", "----------------------------", "Zero-crossing of the Laplacian can be shown to", "return false edges corresponding to local minima", "of the gradient magnitude. Moreover, this operator gives poor localization at curved edges.", "", "", "In order to improve over this basic detector,", "more advanced edge detectors make use of the second order derivatives.", "Several authors have advocated for this choice, in particular:", "", "", "Haralick, R.,", "_Digital step edges from zero crossing of second directional derivatives,_", "IEEE Trans. on Pattern Analysis and Machine Intelligence, 6(1):58-68, 1984.", "", "", "Canny, J.,", "_A computational approach to edge detection,_", "IEEE Trans. on PAMI, 8(6):679-698, 1986", "", "", "Deriche, R.,", "_Using Canny's criteria to derive a recursively implemented optimal edge detector_.", "International Journal of Computer Vision, 1:167-187, 1987.", "", "", "They define the edge locations $\\Ee$ as the zero-crossings of the second-order", "directional derivative in the gradient direction.", "$$ \\Ee = \\enscond{x}{ \\dotp{ H(x) \\times g_\\si(x) }{ g_\\si(x) } = 0 }", " \\qwhereq g_\\si = \\nabla ( f_0 \\star h_\\si ) $$", "where $\\times$ is the matrix-vector multiplication.", "", "", "Define centered first order derivatives."]}, {"cell_type": "code", "language": "python", "metadata": {}, "outputs": [], "collapsed": false, "input": ["dx = lambda f: (f(s, : )-f(t, : )) / 2", "dy = lambda f: dx(f')'"]}, {"cell_type": "markdown", "metadata": {}, "source": ["Define second order derivatives."]}, {"cell_type": "code", "language": "python", "metadata": {}, "outputs": [], "collapsed": false, "input": ["s = [2: n 1]; t = [n 1: n-1]", "d2x = lambda f: f(s, : ) + f(t, : ) - 2*f", "d2y = lambda f: d2x(f')'", "dxy = lambda f: dy(dx(f))"]}, {"cell_type": "markdown", "metadata": {}, "source": ["Define Hessian operator."]}, {"cell_type": "code", "language": "python", "metadata": {}, "outputs": [], "collapsed": false, "input": ["hessian = lambda f: cat(3, d2x(f), dxy(f), d2y(f))"]}, {"cell_type": "markdown", "metadata": {}, "source": ["Compute $g_\\si = \\nabla (f_0 \\star h_\\si). $"]}, {"cell_type": "code", "language": "python", "metadata": {}, "outputs": [], "collapsed": false, "input": ["sigma = 6", "g = grad(blur(f0, sigma))"]}, {"cell_type": "markdown", "metadata": {}, "source": ["Compute $h_\\si = H (f_0 \\star h_\\si). $"]}, {"cell_type": "code", "language": "python", "metadata": {}, "outputs": [], "collapsed": false, "input": ["h = hessian(blur(f0, sigma))"]}, {"cell_type": "markdown", "metadata": {}, "source": ["Compute $ a_\\si(x) = h_\\si(x) \\times g_\\si (x) $", "(this is a matrix times vector operation)."]}, {"cell_type": "code", "language": "python", "metadata": {}, "outputs": [], "collapsed": false, "input": ["a = h(: , : , 1: 2).*repmat(g(: , : , 1), [1 1 2]) + ...", " h(: , : , 2: 3).*repmat(g(: , : , 2), [1 1 2])"]}, {"cell_type": "markdown", "metadata": {}, "source": ["Display the level set of $\\dotp{a_\\si(x)}{g_\\si(x)}$."]}, {"cell_type": "code", "language": "python", "metadata": {}, "outputs": [], "collapsed": false, "input": ["plot_levelset(sum(a.*g, 3) , 0, f0)"]}, {"cell_type": "markdown", "metadata": {}, "source": ["__Exercise 5__", "", "Study the influence of $\\si$."]}, {"cell_type": "code", "language": "python", "metadata": {}, "outputs": [], "collapsed": false, "input": ["solutions.exo5()"]}, {"cell_type": "code", "language": "python", "metadata": {}, "outputs": [], "collapsed": false, "input": ["## Insert your code here."]}]}], "nbformat": 3, "metadata": {"name": ""}, "nbformat_minor": 0}