{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Swirl\n\nImage swirling is a non-linear image deformation that creates a whirlpool\neffect.  This example describes the implementation of this transform in\n``skimage``, as well as the underlying warp mechanism.\n\n## Image warping\nWhen applying a geometric transformation on an image, we typically make use of\na reverse mapping, i.e., for each pixel in the output image, we compute its\ncorresponding position in the input.  The reason is that, if we were to do it\nthe other way around (map each input pixel to its new output position), some\npixels in the output may be left empty.  On the other hand, each output\ncoordinate has exactly one corresponding location in (or outside) the input\nimage, and even if that position is non-integer, we may use interpolation to\ncompute the corresponding image value.\n\n## Performing a reverse mapping\nTo perform a geometric warp in ``skimage``, you simply need to provide the\nreverse mapping to the :py:func:`skimage.transform.warp` function. E.g., consider\nthe case where we would like to shift an image 50 pixels to the left. The reverse\nmapping for such a shift would be::\n\n    def shift_left(xy):\n        xy[:, 0] += 50\n        return xy\n\nThe corresponding call to warp is::\n\n    from skimage.transform import warp\n    warp(image, shift_left)\n\n## The swirl transformation\nConsider the coordinate $(x, y)$ in the output image.  The reverse\nmapping for the swirl transformation first computes, relative to a center\n$(x_0, y_0)$, its polar coordinates,\n\n\\begin{align}\\theta = \\arctan((y-y0)/(x-x0))\n\n    \\rho = \\sqrt{(x - x_0)^2 + (y - y_0)^2},\\end{align}\n\nand then transforms them according to\n\n\\begin{align}r = \\ln(2) \\, \\mathtt{radius} / 5\n\n    \\phi = \\mathtt{rotation}\n\n    s = \\mathtt{strength}\n\n    \\theta' = \\phi + s \\, e^{-\\rho / r} + \\theta\\end{align}\n\nwhere ``radius`` indicates the swirl extent in pixels, ``rotation`` adds a\nrotation angle, and ``strength`` is a parameter for the amount of swirl. The\ntransformation of ``radius`` into $r$ is to ensure that the\ntransformation decays to $\\approx 1/1000^{\\mathsf{th}}$ within the\nspecified radius.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\n\nfrom skimage import data\nfrom skimage.transform import swirl\n\n\nimage = data.checkerboard()\nswirled = swirl(image, rotation=0, strength=10, radius=120)\n\nfig, (ax0, ax1) = plt.subplots(\n    nrows=1, ncols=2, figsize=(8, 3), sharex=True, sharey=True\n)\n\nax0.imshow(image, cmap=plt.cm.gray)\nax0.axis('off')\nax1.imshow(swirled, cmap=plt.cm.gray)\nax1.axis('off')\n\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
}