Perspective-n-Point

Perspective-n-Point is the problem of estimating the pose of a calibrated camera given a set of $n$ 3D points in the world and their corresponding 2D projections in the image. The camera pose consists of 6 degrees-of-freedom (DOF) which are made up of the rotation (roll, pitch, and yaw) and 3D translation of the camera with respect to the world. This problem originates from camera calibration and has many applications in computer vision and other areas, including 3D pose estimation, robotics and augmented reality. A commonly used solution to the problem exists for $n = 3$ called P3P, and many solutions are available for the general case of $n ≥ 3$. A solution for $n = 2$ exists if feature orientations are available at the two points. Implementations of these solutions are also available in open source software.

Definition
Given a set of $n$ 3D points in a world reference frame and their corresponding 2D image projections as well as the calibrated intrinsic camera parameters, determine the 6 DOF pose of the camera in the form of its rotation and translation with respect to the world. This follows the perspective projection model for cameras:

s\,p_c = K\,[\,R\, |\, T\, ]\, p_w $$.

where $$\textstyle p_w = \begin{bmatrix}x & y & z & 1\end{bmatrix}^T$$ is the homogeneous world point, $$\textstyle p_c = \begin{bmatrix}u & v & 1\end{bmatrix}^T$$ is the corresponding homogeneous image point, $$\textstyle K$$ is the matrix of intrinsic camera parameters, (where $$\textstyle f_x$$ and $$f_y$$ are the scaled focal lengths, $$\textstyle \gamma$$ is the skew parameter which is sometimes assumed to be 0, and $$\textstyle (u_0,\, v_0)$$ is the principal point), $$\textstyle s$$ is a scale factor for the image point, and $$\textstyle R$$ and $$\textstyle T$$ are the desired 3D rotation and 3D translation of the camera (extrinsic parameters) that are being calculated. This leads to the following equation for the model:

s\begin{bmatrix}u\\v\\1\end{bmatrix} = \begin{bmatrix} f_x & \gamma & u_0\\ 0 & f_y & v_0\\ 0 & 0 & 1 \end{bmatrix}\begin{bmatrix} r_{11} & r_{12} & r_{13} & t_{1}\\ r_{21} & r_{22} & r_{23} & t_{2}\\ r_{31} & r_{32} & r_{33} & t_{3}\\ \end{bmatrix} \begin{bmatrix}x\\y\\z\\1\end{bmatrix} $$.

Assumptions and Data Characteristics
There are a few preliminary aspects of the problem that are common to all solutions of PnP. The assumption made in most solutions is that the camera is already calibrated. Thus, its intrinsic properties are already known, such as the focal length, principal image point, skew parameter, and other parameters. Some methods, such as UPnP. or the Direct Linear Transform (DLT) applied to the projection model, are exceptions to this assumption as they estimate these intrinsic parameters as well as the extrinsic parameters which make up the pose of the camera that the original PnP problem is trying to find.

For each solution to PnP, the chosen point correspondences cannot be colinear. In addition, PnP can have multiple solutions, and choosing a particular solution would require post-processing of the solution set. RANSAC is also commonly used with a PnP method to make the solution robust to outliers in the set of point correspondences. P3P methods assume that the data is noise free, most PnP methods assume Gaussian noise on the inlier set.

Methods
This following section describes two common methods that can be used to solve the PnP problem that are also readily available in open source software and how RANSAC can be used to deal with outliers in the data set.

P3P
When $n = 3$, the PnP problem is in its minimal form of P3P and can be solved with three point correspondences. However, with just three point correspondences, P3P yields up to four real, geometrically feasible solutions. For low noise levels a fourth correspondence can be used to remove ambiguity. The setup for the problem is as follows.

Let P be the center of projection for the camera, A, B, and C be 3D world points with corresponding images points u, v, and w. Let X = |PA|, Y = |PB|, Z = |PC|, $$\alpha = \angle BPC$$, $$\beta = \angle APC$$, $$\gamma = \angle APB$$, $$p = 2\cos\alpha$$, $$q = 2\cos\beta$$, $$r = 2\cos\gamma$$, $$a' = |AB|$$, $$b' = |BC|$$, $$c' = |AC|$$. This forms triangles PBC, PAC, and PAB from which we obtain a sufficient equation system for P3P:



\begin{cases} Y^2 + Z^2 - YZp - b'^2 &= 0\\ Z^2 + X^2 - XZq - c'^2 &= 0\\ X^2 + Y^2 - XYr - a'^2 &= 0\\ \end{cases} $$.

Solving the P3P system results in up to four geometrically feasible real solutions for $R$ and $T$. The oldest published solution dates to 1841. A recent algorithm for solving the problem as well as a solution classification for it is given in the 2003 IEEE Transactions on Pattern Analysis and Machine Intelligence paper by Gao, et al. An open source implementation of Gao's P3P solver can be found in OpenCV's calib3d module in the solvePnP function. Several faster and more accurate versions have been published since, including Lambda Twist P3P which achieved state of the art performance in 2018 with a 50 fold increase in speed and a 400 fold decrease in numerical failures. Lambdatwist is available as open source in OpenMVG and at https://github.com/midjji/pnp.

EPnP
Efficient PnP (EPnP) is a method developed by Lepetit, et al. in their 2008 International Journal of Computer Vision paper that solves the general problem of PnP for $n ≥ 4$. This method is based on the notion that each of the n points (which are called reference points) can be expressed as a weighted sum of four virtual control points. Thus, the coordinates of these control points become the unknowns of the problem. It is from these control points that the final pose of the camera is solved for.

As an overview of the process, first note that each of the $n$ reference points in the world frame, $$p^w_i$$, and their corresponding image points, $$p^c_i$$, are weighted sums of the four controls points, $$c^w_j$$ and $$c^c_j$$ respectively, and the weights are normalized per reference point as shown below. All points are expressed in homogeneous form.
 * $$p^w_i = \sum^4_{j=1}{\alpha_{ij}c^w_j}$$
 * $$p^c_i = \sum^4_{j=1}{\alpha_{ij}c^c_j}$$
 * $$\sum^4_{j=1}{\alpha_{ij}} = 1$$

From this, the derivation of the image reference points becomes
 * $$s_i\,p^{img}_i = K\sum^4_{j=1}{\alpha_{ij}c^c_j}$$.

Where $$p^{img}_i$$ is the image reference points with pixel coordinate $$\begin{bmatrix}u_i & v_i & 1\end{bmatrix}^T$$. The homogeneous image control point has the form $$\textstyle c^c_j = \begin{bmatrix}x^c_j & y^c_j & z^c_j\end{bmatrix}^T$$. Rearranging the image reference point equation yields the following two linear equations for each reference point:

\sum^4_{j=1}{\alpha_{ij}f_xx^c_j + \alpha_{ij}(u_0 - u_i)z^c_j} = 0 $$

\sum^4_{j=1}{\alpha_{ij}f_yy^c_j + \alpha_{ij}(v_0 - v_i)z^c_j} = 0 $$. Using these two equations for each of the $n$ reference points, the system $$\textstyle Mx = 0$$ can be formed where $$\textstyle x = \begin{bmatrix}c^{c^T}_1 & c^{c^T}_2 & c^{c^T}_3 & c^{c^T}_4\end{bmatrix}^T$$. The solution for the control points exists in the null space of $M$ and is expressed as
 * $$x = \sum^N_{i=1}{\beta_iv_i}$$

where $$N$$ is the number of null singular values in $$M$$ and each $$v_i$$ is the corresponding right singular vector of $$M$$. $$N$$ can range from 0 to 4. After calculating the initial coefficients $$\beta_i$$, the Gauss-Newton algorithm is used to refine them. The $R$ and $T$ matrices that minimize the reprojection error of the world reference points, $$p^w_i$$, and their corresponding actual image points $$p^c_i$$, are then calculated.

This solution has $$O(n)$$ complexity and works in the general case of PnP for both planar and non-planar control points. Open source software implementations of this method can be found in OpenCV's Camera Calibration and 3D Reconstruction module in the solvePnP function as well as from the code published by Lepetit, et al. at their website, CVLAB at EPFL.

This method is not robust against outliers and generally compares poorly to RANSAC P3P followed by nonlinear refinement.

SQPnP
SQPnP was described by Terzakis and Lourakis in an ECCV 2020 paper. It is a non-minimal, non-polynomial solver which casts PnP as a non-linear quadratic program. SQPnP identifies regions in the parameter space of 3D rotations (i.e., the 8-sphere) that contain unique minima with guarantees that at least one of them is the global one. Each regional minimum is computed with sequential quadratic programming that is initiated at nearest orthogonal approximation matrices.

SQPnP has similar or even higher accuracy compared to state of the art polynomial solvers, is globally optimal and computationally very efficient, being practically linear in the number of supplied points $n$. A C++ implementation is, which has also been ported to OpenCV and included in the Camera Calibration and 3D Reconstruction module (SolvePnP function).

Using RANSAC
PnP is prone to errors if there are outliers in the set of point correspondences. Thus, RANSAC can be used in conjunction with existing solutions to make the final solution for the camera pose more robust to outliers. An open source implementation of PnP methods with RANSAC can be found in OpenCV's Camera Calibration and 3D Reconstruction module in the solvePnPRansac function.