{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "GzFYZYcT9oyb" }, "source": [ "# RDD matching example (sparse, semi-dense and lightglue)" ] }, { "cell_type": "markdown", "metadata": { "id": "97Mbt4a89z3Z" }, "source": [ "## Initialize RDD" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from RDD.RDD import build\n", "from RDD.RDD_helper import RDD_helper\n", "from matplotlib import pyplot as plt\n", "from time import time\n", "\n", "RDD_model = build(weights='./weights/RDD-v2.pth')\n", "RDD_model.eval()\n", "RDD = RDD_helper(RDD_model)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import cv2\n", "\n", "def draw_matches(ref_points, dst_points, img0, img1):\n", " \n", " # Prepare keypoints and matches for drawMatches function\n", " keypoints0 = [cv2.KeyPoint(p[0], p[1], 1000) for p in ref_points]\n", " keypoints1 = [cv2.KeyPoint(p[0], p[1], 1000) for p in dst_points]\n", " matches = [cv2.DMatch(i,i,0) for i in range(len(ref_points))]\n", "\n", " # Draw inlier matches\n", " img_matches = cv2.drawMatches(img0, keypoints0, img1, keypoints1, matches, None,\n", " matchColor=(0, 255, 0), flags=2)\n", "\n", " return img_matches\n", "\n", "\n", "def draw_points(points, img):\n", " for p in points:\n", " cv2.circle(img, (int(p[0]), int(p[1])), 2, (0, 255, 0), -1)\n", " \n", " return img\n" ] }, { "cell_type": "markdown", "metadata": { "id": "b83vE-Dt-cTC" }, "source": [ "## Matching example - Sparse" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#Load some example images\n", "im0 = cv2.imread('./assets/image0.jpg')\n", "im1 = cv2.imread('./assets/image1.jpg')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 431 }, "id": "8qm_cdIq9-jy", "outputId": "ebd99a35-807d-4684-f43b-4f1b0a022c66" }, "outputs": [], "source": [ "start = time()\n", "mkpts_0, mkpts_1, conf = RDD.match(im0, im1, resize=1024)\n", "print(f\"Found {len(mkpts_0)} matches in {time()-start:.2f} seconds\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "canvas = draw_matches(mkpts_0, mkpts_1, im0, im1)\n", "plt.figure(figsize=(12,12))\n", "plt.imshow(canvas[..., ::-1]), plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Matching example - Semi-Dense" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "start = time()\n", "mkpts_0, mkpts_1, conf = RDD.match_dense(im0, im1, resize=1024)\n", "print(f\"Found {len(mkpts_0)} matches in {time()-start:.2f} seconds\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "canvas = draw_matches(mkpts_0, mkpts_1, im0, im1)\n", "plt.figure(figsize=(12,12))\n", "plt.imshow(canvas[..., ::-1]), plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Matching example - LightGlue" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "start = time()\n", "mkpts_0, mkpts_1, conf = RDD.match_lg(im0, im1, resize=1024)\n", "print(f\"Found {len(mkpts_0)} matches in {time()-start:.2f} seconds\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "canvas = draw_matches(mkpts_0, mkpts_1, im0, im1)\n", "plt.figure(figsize=(12,12))\n", "plt.imshow(canvas[..., ::-1]), plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Matching example - Using differnt detector + RDD descriptor" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "start = time()\n", "mkpts_0, mkpts_1, conf = RDD.match_3rd_party(im0, im1, resize=1024, model='aliked')\n", "print(f\"Found {len(mkpts_0)} matches in {time()-start:.2f} seconds\")\n", "\n", "# take a look at folder third_party, RDD/RDD.py and RDD/RDD_helper.py \n", "# if you want to configure your own detector" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "colab": { "collapsed_sections": [ "KM1KQaj9-oOv" ], "provenance": [], "toc_visible": true }, "kernelspec": { "display_name": "RDD", "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.8.18" } }, "nbformat": 4, "nbformat_minor": 0 }