我使用openCV函数projectPoints()来旋转,平移和投影一组3D点和solvePnp()来找到这个旋转和平移.当镜头失真系数全为零但是否则失败时,这很有效.完全失败只需要很少的失真:
distCoeffs << 0.0, 0.01, 0.0, 0.0, 0.0;
Run Code Online (Sandbox Code Playgroud)
代码如下:
#include <iostream>
#include "opencv.hpp"
using namespace std;
using namespace cv;
#define DEG2RAD (3.1415293/180.0)
#define RAD2DEG (1.0/DEG2RAD)
int main() {
const int npoints = 10; // number of points
// extrinsic
const Point3f tvec(10, 20, 30);
Point3f rvec(3, 5, 7);
cout << "Finding extrinsic parameters (PnP)" << endl;
cout<<"Test transformations: ";
cout<<"Rotation: "<<rvec<<"; translation: "<<tvec<<endl;
rvec*=DEG2RAD;
// intrinsic
Mat_ <double>cameraMatrix(3, 3);
cameraMatrix << 300., 0., 200., 0, 300., 100., 0., 0., 1.; …Run Code Online (Sandbox Code Playgroud)