Skip to content

Lesson 5 of 9 · 27m

Homography and Perspective Measurement

Estimate planar transformations, warp images, map points between views, and recognize when a homography is the wrong geometric model.

A homography is one of the most useful ideas in applied vision: points on a plane in one view can be mapped to points in another view with a 3x3 matrix. It powers document scanning, sports-field overlays, planar tracking, warehouse floor measurement, and image stitching. It is also routinely misapplied to scenes that are not planar, producing confidence wrapped around bad geometry.

The model is planar — respect it

A homography is appropriate when the points lie on the same physical plane or when the camera rotates about its optical center. It is not appropriate for arbitrary 3D scenes with depth variation. A building facade can fit; a cluttered desk cannot. Before estimating anything, ask what plane you are measuring.

Correspondences must be accurate and distributed

Minimum input is four point pairs, but production estimation needs more:

src = np.float32([[84, 112], [612, 104], [608, 492], [91, 486]])
dst = np.float32([[0, 0], [800, 0], [800, 600], [0, 600]])
H, mask = cv2.findHomography(src, dst, cv2.RANSAC, 3.0)

Points should surround the region of interest. If all correspondences cluster in one area, the matrix can interpolate well there and extrapolate terribly everywhere else. Subpixel corner positions matter when measurements matter.

Warping images is easy; interpreting them is harder

warped = cv2.warpPerspective(img, H, (800, 600))

warpPerspective converts the source image into the destination plane. Interpolation, border behavior, and output size all affect measurement and downstream processing. Use WARP_INVERSE_MAP only when you truly need inverse mapping; accidental inverse warps are a classic source of mirrored or blank outputs.

Transform coordinates explicitly

A homography maps points, not bounding boxes as independent corners if you care about perspective-correct shape. OpenCV handles homogeneous conversion for you:

pts = np.float32([[120, 130], [580, 460]]).reshape(-1, 1, 2)
out = cv2.perspectiveTransform(pts, H)

For measurements, calibrate the destination units. If the destination image represents a 1000 mm by 700 mm bench surface, map image coordinates into millimeters through a second scale transform or include real-world dimensions directly in dst.

Estimate uncertainty in practice

OpenCV gives you a matrix, not a confidence interval. Build confidence operationally:

inlier ratio
mean and max reprojection error
point spread across the plane
repeatability across frames
residual motion of known fixed points

Reprojection error is especially useful:

projected = cv2.perspectiveTransform(src.reshape(-1, 1, 2), H)
err = np.linalg.norm(projected - dst.reshape(-1, 1, 2), axis=2)
print(float(err.mean()), float(err.max()))

A low mean with one huge maximum can mean a bad correspondence survived. A uniformly mediocre error can mean lens distortion or non-planarity.

Lens distortion breaks planar fantasy

Wide-angle cameras bend straight lines. If you measure near image edges without undistortion, the planar model absorbs lens error and becomes viewpoint-dependent. Calibrate first when precision matters, or restrict measurement to a low-distortion region.

Field notes

  • Four points are a mathematical minimum, not an engineering target.
  • Do not use a homography across independently moving objects.
  • Validate the transform using known points not used in estimation.
  • RANSAC threshold is measured in pixels and depends on resolution.
  • If output is black, check matrix direction, output size, dtype, and whether the source actually maps into the destination canvas.

Was this lesson helpful?

Sign in to rate