Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Kornia Kornia LoFTR

From Leeroopedia


Knowledge Sources
Domains Vision, Feature_Matching, Deep_Learning
Last Updated 2026-02-09 15:00 GMT

Overview

Concrete tool for detector-free dense feature matching using Transformers provided by Kornia.

Description

The LoFTR class implements the Detector-Free Local Feature Matching with Transformers architecture. It accepts a dictionary with two grayscale images {"image0": (B,1,H,W), "image1": (B,1,H,W)} and returns matched keypoint coordinates, confidence scores, and batch indices. Pretrained weights are available for "outdoor" and "indoor" scenes. The module handles feature extraction, coarse matching via Transformer attention, and fine-level sub-pixel refinement.

Usage

Initialize with pretrained weights and pass image pair dictionaries. Commonly used with ImageStitcher or as input to RANSAC for geometric verification.

Code Reference

Source Location

  • Repository: kornia
  • File: kornia/feature/loftr/loftr.py
  • Lines: L70-212

Signature

class LoFTR(nn.Module):
    def __init__(
        self,
        pretrained: Optional[str] = "outdoor",
        config: dict[str, Any] = default_cfg
    ) -> None

Forward

def forward(
    self,
    data: dict[str, torch.Tensor]
) -> dict[str, torch.Tensor]

Import

from kornia.feature import LoFTR

I/O Contract

Inputs

Name Type Required Description
data dict[str, torch.Tensor] Yes Dictionary with keys "image0" and "image1", each a grayscale tensor of shape (B, 1, H, W)

Outputs

Name Type Description
keypoints0 torch.Tensor (N, 2) Matched keypoint coordinates in image0
keypoints1 torch.Tensor (N, 2) Matched keypoint coordinates in image1
confidence torch.Tensor (N,) Confidence scores for each match
batch_indexes torch.Tensor (N,) Batch index for each match

Usage Examples

Basic Matching Between Two Images

import torch
from kornia.feature import LoFTR

matcher = LoFTR(pretrained="outdoor")
img0 = torch.rand(1, 1, 480, 640)  # grayscale
img1 = torch.rand(1, 1, 480, 640)

input_dict = {"image0": img0, "image1": img1}
with torch.inference_mode():
    result = matcher(input_dict)

keypoints0 = result["keypoints0"]  # (N, 2)
keypoints1 = result["keypoints1"]  # (N, 2)
confidence = result["confidence"]  # (N,)

Using with ImageStitcher

from kornia.feature import LoFTR
from kornia.contrib import ImageStitcher

matcher = LoFTR(pretrained="outdoor")
stitcher = ImageStitcher(matcher)

Related Pages

Implements Principle

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment