{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Image Registration\n\nIn this example, we use phase cross-correlation to identify the\nrelative shift between two similar-sized images.\n\nThe ``phase_cross_correlation`` function uses cross-correlation in\nFourier space, optionally employing an upsampled matrix-multiplication\nDFT to achieve arbitrary subpixel precision [1]_.\n\n.. [1] Manuel Guizar-Sicairos, Samuel T. Thurman, and James R. Fienup,\n       \"Efficient subpixel image registration algorithms,\" Optics Letters 33,\n       156-158 (2008). :DOI:`10.1364/OL.33.000156`\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import numpy as np\nimport matplotlib.pyplot as plt\n\nfrom skimage import data\nfrom skimage.registration import phase_cross_correlation\nfrom skimage.registration._phase_cross_correlation import _upsampled_dft\nfrom scipy.ndimage import fourier_shift\n\nimage = data.camera()\nshift = (-22.4, 13.32)\n# The shift corresponds to the pixel offset relative to the reference image\noffset_image = fourier_shift(np.fft.fftn(image), shift)\noffset_image = np.fft.ifftn(offset_image)\nprint(f'Known offset (y, x): {shift}')\n\n# pixel precision first\nshift, error, diffphase = phase_cross_correlation(image, offset_image)\n\nfig = plt.figure(figsize=(8, 3))\nax1 = plt.subplot(1, 3, 1)\nax2 = plt.subplot(1, 3, 2, sharex=ax1, sharey=ax1)\nax3 = plt.subplot(1, 3, 3)\n\nax1.imshow(image, cmap='gray')\nax1.set_axis_off()\nax1.set_title('Reference image')\n\nax2.imshow(offset_image.real, cmap='gray')\nax2.set_axis_off()\nax2.set_title('Offset image')\n\n# Show the output of a cross-correlation to show what the algorithm is\n# doing behind the scenes\nimage_product = np.fft.fft2(image) * np.fft.fft2(offset_image).conj()\ncc_image = np.fft.fftshift(np.fft.ifft2(image_product))\nax3.imshow(cc_image.real)\nax3.set_axis_off()\nax3.set_title(\"Cross-correlation\")\n\nplt.show()\n\nprint(f'Detected pixel offset (y, x): {shift}')\n\n# subpixel precision\nshift, error, diffphase = phase_cross_correlation(\n    image, offset_image, upsample_factor=100\n)\n\nfig = plt.figure(figsize=(8, 3))\nax1 = plt.subplot(1, 3, 1)\nax2 = plt.subplot(1, 3, 2, sharex=ax1, sharey=ax1)\nax3 = plt.subplot(1, 3, 3)\n\nax1.imshow(image, cmap='gray')\nax1.set_axis_off()\nax1.set_title('Reference image')\n\nax2.imshow(offset_image.real, cmap='gray')\nax2.set_axis_off()\nax2.set_title('Offset image')\n\n# Calculate the upsampled DFT, again to show what the algorithm is doing\n# behind the scenes.  Constants correspond to calculated values in routine.\n# See source code for details.\ncc_image = _upsampled_dft(image_product, 150, 100, (shift * 100) + 75).conj()\nax3.imshow(cc_image.real)\nax3.set_axis_off()\nax3.set_title(\"Supersampled XC sub-area\")\n\n\nplt.show()\n\nprint(f'Detected subpixel offset (y, x): {shift}')"
      ]
    }
  ],
  "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
}