Ultralytics YOLO27: Join the waitlist.
License YOLO
Back to Ultralytics Glossary

Homography

Homography is a projective transformation that maps planes between views, enabling perspective correction, image registration, and panorama stitching.

A homography is an invertible projective transformation that maps points on one plane to points on another while preserving straight lines. In computer vision, it describes how a flat surface appears from different viewpoints: a rectangular page photographed at an angle can be transformed into a front-facing rectangle. Homographies support image registration, perspective correction, and panorama stitching. "Projective transformation" is a synonym; the linguistic term "homograph" refers to words with identical spelling.

How the Homography Matrix Works#

A homography is represented by a 3 x 3 matrix, usually called H. It operates on homogeneous coordinates, which represent a two-dimensional point using three values. After matrix multiplication, dividing the first two output values by the third produces ordinary image coordinates.

Although H contains nine entries, it has eight independent parameters, or degrees of freedom: multiplying every entry by the same nonzero number leaves the mapping unchanged. This geometry of planar image mappings explains why four suitable point correspondences determine a homography.

An affine transformation is a restricted homography that preserves parallel lines. A general homography also represents perspective effects, including parallel edges converging toward a vanishing point. Distances and angles generally change. Michel Chasles developed the concept in his 1837 memoir on duality and homography.

How to Estimate a Homography#

Estimation begins with corresponding points: locations representing the same physical features in two images. These can be manually selected corners or matched keypoints obtained through feature extraction. Methods such as Scale-Invariant Feature Transform provide descriptors—numerical summaries of local appearance—for matching features across views.

The minimum is four point pairs, with no three points collinear in either set. The Direct Linear Transform (DLT) algorithm converts their coordinate relationships into a linear system. Additional correspondences improve estimation when measurements contain noise.

Incorrect matches require robust fitting. Random Sample Consensus (RANSAC) repeatedly fits candidate transformations to small subsets and identifies inliers: matches consistent with a candidate. Reprojection error measures the distance between a transformed source point and its observed destination. OpenCV's homography estimation API exposes this process through cv2.findHomography, returning both H and an inlier mask.

Apply a Homography in Python#

OpenCV provides separate operations for estimating a matrix and warping an image. Install the dependencies with pip install opencv-python numpy. This example maps the sample image's rectangular boundary into a trapezoid:

from urllib.request import urlopen

import cv2
import numpy as np

image_url = "https://ultralytics.com/images/bus.jpg"
with urlopen(image_url) as response:
    encoded = np.frombuffer(response.read(), dtype=np.uint8)
image = cv2.imdecode(encoded, cv2.IMREAD_COLOR)
height, width = image.shape[:2]

# Keep the same clockwise corner order in both arrays.
source = np.float32([[0, 0], [width - 1, 0], [width - 1, height - 1], [0, height - 1]])
target = np.float32([[120, 80], [width - 121, 80], [width - 1, height - 1], [0, height - 1]])
matrix, _ = cv2.findHomography(source, target)
warped = cv2.warpPerspective(image, matrix, (width, height))
cv2.imwrite("homography_result.jpg", warped)

The saved image shows perspective foreshortening: its upper edge is narrower while straight scene edges remain straight.

Where Homography Is Used#

  • Document Processing: Perspective rectification maps a flat document's four corners to a rectangle. This preprocessing step regularizes text layout before optical character recognition, following the same planar-warp geometry demonstrated in OpenCV's perspective-correction examples.
  • Panorama Stitching: Overlapping photographs are aligned before blending. Matthew Brown and David Lowe's Autostitch system uses matched local features to automate panorama construction, including identifying overlapping images from unordered collections.

In Ultralytics YOLO, RandomPerspective augmentation applies projective warps during training, transforming images and their associated bounding boxes, segmentation outlines, and keypoints together. Its perspective setting controls the perspective component of this data augmentation.

A single homography exactly relates ideal pinhole-camera views of one plane, or views captured by rotating a camera around its projection center. With camera translation and objects at different depths, parallax produces depth-dependent displacement. Aligning a background plane then leaves foreground objects displaced, creating doubled edges or ghosting in stitched images.

Camera calibration addresses lens distortion, whose curved image geometry requires correction before planar alignment. For general three-dimensional scenes, epipolar geometry constrains a corresponding point to a line; a homography predicts its location directly. In stereo vision, that remaining position along the epipolar line supplies disparity information used for depth estimation.

Explore solutions

Computer vision in sports

Computer vision in sports

Discover how Ultralytics YOLO powers sports vision with player and ball tracking, performance analytics, coaching insights, and athlete rehabilitation.
Learn more
Computer vision for aerial imagery

Computer vision for aerial imagery

Turn drone and aerial imagery into real-time insights for agriculture, aquaculture, conservation, and geospatial analysis with Ultralytics YOLO.
Learn more
Computer vision in aerospace

Computer vision in aerospace

Apply computer vision in aerospace with Ultralytics YOLO for aircraft inspection, runway FOD detection, airport ground operations, and composite defect detection.
Learn more
Computer vision in security

Computer vision in security

Protect every site with Ultralytics YOLO models. Vision AI powers perimeter monitoring, threat detection, license-plate recognition, and workplace safety.
Learn more
Computer vision in robotics

Computer vision in robotics

Power smarter machines with Ultralytics YOLO models. Vision AI in robotics drives autonomous navigation, perception, object tracking, and real-time control.
Learn more
Computer vision in logistics

Computer vision in logistics

Streamline logistics with Ultralytics YOLO models. Vision AI enables package inspection, sorting, vehicle tracking, and real-time warehouse safety monitoring.
Learn more
Computer vision in retail

Computer vision in retail

Reimagine retail with Ultralytics YOLO models. Vision AI powers inventory tracking, shelf monitoring, queue management, and smarter customer insights.
Learn more
Computer vision in healthcare

Computer vision in healthcare

Build healthcare solutions with Ultralytics YOLO models. Vision AI in healthcare powers faster medical imaging, smarter diagnostics, and patient monitoring.
Learn more
Computer vision in manufacturing

Computer vision in manufacturing

Optimize manufacturing with Ultralytics YOLO models. Vision AI drives quality control, defect detection, PPE compliance, and assembly line automation.
Learn more
Computer vision in automotive

Computer vision in automotive

Apply computer vision in automotive with Ultralytics YOLO models. Vision AI elevates road safety, driver assistance, and vehicle automation for smarter roads.
Learn more
Computer vision in agriculture

Computer vision in agriculture

Bring vision AI to smart agriculture with Ultralytics YOLO models. Power crop monitoring, livestock tracking, and precision farming for higher, smarter yields.
Learn more

Let's build the future of AI together!

Begin your journey with the future of machine learning