{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# BRIEF binary descriptor\n\nThis example demonstrates the BRIEF binary description algorithm. The descriptor\nconsists of relatively few bits and can be computed using a set of intensity\ndifference tests. The short binary descriptor results in low memory footprint\nand very efficient matching based on the Hamming distance metric. BRIEF does not\nprovide rotation-invariance. Scale-invariance can be achieved by detecting and\nextracting features at different scales.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from skimage import data\nfrom skimage import transform\nfrom skimage.feature import (\n    match_descriptors,\n    corner_peaks,\n    corner_harris,\n    plot_matched_features,\n    BRIEF,\n)\nfrom skimage.color import rgb2gray\nimport matplotlib.pyplot as plt\n\n\nimg1 = rgb2gray(data.astronaut())\ntform = transform.AffineTransform(scale=(1.2, 1.2), translation=(0, -100))\nimg2 = transform.warp(img1, tform)\nimg3 = transform.rotate(img1, 25)\n\nkeypoints1 = corner_peaks(corner_harris(img1), min_distance=5, threshold_rel=0.1)\nkeypoints2 = corner_peaks(corner_harris(img2), min_distance=5, threshold_rel=0.1)\nkeypoints3 = corner_peaks(corner_harris(img3), min_distance=5, threshold_rel=0.1)\n\nextractor = BRIEF()\n\nextractor.extract(img1, keypoints1)\nkeypoints1 = keypoints1[extractor.mask]\ndescriptors1 = extractor.descriptors\n\nextractor.extract(img2, keypoints2)\nkeypoints2 = keypoints2[extractor.mask]\ndescriptors2 = extractor.descriptors\n\nextractor.extract(img3, keypoints3)\nkeypoints3 = keypoints3[extractor.mask]\ndescriptors3 = extractor.descriptors\n\nmatches12 = match_descriptors(descriptors1, descriptors2, cross_check=True)\nmatches13 = match_descriptors(descriptors1, descriptors3, cross_check=True)\n\nfig, ax = plt.subplots(nrows=2, ncols=1)\n\nplt.gray()\n\nplot_matched_features(\n    img1,\n    img2,\n    keypoints0=keypoints1,\n    keypoints1=keypoints2,\n    matches=matches12,\n    ax=ax[0],\n)\nax[0].axis('off')\nax[0].set_title(\"Original Image vs. Transformed Image\")\n\nplot_matched_features(\n    img1,\n    img3,\n    keypoints0=keypoints1,\n    keypoints1=keypoints3,\n    matches=matches13,\n    ax=ax[1],\n)\nax[1].axis('off')\nax[1].set_title(\"Original Image vs. Transformed Image\")\n\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
}