Lulumoy Studio

Math guide

Perspective transform (4 points)

Homography: the mapping between two views of a plane taken from different points. It preserves straight lines but not parallelism (parallel lines may converge to a vanishing point). Useful to straighten documents, façades or boards photographed at an angle.

[uvw]=[h11h12h13h21h22h23h31h321][xy1],x′=uw,y′=vw
Homogeneous coordinates: dividing by w produces the perspective effect. H is defined up to a scale factor, hence h33 = 1 and 8 unknowns remain.
{xk′(h31xk+h32yk+1)=h11xk+h12yk+h13yk′(h31xk+h32yk+1)=h21xk+h22yk+h23k=1,…,4
Four point pairs give eight linear equations in the hij; cv::getPerspectiveTransform solves the 8×8 system. The solution is unique if no three points are collinear at source or destination, which the program checks.

As with the affine transform, cv::warpPerspective computes each destination pixel with the inverse transform H−1 and interpolates it. The result keeps the image size.

Parameters

ParameterMeaning
InterpolationHow values at non-integer positions are computed.
Empty areasValue of pixels that fall outside the original image.

See also: Interpolation and borders, Affine transform (3 points)

References

  1. OpenCV 4.x documentation — Geometric Image Transformations (warpAffine, warpPerspective, getAffineTransform, getPerspectiveTransform, getRotationMatrix2D, interpolation flags). docs.opencv.org/4.x/da/d54/group__imgproc__transform.html
  2. R. Hartley, A. Zisserman. Multiple View Geometry in Computer Vision, 2nd ed. Cambridge University Press, 2004.
  3. R. Szeliski. Computer Vision: Algorithms and Applications, 2nd ed. Springer, 2022. szeliski.org/Book/