{ "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "code", "language": "python", "outputs": [], "collapsed": false, "input": [ "%matplotlib inline" ], "metadata": {} }, { "source": "
\n

# Using geometric transformations

\n

In this example, we will see how to use geometric transformations in the context\nof image processing.

\n
\n", "cell_type": "markdown", "metadata": {} }, { "cell_type": "code", "language": "python", "outputs": [], "collapsed": false, "input": "\nfrom __future__ import print_function\n\nimport math\nimport numpy as np\nimport matplotlib.pyplot as plt\n\nfrom skimage import data\nfrom skimage import transform as tf\n\nmargins = dict(hspace=0.01, wspace=0.01, top=1, bottom=0, left=0, right=1)", "metadata": {} }, { "source": "
\n

# Basics

\n

Several different geometric transformation types are supported: similarity,\naffine, projective and polynomial.

\n

Geometric transformations can either be created using the explicit parameters\n(e.g. scale, shear, rotation and translation) or the transformation matrix:

\n

First we create a transformation using explicit parameters:

\n
\n", "cell_type": "markdown", "metadata": {} }, { "cell_type": "code", "language": "python", "outputs": [], "collapsed": false, "input": "\ntform = tf.SimilarityTransform(scale=1, rotation=math.pi / 2,\n translation=(0, 1))\nprint(tform.params)", "metadata": {} }, { "source": "
\n

Alternatively you can define a transformation by the transformation matrix\nitself:

\n
\n", "cell_type": "markdown", "metadata": {} }, { "cell_type": "code", "language": "python", "outputs": [], "collapsed": false, "input": "\nmatrix = tform.params.copy()\nmatrix[1, 2] = 2\ntform2 = tf.SimilarityTransform(matrix)", "metadata": {} }, { "source": "
\n

These transformation objects can then be used to apply forward and inverse\ncoordinate transformations between the source and destination coordinate\nsystems:

\n
\n", "cell_type": "markdown", "metadata": {} }, { "cell_type": "code", "language": "python", "outputs": [], "collapsed": false, "input": "\ncoord = [1, 0]\nprint(tform2(coord))\nprint(tform2.inverse(tform(coord)))", "metadata": {} }, { "source": "
\n

# Image warping

\n

Geometric transformations can also be used to warp images:

\n
\n", "cell_type": "markdown", "metadata": {} }, { "cell_type": "code", "language": "python", "outputs": [], "collapsed": false, "input": "\ntext = data.text()\n\ntform = tf.SimilarityTransform(scale=1, rotation=math.pi / 4,\n translation=(text.shape / 2, -100))\n\nrotated = tf.warp(text, tform)\nback_rotated = tf.warp(rotated, tform.inverse)\n\nfig, (ax1, ax2, ax3) = plt.subplots(ncols=3, figsize=(8, 3))\nfig.subplots_adjust(**margins)\nplt.gray()\nax1.imshow(text)\nax1.axis('off')\nax2.imshow(rotated)\nax2.axis('off')\nax3.imshow(back_rotated)\nax3.axis('off')", "metadata": {} }, { "source": "
\n

# Parameter estimation

\n

In addition to the basic functionality mentioned above you can also estimate the\nparameters of a geometric transformation using the least-squares method.

\n

This can amongst other things be used for image registration or rectification,\nwhere you have a set of control points or homologous/corresponding points in two\nimages.

\n

Let's assume we want to recognize letters on a photograph which was not taken\nfrom the front but at a certain angle. In the simplest case of a plane paper\nsurface the letters are projectively distorted. Simple matching algorithms would\nnot be able to match such symbols. One solution to this problem would be to warp\nthe image so that the distortion is removed and then apply a matching algorithm:

\n
\n", "cell_type": "markdown", "metadata": {} }, { "cell_type": "code", "language": "python", "outputs": [], "collapsed": false, "input": "\ntext = data.text()\n\nsrc = np.array((\n (0, 0),\n (0, 50),\n (300, 50),\n (300, 0)\n))\ndst = np.array((\n (155, 15),\n (65, 40),\n (260, 130),\n (360, 95)\n))\n\ntform3 = tf.ProjectiveTransform()\ntform3.estimate(src, dst)\nwarped = tf.warp(text, tform3, output_shape=(50, 300))\n\nfig, (ax1, ax2) = plt.subplots(nrows=2, figsize=(8, 3))\nfig.subplots_adjust(**margins)\nplt.gray()\nax1.imshow(text)\nax1.plot(dst[:, 0], dst[:, 1], '.r')\nax1.axis('off')\nax2.imshow(warped)\nax2.axis('off')", "metadata": {} }, { "source": "
\n
\n", "cell_type": "markdown", "metadata": {} }, { "cell_type": "code", "language": "python", "outputs": [], "collapsed": false, "input": "\nplt.show()", "metadata": {} } ], "metadata": {} } ], "metadata": { "name": "" } }