{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Piecewise Affine Transformation\n\nThis example shows how to use the Piecewise Affine Transformation.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import numpy as np\nimport matplotlib.pyplot as plt\nfrom skimage.transform import PiecewiseAffineTransform, warp\nfrom skimage import data\n\n\nimage = data.astronaut()\nrows, cols = image.shape[0], image.shape[1]\n\nsrc_cols = np.linspace(0, cols, 20)\nsrc_rows = np.linspace(0, rows, 10)\nsrc_rows, src_cols = np.meshgrid(src_rows, src_cols)\nsrc = np.dstack([src_cols.flat, src_rows.flat])[0]\n\n# add sinusoidal oscillation to row coordinates\ndst_rows = src[:, 1] - np.sin(np.linspace(0, 3 * np.pi, src.shape[0])) * 50\ndst_cols = src[:, 0]\ndst_rows *= 1.5\ndst_rows -= 1.5 * 50\ndst = np.vstack([dst_cols, dst_rows]).T\n\n\ntform = PiecewiseAffineTransform.from_estimate(src, dst)\n\nout_rows = image.shape[0] - 1.5 * 50\nout_cols = cols\nout = warp(image, tform, output_shape=(out_rows, out_cols))\n\nfig, ax = plt.subplots()\nax.imshow(out)\nax.plot(tform.inverse(src)[:, 0], tform.inverse(src)[:, 1], '.b')\nax.axis((0, out_cols, out_rows, 0))\nplt.show()"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.12.12"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}