From 724a931d4217175a85a7aea02c77a48ec7d222f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Sch=C3=B6nberger?= Date: Fri, 10 Aug 2012 07:49:04 +0200 Subject: [PATCH] adapt geometric example script to new API and improve some expressions --- doc/examples/applications/plot_geometric.py | 81 ++++++++++----------- 1 file changed, 39 insertions(+), 42 deletions(-) diff --git a/doc/examples/applications/plot_geometric.py b/doc/examples/applications/plot_geometric.py index 4e0a0cea..337ecad7 100644 --- a/doc/examples/applications/plot_geometric.py +++ b/doc/examples/applications/plot_geometric.py @@ -25,24 +25,27 @@ affine, projective and polynomial. Geometric transformations can either be created using the explicit parameters (e.g. scale, shear, rotation and translation) or the transformation matrix: + +First we create a transformation using explicit parameters: """ -#: create using explicit parameters -tform = tf.SimilarityTransform() -scale = 1 -rotation = math.pi/2 -translation = (0, 1) -tform.compose_implicit(scale, rotation, translation) +tform = tf.SimilarityTransform(scale=1, rotation=math.pi / 2, + translation=(0, 1)) print tform._matrix -#: create using transformation matrix +""" +Alternatively you can define a transformation by the transformation matrix +itself: +""" + matrix = tform._matrix.copy() matrix[1, 2] = 2 tform2 = tf.SimilarityTransform(matrix) """ -These transformation objects can be used to forward and reverse transform -coordinates between the source and destination coordinate systems: +These transformation objects can then be used to apply forward and inverse +coordinate transformations between the source and destination coordinate +systems: """ coord = [1, 0] @@ -57,26 +60,22 @@ Geometric transformations can also be used to warp images: """ text = data.text() -tform.compose_implicit(1, math.pi/4, (text.shape[0] / 2, -100)) -# uses tform.inverse, alternatively use tf.warp(text, tform.inverse) +tform = tf.SimilarityTransform(scale=1, rotation=math.pi / 4, + translation=(text.shape[0] / 2, -100)) + rotated = tf.warp(text, tform) -back_rotated = tf.warp(rotated, tform) +back_rotated = tf.warp(rotated, tform.inverse) -plt.figure(figsize=(8, 3)) -plt.subplot(131) -plt.imshow(text) -plt.axis('off') +fig, (ax1, ax2, ax3) = plt.subplots(ncols=3, figsize=(8, 3)) +fig.subplots_adjust(**margins) plt.gray() -plt.subplot(132) -plt.imshow(rotated) -plt.axis('off') -plt.gray() -plt.subplot(133) -plt.imshow(back_rotated) -plt.axis('off') -plt.gray() -plt.subplots_adjust(**margins) +ax1.imshow(text) +ax1.axis('off') +ax2.imshow(rotated) +ax2.axis('off') +ax3.imshow(back_rotated) +ax3.axis('off') """ .. image:: PLOT2RST.current_figure @@ -88,7 +87,8 @@ In addition to the basic functionality mentioned above you can also estimate the parameters of a geometric transformation using the least-squares method. This can amongst other things be used for image registration or rectification, -where you have a set of control points or homologous points in two images. +where you have a set of control points or homologous/corresponding points in two +images. Let's assume we want to recognize letters on a photograph which was not taken from the front but at a certain angle. In the simplest case of a plane paper @@ -100,33 +100,30 @@ the image so that the distortion is removed and then apply a matching algorithm: text = data.text() src = np.array(( - (155, 15), - (65, 40), - (260, 130), - (360, 95) -)) -dst = np.array(( (0, 0), (0, 50), (300, 50), (300, 0) )) +dst = np.array(( + (155, 15), + (65, 40), + (260, 130), + (360, 95) +)) tform3 = tf.ProjectiveTransform() tform3.estimate(src, dst) warped = tf.warp(text, tform3, output_shape=(50, 300)) -plt.figure(figsize=(8, 3)) -plt.subplot(211) -plt.imshow(text) -plt.plot(src[:, 0], src[:, 1], '.r') -plt.axis('off') +fig, (ax1, ax2) = plt.subplots(nrows=2, figsize=(8, 3)) +fig.subplots_adjust(**margins) plt.gray() -plt.subplot(212) -plt.imshow(warped) -plt.axis('off') -plt.gray() -plt.subplots_adjust(**margins) +ax1.imshow(text) +ax1.plot(dst[:, 0], dst[:, 1], '.r') +ax1.axis('off') +ax2.imshow(warped) +ax2.axis('off') """ .. image:: PLOT2RST.current_figure