#!/usr/bin/env python # coding: utf-8 # > 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. # # # 11.2. Applying filters on an image # 1. Let's import the packages. # In[ ]: import numpy as np import matplotlib.pyplot as plt import skimage import skimage.filter as skif import skimage.data as skid import matplotlib as mpl get_ipython().run_line_magic('matplotlib', 'inline') # 2. We create a function that displays a grayscale image. # In[ ]: def show(img): plt.figure(figsize=(4,2)); plt.imshow(img, cmap=plt.cm.gray); plt.axis('off') plt.show(); # 3. Now, we load the Lena image (bundled in scikit-image). We select a single RGB component to get a grayscale image. # In[ ]: img = skimage.img_as_float(skid.lena())[...,0] # In[ ]: show(img) # 4. Let's apply a blurring **Gaussian filter** to the image. # In[ ]: show(skif.gaussian_filter(img, 5.)) # 5. We now apply a **Sobel filter** that enhances the edges in the image. # In[ ]: sobimg = skif.sobel(img) show(sobimg) # 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. # In[ ]: from IPython.html import widgets @widgets.interact(x=(0.01, .4, .005)) def edge(x): show(sobimg You'll find all the explanations, figures, references, and much more in the book (to be released later this summer). # # > [IPython Cookbook](http://ipython-books.github.io/), by [Cyrille Rossant](http://cyrille.rossant.net), Packt Publishing, 2014 (500 pages).