Computer Vision/Augmented Reality with Planar Homographies
10 / 67

02/2024Computer Vision

Augmented Reality with Planar Homographies

╌╌╌╌

Overlaying one image onto another in correct perspective is the classic augmented-reality effect. A book cover in a video becomes a screen: find where the plane sits in every frame, warp the overlay to match, and composite.

Two views of the same flat surface are related by a homography acting on homogeneous coordinates,

defined up to scale — eight degrees of freedom, so four point correspondences pin it down. Each correspondence contributes two linear constraints, and the direct linear transform stacks them into the system , solved by the smallest singular vector of . computeH builds row by row and reads off the last row of from the SVD; computeH_norm first conditions each point set (shift the centroid to the origin and scale so the mean distance to it is ), runs the DLT, then undoes the two similarity transforms, which keeps the linear system well-behaved.

A planar homography H maps the four corners of the source plane (the flat book cover) to their positions in the image plane, where the same surface appears in perspective. Four correspondences fix H.

matchPics detects FAST corners in both grayscale images and describes each with a BRIEF binary descriptor, then keeps a match only when the nearest descriptor beats the runner-up by a ratio test. Real matches are polluted with outliers, so computeH_ransac estimates the homography with RANSAC: fit candidate models to minimal random samples and keep the one with the most inliers:

Algorithm:Ransac-Homography(M,N,τ)\textsc{Ransac-Homography}(M, N, \tau)
  1. 1
    input: putative matches MM, iteration budget NN, inlier threshold τ\tau
  2. 2
    best \gets empty set
  3. 3
    repeat NN times
  4. 4
    sample 4 matches from MM at random; fit HH by the direct linear transform
  5. 5
    inliers \gets matches in MM with reprojection error xHx<τ\lVert \mathbf{x}' - H\mathbf{x} \rVert < \tau
  6. 6
    if \lvertinliers>\rvert > \lvertbest\rvert then best \gets inliers
  7. 7
    refit HH on best by least squares
  8. 8
    return HH

The implementation samples correspondences per iteration over a budget of iterations, counts a point as an inlier when its reprojection error falls under pixels, and refits the homography on the full inlier set at the end. A sample of points is all-inlier with probability at inlier rate , so a principled budget follows from for a target confidence .

compositeH warps the overlay and an all-ones mask by with warpPerspective, then blends the warped template over the scene wherever the mask lands — dropping a new cover onto the book in cv_desk.png. Frames for the moving version come from loadVid, and an extra-credit panorama script reuses the same match-and-warp pipeline to stitch overlapping photos. The full write-up is available as a PDF report.

Every frame repeats the same move: the flat overlay is warped by that frame's homography H into the exact perspective quadrilateral where the book cover sits in the video, then composited wherever the warped mask lands. Re-estimating H per frame keeps the overlay pinned to the cover as it moves.

References

  1. Project repository
  2. Reference notes: Linear Algebra

╌╌ END ╌╌