{"worksheets": [{"cells": [{"cell_type": "markdown", "metadata": {}, "source": ["Introduction to Image Processing", "================================", "", "*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 some basic image processing tasks."]}, {"cell_type": "code", "language": "python", "metadata": {}, "outputs": [], "collapsed": false, "input": ["from __future__ import division", "import nt_toolbox as nt", "from nt_solutions import introduction_3_image as solutions", "%matplotlib inline", "%load_ext autoreload", "%autoreload 2"]}, {"cell_type": "markdown", "metadata": {}, "source": ["Image Loading and Displaying", "----------------------------", "Several functions are implemented to load and display images.", "", "", "First we load an image.", "", "path to the images"]}, {"cell_type": "code", "language": "python", "metadata": {}, "outputs": [], "collapsed": false, "input": ["name = 'lena'", "n = 256", "M = load_image(name, [])", "M = rescale(crop(M, n))"]}, {"cell_type": "markdown", "metadata": {}, "source": ["We can display it. It is possible to zoom on it, extract pixels, etc."]}, {"cell_type": "code", "language": "python", "metadata": {}, "outputs": [], "collapsed": false, "input": ["imageplot(M, 'Original', 1, 2, 1)", "imageplot(crop(M, 50), 'Zoom', 1, 2, 2)"]}, {"cell_type": "markdown", "metadata": {}, "source": ["Image Modification", "------------------", "", "", "An image is a 2D array, that can be modified as a matrix."]}, {"cell_type": "code", "language": "python", "metadata": {}, "outputs": [], "collapsed": false, "input": ["imageplot(-M, '-M', 1, 2, 1)", "imageplot(M(n: -1: 1, : ), 'Flipped', 1, 2, 2)"]}, {"cell_type": "markdown", "metadata": {}, "source": ["Blurring is achieved by computing a convolution with a kernel.", "", "compute the low pass kernel"]}, {"cell_type": "code", "language": "python", "metadata": {}, "outputs": [], "collapsed": false, "input": ["k = 9", "h = ones(k, k)", "h = h/ sum(h(: ))"]}, {"cell_type": "markdown", "metadata": {}, "source": ["compute the convolution"]}, {"cell_type": "code", "language": "python", "metadata": {}, "outputs": [], "collapsed": false, "input": ["Mh = perform_convolution(M, h)"]}, {"cell_type": "markdown", "metadata": {}, "source": ["display"]}, {"cell_type": "code", "language": "python", "metadata": {}, "outputs": [], "collapsed": false, "input": ["imageplot(M, 'Image', 1, 2, 1)", "imageplot(Mh, 'Blurred', 1, 2, 2)"]}, {"cell_type": "markdown", "metadata": {}, "source": ["Several differential and convolution operators are implemented."]}, {"cell_type": "code", "language": "python", "metadata": {}, "outputs": [], "collapsed": false, "input": ["G = grad(M)", "", "imageplot(G(: , : , 1), 'd/ dx', 1, 2, 1)", "imageplot(G(: , : , 2), 'd/ dy', 1, 2, 2)"]}, {"cell_type": "markdown", "metadata": {}, "source": ["Fourier Transform", "-----------------", "The 2D Fourier transform can be used to perform low pass approximation", "and interpolation (by zero padding).", "", "", "Compute and display the Fourier transform (display over a log scale).", "The function |fftshift| is useful to put the 0 low frequency in the", "middle. After |fftshift|, the zero frequency is located at position", "(n/2+1,n/2+1)."]}, {"cell_type": "code", "language": "python", "metadata": {}, "outputs": [], "collapsed": false, "input": ["Mf = fft2(M)", "Lf = fftshift(log(abs(Mf) + 1e-1))", "", "imageplot(M, 'Image', 1, 2, 1)", "imageplot(Lf, 'Fourier transform', 1, 2, 2)"]}, {"cell_type": "markdown", "metadata": {}, "source": ["__Exercise 1__", "", "To avoid boundary artifacts and estimate really the frequency content of", "the image (and not of the artifacts!), one needs to multiply |M| by a", "smooth windowing function |h| and compute |fft2(M.*h)|. Use a sine", "windowing function. Can you interpret the resulting filter ?", "ompute kernel h", "ompute FFT", "isplay"]}, {"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": ["__Exercise 2__", "", "Perform low pass filtering by removing the high frequencies of the", "spectrum. What do you oberve ?", "isplay"]}, {"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": ["It is possible to do image interpolating by adding high frequencies"]}, {"cell_type": "code", "language": "python", "metadata": {}, "outputs": [], "collapsed": false, "input": ["p = 64", "n = p*4", "M = load_image('boat', 2*p); M = crop(M, p)", "Mf = fftshift(fft2(M))", "MF = zeros(n, n)", "sel = n/ 2-p/ 2 + 1: n/ 2 + p/ 2", "sel = sel", "MF(sel, sel) = Mf", "MF = fftshift(MF)", "Mpad = real(ifft2(MF))", "", "imageplot(crop(M), 'Image', 1, 2, 1)", "imageplot(crop(Mpad), 'Interpolated', 1, 2, 2)"]}, {"cell_type": "markdown", "metadata": {}, "source": ["A better way to do interpolation is to use cubic-splines.", "It avoid ringing artifact because the spline kernel has a smaller support", "with less oscillations."]}, {"cell_type": "code", "language": "python", "metadata": {}, "outputs": [], "collapsed": false, "input": ["Mspline = image_resize(M, n, n)", "", "imageplot(crop(Mpad), 'Fourier (sinc)', 1, 2, 1)", "imageplot(crop(Mspline), 'Spline', 1, 2, 2)"]}]}], "nbformat": 3, "metadata": {"name": ""}, "nbformat_minor": 0}