NOTE / 3/17/2019

[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)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\mathbf M\mathbf x=0, yielding both speed and good accuracy.

1. Derivation

1.1 Homogeneous barycentric coordinates

The notation follows the original paper. For nn correspondences, let pi\mathbf p_i, i=1,…,ni=1,\ldots,n, be a non-homogeneous 3D point. Superscripts cc and ww indicate camera and world coordinates.

EPnP introduces four control points cj\mathbf c_j, j=1,…,4j=1,\ldots,4. Every world point is represented as

piw=∑j=14αijcjw,∑j=14αij=1.(1)\mathbf p_i^w=\sum_{j=1}^4\alpha_{ij}\mathbf c_j^w, \qquad \sum_{j=1}^4\alpha_{ij}=1. \tag{1}

The coefficients αij\alpha_{ij} are homogeneous barycentric (hb) coordinates. Equivalently,

[piw1]=[c1wc2wc3wc4w1111]⏟C[αi1αi2αi3αi4].(2)\begin{bmatrix}\mathbf p_i^w\\1\end{bmatrix} = \underbrace{\begin{bmatrix} \mathbf c_1^w&\mathbf c_2^w&\mathbf c_3^w&\mathbf c_4^w\\ 1&1&1&1 \end{bmatrix}}_{\mathbf C} \begin{bmatrix}\alpha_{i1}\\\alpha_{i2}\\\alpha_{i3}\\\alpha_{i4}\end{bmatrix}. \tag{2}

In other words, a point’s homogeneous coordinates are a linear combination of the control points’ homogeneous coordinates. Let [R∣t][\mathbf R\mid\mathbf t] be the camera pose. A control point in camera coordinates is

cjc=[R∣t][cjw1].(3)\mathbf c_j^c = [\mathbf R\mid\mathbf t] \begin{bmatrix}\mathbf c_j^w\\1\end{bmatrix}. \tag{3}

Therefore

pic=[R∣t][piw1]=[R∣t][∑j=14αijcjw∑j=14αij]=∑j=14αijcjc.(4)\begin{aligned} \mathbf p_i^c &= [\mathbf R\mid\mathbf t] \begin{bmatrix}\mathbf p_i^w\\1\end{bmatrix}\\ &= [\mathbf R\mid\mathbf t] \begin{bmatrix} \sum_{j=1}^4\alpha_{ij}\mathbf c_j^w\\ \sum_{j=1}^4\alpha_{ij} \end{bmatrix} = \sum_{j=1}^4\alpha_{ij}\mathbf c_j^c. \end{aligned} \tag{4}

The hb coordinates are identical for the same 3D point in world and camera frames. Thus αij\alpha_{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\mathbf C in Equation (2) is invertible. For numerical stability, select the first control point as the centroid:

c1w=1n∑i=1npiw.(5)\mathbf c_1^w=\frac1n\sum_{i=1}^n\mathbf p_i^w. \tag{5}

Construct the centered matrix

A=[(p1w−c1w)T⋮(pnw−c1w)T].(6)\mathbf A = \begin{bmatrix} (\mathbf p_1^w-\mathbf c_1^w)^{\mathsf T}\\ \vdots\\ (\mathbf p_n^w-\mathbf c_1^w)^{\mathsf T} \end{bmatrix}. \tag{6}

Let λ1,λ2,λ3\lambda_1,\lambda_2,\lambda_3 and v1,v2,v3\mathbf v_1,\mathbf v_2,\mathbf v_3 be eigenvalues and eigenvectors of ATA\mathbf A^{\mathsf T}\mathbf A. The remaining controls are

ck+1w=c1w+λknvk,k=1,2,3.(7)\mathbf c_{k+1}^w = \mathbf c_1^w+\sqrt{\frac{\lambda_k}{n}}\mathbf v_k, \qquad k=1,2,3. \tag{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\mathbf c_j^w and all hb coordinates αij\alpha_{ij} are known. Recovering cjc\mathbf c_j^c allows recovery of every pic\mathbf p_i^c and then [R∣t][\mathbf R\mid\mathbf t].

1.3 Camera-frame control points

Analytical solution

The projection equation is

ωi[ui1]=Kpic=K∑j=14αijcjc,K=[fx0cx0fycy001].(8)\omega_i \begin{bmatrix}\mathbf u_i\\1\end{bmatrix} = \mathbf K\mathbf p_i^c = \mathbf K\sum_{j=1}^4\alpha_{ij}\mathbf c_j^c, \qquad \mathbf K= \begin{bmatrix}f_x&0&c_x\\0&f_y&c_y\\0&0&1\end{bmatrix}. \tag{8}

Removing the final row gives two equations per correspondence:

∑j=14(αijfxxjc+αij(cx−ui)zjc)=0,∑j=14(αijfyyjc+αij(cy−vi)zjc)=0.(9)\begin{aligned} \sum_{j=1}^4 \left(\alpha_{ij}f_xx_j^c+\alpha_{ij}(c_x-u_i)z_j^c\right)&=0,\\ \sum_{j=1}^4 \left(\alpha_{ij}f_yy_j^c+\alpha_{ij}(c_y-v_i)z_j^c\right)&=0. \end{aligned} \tag{9}

The hb coordinates, intrinsics, and image point coordinates are known. The unknowns are the xjc,yjc,zjcx_j^c,y_j^c,z_j^c coordinates of four controls: 12 values in total. All points form

Mx=0,(10)\mathbf M\mathbf x=0, \tag{10}

where M\mathbf M is 2n×122n\times12. The solution lies in the null space:

x=∑i=1Nβivi.(11)\mathbf x=\sum_{i=1}^N\beta_i\mathbf v_i. \tag{11}

Here vi\mathbf v_i are right singular vectors associated with zero singular values. They can be obtained from the eigenvectors of MTM\mathbf M^{\mathsf T}\mathbf M. Regardless of correspondence count, MTM\mathbf M^{\mathsf T}\mathbf M remains 12×1212\times12, and forming it costs O(n)O(n).

The question is how to determine βi\beta_i. The original paper considers N=1,2,3,4N=1,2,3,4 according to the data, controls, focal length, and noise.

Case N=1N=1

For x=βv\mathbf x=\beta\mathbf v, distances between controls are invariant between coordinate frames:

∥cic−cjc∥2=∥ciw−cjw∥2.(12)\lVert\mathbf c_i^c-\mathbf c_j^c\rVert^2 = \lVert\mathbf c_i^w-\mathbf c_j^w\rVert^2. \tag{12}

Let v[i]\mathbf v^{[i]} denote the three entries of v\mathbf v associated with control ii. Then

∥βv[i]−βv[j]∥2=∥ciw−cjw∥2.(13)\left\lVert \beta\mathbf v^{[i]}-\beta\mathbf v^{[j]} \right\rVert^2 = \lVert\mathbf c_i^w-\mathbf c_j^w\rVert^2. \tag{13}

The four controls give the closed-form estimate

β=∑{i,j}∈[1:4]∥v[i]−v[j]∥∥ciw−cjw∥∑{i,j}∈[1:4]∥v[i]−v[j]∥2.(14)\beta= \frac{ \sum_{\{i,j\}\in[1:4]} \lVert\mathbf v^{[i]}-\mathbf v^{[j]}\rVert \lVert\mathbf c_i^w-\mathbf c_j^w\rVert }{ \sum_{\{i,j\}\in[1:4]} \lVert\mathbf v^{[i]}-\mathbf v^{[j]}\rVert^2 }. \tag{14}

Case N=2N=2

Now x=β1v1+β2v2\mathbf x=\beta_1\mathbf v_1+\beta_2\mathbf v_2. Define

S1=v1[i]−v1[j],S2=v2[i]−v2[j],c=∥ciw−cjw∥2.\mathbf S_1=\mathbf v_1^{[i]}-\mathbf v_1^{[j]}, \qquad \mathbf S_2=\mathbf v_2^{[i]}-\mathbf v_2^{[j]}, \qquad c=\lVert\mathbf c_i^w-\mathbf c_j^w\rVert^2.

Distance invariance becomes

β12S1TS1+2β1β2S1TS2+β22S2TS2=c.(15)\beta_1^2\mathbf S_1^{\mathsf T}\mathbf S_1 +2\beta_1\beta_2\mathbf S_1^{\mathsf T}\mathbf S_2 +\beta_2^2\mathbf S_2^{\mathsf T}\mathbf S_2=c. \tag{15}

Introduce β11=β12\beta_{11}=\beta_1^2, β22=β22\beta_{22}=\beta_2^2, and β12=β1β2\beta_{12}=\beta_1\beta_2 to turn it into a linear system. Six control-point pairs form

Lβ=ρ,β=[β11β12β22]T,(16)\mathbf L\boldsymbol\beta=\boldsymbol\rho, \qquad \boldsymbol\beta= \begin{bmatrix}\beta_{11}&\beta_{12}&\beta_{22}\end{bmatrix}^{\mathsf T}, \tag{16}

with L∈R6×3\mathbf L\in\mathbb R^{6\times3}. The resulting candidates are disambiguated by requiring each camera-frame control point to have positive depth.

Cases N=3N=3 and N=4N=4

The N=3N=3 construction is the same, with

β=[β11,β12,β13,β22,β23,β33]T,L∈R6×6.\boldsymbol\beta= [\beta_{11},\beta_{12},\beta_{13},\beta_{22},\beta_{23},\beta_{33}]^{\mathsf T}, \qquad \mathbf L\in\mathbb R^{6\times6}.

For N=4N=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 β\boldsymbol\beta

Starting from the candidates, optimize β=[β1,β2,β3,β4]T\boldsymbol\beta=[\beta_1,\beta_2,\beta_3,\beta_4]^{\mathsf T} by minimizing discrepancies between control-point distances:

β∗=arg⁡min⁡β∑i<j∥Error⁡ij(β)∥2,\boldsymbol\beta^* = \arg\min_{\boldsymbol\beta} \sum_{i<j} \left\lVert \operatorname{Error}_{ij}(\boldsymbol\beta) \right\rVert^2, Error⁡ij(β)=∥cic−cjc∥2−∥ciw−cjw∥2.(19)\operatorname{Error}_{ij}(\boldsymbol\beta) = \lVert\mathbf c_i^c-\mathbf c_j^c\rVert^2 - \lVert\mathbf c_i^w-\mathbf c_j^w\rVert^2. \tag{19}

For each pair, let Sk=vk[i]−vk[j]\mathbf S_k=\mathbf v_k^{[i]}-\mathbf v_k^{[j]}. Then

∥cic−cjc∥2=∥∑k=14βkSk∥2.\lVert\mathbf c_i^c-\mathbf c_j^c\rVert^2 = \left\lVert\sum_{k=1}^4\beta_k\mathbf S_k\right\rVert^2.

The Jacobian of this residual is

Jij=2[S1T∑kβkSkS2T∑kβkSkS3T∑kβkSkS4T∑kβkSk].\mathbf J_{ij} = 2\begin{bmatrix} \mathbf S_1^{\mathsf T}\sum_k\beta_k\mathbf S_k& \mathbf S_2^{\mathsf T}\sum_k\beta_k\mathbf S_k& \mathbf S_3^{\mathsf T}\sum_k\beta_k\mathbf S_k& \mathbf S_4^{\mathsf T}\sum_k\beta_k\mathbf S_k \end{bmatrix}.

Stack the six pairwise Jacobians and residuals as J\mathbf J and r\mathbf r. Gauss–Newton solves

JTJ δβ=−JTr,β←β+δβ.(22–23)\mathbf J^{\mathsf T}\mathbf J\,\delta\boldsymbol\beta = -\mathbf J^{\mathsf T}\mathbf r, \qquad \boldsymbol\beta\leftarrow\boldsymbol\beta+\delta\boldsymbol\beta. \tag{22--23}

This is a standard least-squares optimization.

1.4 Recovering [R∣t][\mathbf R\mid\mathbf t]

Once β\boldsymbol\beta is known, all camera-frame controls and consequently all pic\mathbf p_i^c are known. This produces a matched 3D–3D alignment problem.

  1. Compute centroids:

    pcc=1n∑i=1npic,pcw=1n∑i=1npiw.\mathbf p_c^c=\frac1n\sum_{i=1}^n\mathbf p_i^c, \qquad \mathbf p_c^w=\frac1n\sum_{i=1}^n\mathbf p_i^w.
  2. Center the point sets, yielding Pc\mathbf P^c and Pw\mathbf P^w.

  3. Compute rotation:

    W=PcTPw,[U,Σ,V]=SVD⁡(W),R=UVT.\mathbf W={\mathbf P^c}^{\mathsf T}\mathbf P^w, \qquad [\mathbf U,\mathbf\Sigma,\mathbf V]=\operatorname{SVD}(\mathbf W), \qquad \mathbf R=\mathbf U\mathbf V^{\mathsf T}.

    If det⁡(R)<0\det(\mathbf R)<0, correct the sign of the relevant singular-vector row.

  4. Compute translation:

    t=pcc−Rpcw.\mathbf t=\mathbf p_c^c-\mathbf R\mathbf p_c^w.

There can be four pose candidates from the four β\boldsymbol\beta 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.0153 ms0.0852\pm0.0153\,\mathrm{ms}
  • CV_EPnP: 0.1202±0.0198 ms0.1202\pm0.0198\,\mathrm{ms}

The two implementations are close in accuracy, stability, and runtime. MY_EPnP is slightly faster; CV_EPnP is slightly more accurate.

Trajectory comparison

Error comparison

Translation error

Rotation error

References

  1. Lepetit, V.; Moreno-Noguer, F.; Fua, P. EPnP: Efficient Perspective-n-Point Camera Pose Estimation. International Journal of Computer Vision, 2009, 81, 155–166.
  2. In-depth EPnP algorithm
  3. PnP algorithm introduction and code analysis
  4. OpenCV EPnP implementation