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.
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:
- 1input: putative matches , iteration budget , inlier threshold
- 2best empty set
- 3repeat times
- 4sample 4 matches from at random; fit by the direct linear transform
- 5inliers matches in with reprojection error
- 6if inliersbest then best inliers
- 7refit on best by least squares
- 8return
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.
References
- Project repository
- Reference notes: Linear Algebra
╌╌ END ╌╌