{ "metadata": { "name": "", "signature": "sha256:a479b8ae3be7bcbbe4e766c1d91526548e38aa69daade24b2b31aa72bd0f1f05" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "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.4. Finding points of interest in an image" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You need to download the *Child* dataset on the book's website. (http://ipython-books.github.io)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1. Let's import the packages." ] }, { "cell_type": "code", "collapsed": false, "input": [ "import numpy as np\n", "import matplotlib.pyplot as plt\n", "import skimage\n", "import skimage.feature as sf\n", "import matplotlib as mpl\n", "%matplotlib inline\n" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "2. We create a function to display a colored or grayscale image." ] }, { "cell_type": "code", "collapsed": false, "input": [ "def show(img, cmap=None):\n", " cmap = cmap or plt.cm.gray\n", " plt.figure(figsize=(4,2));\n", " plt.imshow(img, cmap=cmap);\n", " plt.axis('off');" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "3. We load an image." ] }, { "cell_type": "code", "collapsed": false, "input": [ "img = plt.imread('data/pic2.jpg')" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "show(img)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "4. We find salient points in the image with the Harris corner method. The first step consists in computing the **Harris corner measure response image** with the `corner_harris` function (we will explain this measure in *How it works...*)." ] }, { "cell_type": "code", "collapsed": false, "input": [ "corners = sf.corner_harris(img[:,:,0])" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "show(corners)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We see that the patterns in the child's coat are particularly well detected by this algorithm." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "5. The next step consists in detecting corners from this measure image, using the `corner_peaks` function." ] }, { "cell_type": "code", "collapsed": false, "input": [ "peaks = sf.corner_peaks(corners)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "show(img)\n", "plt.plot(peaks[:,1], peaks[:,0], 'or', ms=4);" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "6. Finally, we create a box around the corner points to define our region of interest." ] }, { "cell_type": "code", "collapsed": false, "input": [ "ymin, xmin = peaks.min(axis=0)\n", "ymax, xmax = peaks.max(axis=0)\n", "w, h = xmax-xmin, ymax-ymin" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "k = .25\n", "xmin -= k*w\n", "xmax += k*w\n", "ymin -= k*h\n", "ymax += k*h" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "show(img[ymin:ymax,xmin:xmax])" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "> 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": {} } ] }