Introduction
The "gold-standard" solution to the PnP consists in estimating the six parameters of the transformation cTw by minimizing the forward projection error using a Gauss-Newton approach. A complete derivation of this problem is given in [2].
Source code
The following source code that uses ViSP is also available in pose-gauss-newton-visp.cpp file. It allows to compute the pose of the camera from points.
#include <visp/vpColVector.h>
#include <visp/vpExponentialMap.h>
#include <visp/vpHomogeneousMatrix.h>
#include <visp/vpMatrix.h>
vpHomogeneousMatrix pose_gauss_newton(
#if VISP_VERSION_INT >= VP_VERSION_INT(2, 10, 0)
const
#endif
std::vector< vpColVector > &wX, const std::vector< vpColVector > &x, const vpHomogeneousMatrix &cTw)
{
int npoints = (int)wX.size();
vpMatrix J(2*npoints, 6), Jp;
vpColVector err, sd(2*npoints), s(2*npoints), xq(npoints*2), xn(npoints*2);
vpHomogeneousMatrix cTw_ = cTw;
double residual=0, residual_prev, lambda = 0.25;
for (int i = 0; i < x.size(); i ++) {
xn[i*2] = x[i][0];
xn[i*2+1] = x[i][1];
}
do {
for (int i = 0; i < npoints; i++) {
vpColVector cX = cTw_ * wX[i];
double Xi = cX[0];
double Yi = cX[1];
double Zi = cX[2];
double xi = Xi / Zi;
double yi = Yi / Zi;
xq[i*2] = xi;
xq[i*2+1] = yi;
J[i*2][0] = -1 / Zi;
J[i*2][1] = 0;
J[i*2][2] = xi / Zi;
J[i*2][3] = xi * yi;
J[i*2][4] = -(1 + xi * xi);
J[i*2][5] = yi;
J[i*2+1][0] = 0;
J[i*2+1][1] = -1 / Zi;
J[i*2+1][2] = yi / Zi;
J[i*2+1][3] = 1 + yi * yi;
J[i*2+1][4] = -xi * yi;
J[i*2+1][5] = -xi;
}
vpColVector e_q = xq - xn;
J.pseudoInverse(Jp);
vpColVector dq = -lambda * Jp * e_q;
cTw_ = vpExponentialMap::direct(dq).inverse() * cTw_;
residual_prev = residual;
residual = e_q.sumSquare();
} while (fabs(residual - residual_prev) > 0);
return cTw_;
}
int main()
{
int npoints = 4;
std::vector< vpColVector > wX(npoints);
std::vector< vpColVector > x(npoints);
for (int i = 0; i < npoints; i++) {
wX[i].resize(4);
x[i].resize(3);
}
vpHomogeneousMatrix cTw_truth(-0.1, 0.1, 0.5, vpMath::rad(5), vpMath::rad(0), vpMath::rad(45));
double L = 0.2;
wX[0][0] = -L; wX[0][1] = -L; wX[0][2] = 0; wX[0][3] = 1;
wX[1][0] = 2*L; wX[1][1] = -L; wX[1][2] = 0; wX[1][3] = 1;
wX[2][0] = L; wX[2][1] = L; wX[2][2] = 0; wX[2][3] = 1;
wX[3][0] = -L; wX[3][1] = L; wX[3][2] = 0; wX[3][3] = 1;
for(int i = 0; i < npoints; i++) {
vpColVector cX = cTw_truth * wX[i];
x[i][0] = cX[0] / cX[2];
x[i][1] = cX[1] / cX[2];
x[i][2] = 1;
}
vpHomogeneousMatrix cTw(-0.05, 0.05, 0.45, vpMath::rad(1), vpMath::rad(0), vpMath::rad(35));
cTw = pose_gauss_newton(wX, x, cTw);
std::cout << "cTw (ground truth):\n" << cTw_truth << std::endl;
std::cout << "cTw (from non linear method):\n" << cTw << std::endl;
return 0;
}
Source code explained
First of all we include ViSP headers that are requested to manipulate vectors and matrices and also to compute the exponential map.
#include <visp/vpColVector.h>
#include <visp/vpExponentialMap.h>
#include <visp/vpHomogeneousMatrix.h>
#include <visp/vpMatrix.h>
Then we introduce the function that does the pose estimation. It takes as input parameters the 3D coordinates of the points in the world frame and their normalized coordinates in the image plane. It returns the estimated pose as an homogeneous matrix transformation.
vpHomogeneousMatrix pose_gauss_newton(
#if VISP_VERSION_INT >= VP_VERSION_INT(2, 10, 0)
const
#endif
std::vector< vpColVector > &wX, const std::vector< vpColVector > &x, const vpHomogeneousMatrix &cTw)
The implementation of the iterative Gauss-Newton minimization process is done next. First, we create the variables used by the algorithm. Since the algorithm needs an initialization, we set an initial value in cTw not to far from the solution. Such an initialization could be done using Pose from Direct Linear Transform method of Pose from Dementhon's POSIT method approaches. Finally, the estimation is iterated.
int npoints = (int)wX.size();
vpMatrix J(2*npoints, 6), Jp;
vpColVector err, sd(2*npoints), s(2*npoints), xq(npoints*2), xn(npoints*2);
vpHomogeneousMatrix cTw_ = cTw;
double residual=0, residual_prev, lambda = 0.25;
for (int i = 0; i < x.size(); i ++) {
xn[i*2] = x[i][0];
xn[i*2+1] = x[i][1];
}
do {
for (int i = 0; i < npoints; i++) {
vpColVector cX = cTw_ * wX[i];
double Xi = cX[0];
double Yi = cX[1];
double Zi = cX[2];
double xi = Xi / Zi;
double yi = Yi / Zi;
xq[i*2] = xi;
xq[i*2+1] = yi;
J[i*2][0] = -1 / Zi;
J[i*2][1] = 0;
J[i*2][2] = xi / Zi;
J[i*2][3] = xi * yi;
J[i*2][4] = -(1 + xi * xi);
J[i*2][5] = yi;
J[i*2+1][0] = 0;
J[i*2+1][1] = -1 / Zi;
J[i*2+1][2] = yi / Zi;
J[i*2+1][3] = 1 + yi * yi;
J[i*2+1][4] = -xi * yi;
J[i*2+1][5] = -xi;
}
vpColVector e_q = xq - xn;
J.pseudoInverse(Jp);
vpColVector dq = -lambda * Jp * e_q;
cTw_ = vpExponentialMap::direct(dq).inverse() * cTw_;
residual_prev = residual;
residual = e_q.sumSquare();
} while (fabs(residual - residual_prev) > 0);
When the residual obtained between two successive iterations can be neglected, we can exit the while loop and return the estimated value of the pose.
Finally we define the main function in which we will initialize the input data before calling the previous function and computing the pose using non linear Gauss-Newton approach.
First in the main we create the data structures that will contain the 3D points coordinates wX in the world frame, their coordinates in the camera frame cX and their coordinates in the image plane x obtained after prerspective projection. Note here that at least 4 non coplanar points are requested to estimate the pose.
int npoints = 4;
std::vector< vpColVector > wX(npoints);
std::vector< vpColVector > x(npoints);
for (int i = 0; i < npoints; i++) {
wX[i].resize(4);
x[i].resize(3);
}
For our simulation we then initialize the input data from a ground truth pose cTw_truth. For each point we set in wX[i] the 3D coordinates in the world frame (wX, wY, wZ, 1) and compute in cX their 3D coordinates (cX, cY, cZ, 1) in the camera frame. Then in x[i] we update their coordinates (x, y) in the image plane, obtained by perspective projection.
vpHomogeneousMatrix cTw_truth(-0.1, 0.1, 0.5, vpMath::rad(5), vpMath::rad(0), vpMath::rad(45));
double L = 0.2;
wX[0][0] = -L; wX[0][1] = -L; wX[0][2] = 0; wX[0][3] = 1;
wX[1][0] = 2*L; wX[1][1] = -L; wX[1][2] = 0; wX[1][3] = 1;
wX[2][0] = L; wX[2][1] = L; wX[2][2] = 0; wX[2][3] = 1;
wX[3][0] = -L; wX[3][1] = L; wX[3][2] = 0; wX[3][3] = 1;
for(int i = 0; i < npoints; i++) {
vpColVector cX = cTw_truth * wX[i];
x[i][0] = cX[0] / cX[2];
x[i][1] = cX[1] / cX[2];
x[i][2] = 1;
}
From here we have initialized and . Since the non-linear minimization method requires a initial value of the pose to estimate we initialize not so far from the solution.
vpHomogeneousMatrix cTw(-0.05, 0.05, 0.45, vpMath::rad(1), vpMath::rad(0), vpMath::rad(35));
- Note
- In a real application this initialization has to be done using:
We are now ready to call the function that does the pose estimation.
cTw = pose_gauss_newton(wX, x, cTw);
Resulting pose estimation
If you run the previous code, it we produce the following result that shows that the estimated pose is equal to the ground truth one used to generate the input data:
cTw (ground truth):
0.7072945484 -0.706170438 0.03252282796 -0.1
0.706170438 0.7036809008 -0.078463382 0.1
0.03252282796 0.078463382 0.9963863524 0.5
0 0 0 1
cTw (from non linear method):
0.7072945484 -0.706170438 0.03252282796 -0.1
0.706170438 0.7036809008 -0.078463382 0.1
0.03252282796 0.078463382 0.9963863524 0.5
0 0 0 1