[PnP] EPnP: An Efficient Solution to the PnP Problem
SLAMTechnical NotesSLAMVIOSensor Fusion
Following the DLT solution to PnP, this article derives another common PnP solver: EPnP. EPnP is also used by ORB-SLAM2. The derivation below includes an Eigen-based implementation.
EPnP has O(n) complexity and is efficient for PnP problems with many correspondences.
It represents every 3D point using four control points. Optimization only concerns those control points; at most four singular vectors are used in solving Mx=0, yielding both speed and good accuracy.
1. Derivation
1.1 Homogeneous barycentric coordinates
The notation follows the original paper. For n correspondences, let pi, i=1,…,n, be a non-homogeneous 3D point. Superscripts c and w indicate camera and world coordinates.
EPnP introduces four control points cj, j=1,…,4. Every world point is represented as
piw=j=1∑4αijcjw,j=1∑4αij=1.(1)
The coefficients αij are homogeneous barycentric (hb) coordinates. Equivalently,
In other words, a point’s homogeneous coordinates are a linear combination of the control points’ homogeneous coordinates. Let [R∣t] be the camera pose. A control point in camera coordinates is
The hb coordinates are identical for the same 3D point in world and camera frames. Thus αij can be computed in the world frame and treated as known in the camera frame.
1.2 Choosing control points
Any control points are valid if C in Equation (2) is invertible. For numerical stability, select the first control point as the centroid:
c1w=n1i=1∑npiw.(5)
Construct the centered matrix
A=(p1w−c1w)T⋮(pnw−c1w)T.(6)
Let λ1,λ2,λ3 and v1,v2,v3 be eigenvalues and eigenvectors of ATA. The remaining controls are
ck+1w=c1w+nλkvk,k=1,2,3.(7)
This is the point-cloud centroid plus its three principal directions; it is closely related to PCA.
At this point, all world-frame controls cjw and all hb coordinates αij are known. Recovering cjc allows recovery of every pic and then [R∣t].
The hb coordinates, intrinsics, and image point coordinates are known. The unknowns are the xjc,yjc,zjc coordinates of four controls: 12 values in total. All points form
Mx=0,(10)
where M is 2n×12. The solution lies in the null space:
x=i=1∑Nβivi.(11)
Here vi are right singular vectors associated with zero singular values. They can be obtained from the eigenvectors of MTM. Regardless of correspondence count, MTM remains 12×12, and forming it costs O(n).
The question is how to determine βi. The original paper considers N=1,2,3,4 according to the data, controls, focal length, and noise.
Case N=1
For x=βv, distances between controls are invariant between coordinate frames:
∥cic−cjc∥2=∥ciw−cjw∥2.(12)
Let v[i] denote the three entries of v associated with control i. Then
Introduce β11=β12, β22=β22, and β12=β1β2 to turn it into a linear system. Six control-point pairs form
Lβ=ρ,β=[β11β12β22]T,(16)
with L∈R6×3. The resulting candidates are disambiguated by requiring each camera-frame control point to have positive depth.
Cases N=3 and N=4
The N=3 construction is the same, with
β=[β11,β12,β13,β22,β23,β33]T,L∈R6×6.
For N=4 there are ten intermediate variables but only six equations, so this direct construction is underdetermined. The paper introduces additional constraints and relinearization. OpenCV uses an approximate solution rather than directly following the four-case derivation; this implementation follows the OpenCV approach.
Optimizing β
Starting from the candidates, optimize β=[β1,β2,β3,β4]T by minimizing discrepancies between control-point distances:
Stack the six pairwise Jacobians and residuals as J and r. Gauss–Newton solves
JTJδβ=−JTr,β←β+δβ.(22–23)
This is a standard least-squares optimization.
1.4 Recovering [R∣t]
Once β is known, all camera-frame controls and consequently all pic are known. This produces a matched 3D–3D alignment problem.
Compute centroids:
pcc=n1i=1∑npic,pcw=n1i=1∑npiw.
Center the point sets, yielding Pc and Pw.
Compute rotation:
W=PcTPw,[U,Σ,V]=SVD(W),R=UVT.
If det(R)<0, correct the sign of the relevant singular-vector row.
Compute translation:
t=pcc−Rpcw.
There can be four pose candidates from the four β cases; select the one with the smallest reprojection error.
2. Implementation and experiments
2.1 Implementation
The solver is implemented with Eigen and differs slightly from OpenCV’s EPnP implementation. Source: ydsf16/PnP_Solver.
2.2 Experiments
The experiment matches the preceding DLT article. The custom EPnP implementation (MY_EPnP) and OpenCV’s (CV_EPnP) were inserted into the ORB-SLAM2 front end and compared with Motion Only BA (MOB) and DLT.
EPnP approaches MOB accuracy and produces smooth trajectories. It is visibly steadier than DLT; one plausible reason is that DLT uses one singular vector for the null-space solution, whereas EPnP considers up to four and then optimizes them.
Translation and rotation errors are clearly lower than DLT. On an Intel i7-8700 running Ubuntu 16.04 with 200–300 point pairs:
MY_EPnP: 0.0852±0.0153ms
CV_EPnP: 0.1202±0.0198ms
The two implementations are close in accuracy, stability, and runtime. MY_EPnP is slightly faster; CV_EPnP is slightly more accurate.
References
Lepetit, V.; Moreno-Noguer, F.; Fua, P. EPnP: Efficient Perspective-n-Point Camera Pose Estimation. International Journal of Computer Vision, 2009, 81, 155–166.