{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "> This is one of the 100 recipes of the [IPython Cookbook](http://ipython-books.github.io/), the definitive guide to high-performance scientific computing and data science in Python.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 11.2. Applying filters on an image" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1. Let's import the packages." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import numpy as np\n", "import matplotlib.pyplot as plt\n", "import skimage\n", "import skimage.filter as skif\n", "import skimage.data as skid\n", "import matplotlib as mpl\n", "%matplotlib inline\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "2. We create a function that displays a grayscale image." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def show(img):\n", " plt.figure(figsize=(4,2));\n", " plt.imshow(img, cmap=plt.cm.gray);\n", " plt.axis('off')\n", " plt.show();" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "3. Now, we load the Lena image (bundled in scikit-image). We select a single RGB component to get a grayscale image." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "img = skimage.img_as_float(skid.lena())[...,0]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "show(img)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "4. Let's apply a blurring **Gaussian filter** to the image." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "show(skif.gaussian_filter(img, 5.))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "5. We now apply a **Sobel filter** that enhances the edges in the image." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "sobimg = skif.sobel(img)\n", "show(sobimg)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "6. We can threshold the filtered image to get a *sketch effect*. We obtain a binary image that only contains the edges. We use a notebook widget to find an adequate thresholding value." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from IPython.html import widgets\n", "@widgets.interact(x=(0.01, .4, .005))\n", "def edge(x):\n", " show(sobimg You'll find all the explanations, figures, references, and much more in the book (to be released later this summer).\n", "\n", "> [IPython Cookbook](http://ipython-books.github.io/), by [Cyrille Rossant](http://cyrille.rossant.net), Packt Publishing, 2014 (500 pages)." ] } ], "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.4.2" } }, "nbformat": 4, "nbformat_minor": 0 }