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 RANSAC

From Leeroopedia


Knowledge Sources
Domains Vision, Geometry, Robust_Estimation
Last Updated 2026-02-09 15:00 GMT

Overview

Concrete tool for robust geometric model estimation via RANSAC provided by Kornia.

Description

The RANSAC class implements the Random Sample Consensus algorithm as a differentiable nn.Module. It supports multiple model types: "homography", "fundamental", "fundamental_7pt", "essential", and "homography_from_linesegments". Features include batched hypothesis evaluation, local optimization (LO-RANSAC), PROSAC priority sampling, and configurable scoring (RANSAC or MSAC). The module uses appropriate minimal solvers (4-point for homography, 8-point for fundamental) and verifiers (symmetric transfer error for homography, Sampson error for fundamental).

Usage

Initialize with model_type and threshold. Call with keypoint pairs to get the estimated model and inlier mask.

Code Reference

Source Location

  • Repository: kornia
  • File: kornia/geometry/ransac.py
  • Lines: L42-400

Signature

class RANSAC(nn.Module):
    def __init__(
        self,
        model_type: str = "homography",
        inl_th: float = 2.0,
        batch_size: int = 2048,
        max_iter: int = 10,
        confidence: float = 0.99,
        max_lo_iters: int = 5,
        score_type: str = "ransac",
        prosac_sampling: bool = False
    ) -> None

Forward

def forward(
    self,
    kp1: torch.Tensor,
    kp2: torch.Tensor,
    weights: Optional[torch.Tensor] = None
) -> Tuple[torch.Tensor, torch.Tensor]

Import

from kornia.geometry import RANSAC

I/O Contract

Inputs

Name Type Required Description
kp1 torch.Tensor Yes Source keypoints of shape (N, 2)
kp2 torch.Tensor Yes Target keypoints of shape (N, 2)
weights Optional[torch.Tensor] No Correspondence weights for PROSAC sampling

Outputs

Name Type Description
model torch.Tensor (3, 3) Estimated geometric model (homography or fundamental matrix)
inlier_mask torch.Tensor (N,) Boolean tensor indicating inlier correspondences

Usage Examples

Homography Estimation

import torch
from kornia.geometry import RANSAC

ransac = RANSAC(model_type="homography", inl_th=2.0, confidence=0.99)

kp1 = torch.rand(100, 2) * 256  # keypoints in image 1
kp2 = torch.rand(100, 2) * 256  # keypoints in image 2

homography, inlier_mask = ransac(kp1, kp2)
# homography.shape == (3, 3)
# inlier_mask.shape == (100,)
print(f"Inliers: {inlier_mask.sum().item()} / {len(inlier_mask)}")

Fundamental Matrix Estimation

import torch
from kornia.geometry import RANSAC

ransac = RANSAC(model_type="fundamental", inl_th=1.0)

kp1 = torch.rand(200, 2) * 480
kp2 = torch.rand(200, 2) * 480

fundamental, inlier_mask = ransac(kp1, kp2)
# fundamental.shape == (3, 3)

Related Pages

Implements Principle

Page Connections

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