{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# SIFT feature detector and descriptor extractor\n\nThis example demonstrates the SIFT feature detection and its description\nalgorithm.\n\nThe scale-invariant feature transform (SIFT) [1]_ was published in 1999 and is\nstill one of the most popular feature detectors available, as its promises to\nbe \"invariant to image scaling, translation, and rotation, and partially\nin-variant to illumination changes and affine or 3D projection\" [2]_. Its\nbiggest drawback is its runtime, that's said to be \"at two orders of\nmagnitude\" [3]_ slower than ORB, which makes it unsuitable for real-time\napplications.\n\n## References\n.. [1] https://en.wikipedia.org/wiki/Scale-invariant_feature_transform\n\n.. [2] D.G. Lowe. \"Object recognition from local scale-invariant\n       features\", Proceedings of the Seventh IEEE International\n       Conference on Computer Vision, 1999, vol.2, pp. 1150-1157.\n       :DOI:`10.1109/ICCV.1999.790410`\n\n.. [3] Ethan Rublee, Vincent Rabaud, Kurt Konolige and Gary Bradski\n       \"ORB: An efficient alternative to SIFT and SURF\"\n       http://www.gwylab.com/download/ORB_2012.pdf\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\n\nfrom skimage import data\nfrom skimage import transform\nfrom skimage.color import rgb2gray\nfrom skimage.feature import match_descriptors, plot_matched_features, SIFT\n\nimg1 = rgb2gray(data.astronaut())\nimg2 = transform.rotate(img1, 180)\ntform = transform.AffineTransform(scale=(1.3, 1.1), rotation=0.5, translation=(0, -200))\nimg3 = transform.warp(img1, tform)\n\ndescriptor_extractor = SIFT()\n\ndescriptor_extractor.detect_and_extract(img1)\nkeypoints1 = descriptor_extractor.keypoints\ndescriptors1 = descriptor_extractor.descriptors\n\ndescriptor_extractor.detect_and_extract(img2)\nkeypoints2 = descriptor_extractor.keypoints\ndescriptors2 = descriptor_extractor.descriptors\n\ndescriptor_extractor.detect_and_extract(img3)\nkeypoints3 = descriptor_extractor.keypoints\ndescriptors3 = descriptor_extractor.descriptors\n\nmatches12 = match_descriptors(\n    descriptors1, descriptors2, max_ratio=0.6, cross_check=True\n)\nmatches13 = match_descriptors(\n    descriptors1, descriptors3, max_ratio=0.6, cross_check=True\n)\nfig, ax = plt.subplots(nrows=2, ncols=2, figsize=(11, 8))\n\nplt.gray()\n\nplot_matched_features(\n    img1,\n    img2,\n    keypoints0=keypoints1,\n    keypoints1=keypoints2,\n    matches=matches12,\n    ax=ax[0, 0],\n)\nax[0, 0].axis('off')\nax[0, 0].set_title(\"Original Image vs. Flipped Image\\n\" \"(all keypoints and matches)\")\n\nplot_matched_features(\n    img1,\n    img3,\n    keypoints0=keypoints1,\n    keypoints1=keypoints3,\n    matches=matches13,\n    ax=ax[1, 0],\n)\nax[1, 0].axis('off')\nax[1, 0].set_title(\n    \"Original Image vs. Transformed Image\\n\" \"(all keypoints and matches)\"\n)\n\nplot_matched_features(\n    img1,\n    img2,\n    keypoints0=keypoints1,\n    keypoints1=keypoints2,\n    matches=matches12[::15],\n    ax=ax[0, 1],\n    only_matches=True,\n)\nax[0, 1].axis('off')\nax[0, 1].set_title(\n    \"Original Image vs. Flipped Image\\n\" \"(subset of matches for visibility)\"\n)\n\nplot_matched_features(\n    img1,\n    img3,\n    keypoints0=keypoints1,\n    keypoints1=keypoints3,\n    matches=matches13[::15],\n    ax=ax[1, 1],\n    only_matches=True,\n)\nax[1, 1].axis('off')\nax[1, 1].set_title(\n    \"Original Image vs. Transformed Image\\n\" \"(subset of matches for visibility)\"\n)\n\nplt.tight_layout()\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
}