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.
Geometric Limits and Related Concepts#
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.










