From 53edb67dd3a7a88d9133a85cd2fe1a9d22a3a436 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Sch=C3=B6nberger?= Date: Mon, 3 Sep 2012 22:49:09 +0200 Subject: [PATCH] Add example script for piecewise affine transform --- doc/examples/plot_piecewise_affine.py | 38 +++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 doc/examples/plot_piecewise_affine.py diff --git a/doc/examples/plot_piecewise_affine.py b/doc/examples/plot_piecewise_affine.py new file mode 100644 index 00000000..9db0e402 --- /dev/null +++ b/doc/examples/plot_piecewise_affine.py @@ -0,0 +1,38 @@ +""" +=============================== +Piecewise Affine Transformation +=============================== + +This example shows how to use the Piecewise Affine Transformation. +""" + +import numpy as np +import matplotlib.pyplot as plt +from skimage.transform import PiecewiseAffineTransform, warp +from skimage import data + + +image = data.lena() +rows, cols = image.shape[0], image.shape[1] + +src_cols = np.linspace(0, cols, 20) +src_rows = np.linspace(0, rows, 20) +src_rows, src_cols = np.meshgrid(src_rows, src_cols) +src = np.dstack([src_cols.flat, src_rows.flat])[0] + +# add sinusoidal oscillation to row coordinates +dst_rows = src[:, 1] - np.sin(np.linspace(0, 3 * np.pi, src.shape[0])) * 50 +dst_cols = src[:, 0] +dst_rows *= 1.5 +dst_rows -= 1.5 * 50 +dst = np.vstack([dst_cols, dst_rows]).T + + +tform = PiecewiseAffineTransform() +tform.estimate(src, dst) + +output_shape = (image.shape[0] - 1.5 * 50, image.shape[1]) +out = warp(image, tform, output_shape=output_shape) + +plt.imshow(out) +plt.show()