配置LoFTR: Detector-Free Local Feature Matching with Transformers,运行demo

1、论文下载地址

https://arxiv.org/pdf/2104.00680.pdf

2、代码下载地址

GitHub - zju3dv/LoFTR: Code for "LoFTR: Detector-Free Local Feature Matching with Transformers", CVPR 2021, T-PAMI 2022

3、解压代码包,在Anaconda Prompt中打开LoFTR-master,利用以下代码创建虚拟环境

conda env create --file environment.yaml

此过程耗费时间长,请耐心等待。

4、下载相关模型

链接:https://pan.baidu.com/s/1_eVrFVY5gapQ9CwPH4wi3w?pwd=vdnx 
提取码:vdnx
下载完成后,将文件放入LoFTR-master\demo文件夹下

5、在demo文件夹下创建两个文件夹,命名为images和output。

其中images文件夹需要放入进行特征匹配的照片,output文件夹则是用来存放输出结果。

6、将LoFTR-master文件夹下的src拷贝到demo文件夹下,最终demo文件夹展示如下

7、将新建.py文件,将以下代码写入,其中

import os
os.chdir("..")
from copy import deepcopy
 
# import os
# os.environ["CUDA_VISIBLE_DEVICES"] = "0"
 
import torch
import cv2
import numpy as np
import matplotlib.cm as cm
from src.utils.plotting import make_matching_figure
 
 
from src.loftr import LoFTR, default_cfg
 
# The default config uses dual-softmax.
# The outdoor and indoor models share the same config.
# You can change the default values like thr and coarse_match_type.
 
matcher = LoFTR(config=default_cfg)
matcher.load_state_dict(torch.load("LoFTR-master\demo\outdoor_ds.ckpt")['state_dict'])
matcher = matcher.eval().cuda()
 
 
default_cfg['coarse']
 
# Load example images
img0_pth = "LoFTR-master\demo\images\car-c.jpg" # 照片名字需要进行替换
img1_pth = "LoFTR-master\demo\images\car-d.jpg" # 照片名字需要进行替换
img0_raw = cv2.imread(img0_pth, cv2.IMREAD_GRAYSCALE)
img1_raw = cv2.imread(img1_pth, cv2.IMREAD_GRAYSCALE)

if img0_raw is None:
    print('Failed to load image:', img0_pth)
    exit(0)
if img1_raw is None:
    print('Failed to load image:', img1_pth)
    exit(0)
    
img0_raw = cv2.resize(img0_raw, (img0_raw.shape[1]//8*8, img0_raw.shape[0]//8*8))  # input size shuold be divisible by 8
img1_raw = cv2.resize(img1_raw, (img1_raw.shape[1]//8*8, img1_raw.shape[0]//8*8))
 
img0 = torch.from_numpy(img0_raw)[None][None].cuda() / 255.
img1 = torch.from_numpy(img1_raw)[None][None].cuda() / 255.
batch = {'image0': img0, 'image1': img1}
 
# Inference with LoFTR and get prediction
with torch.no_grad():
    matcher(batch)
    mkpts0 = batch['mkpts0_f'].cpu().numpy()
    mkpts1 = batch['mkpts1_f'].cpu().numpy()
    mconf = batch['mconf'].cpu().numpy()
 
# Draw
color = cm.jet(mconf)
text = [
    'LoFTR',
    'Matches: {}'.format(len(mkpts0)),
]
fig = make_matching_figure(img0_raw, img1_raw, mkpts0, mkpts1, color, text=text,path="LoFTR-master/demo/output/LoFTR-colab-demo.pdf")

8、运行上一步的py文件,运行成功后会在output文件夹下生成一个LoFTR-colab-demo.pdf文件,该文件则为最后的输出结果。

9、效果展示

  • 9
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值