我正在尝试将相机外部矩阵和内部矩阵转换为用于AR的openGL上的模型视图和投影矩阵。
通过相机校准估计相机固有矩阵,我已经计算了外在矩阵(我知道世界坐标(CAD模型)和相机坐标之间的对应关系)。
仅用于检查我是否正确计算了相机外部矩阵,我使用以下方法在图像上增加了CAD模型:
P = KE
(u, v, w) = P(X, Y, Z)
其中P:投影矩阵,K:内在矩阵,E:外在矩阵, (u / w,v / w):图像坐标,(X,Y,Z):CAD模型的世界坐标。 结果如下所示,结果良好。我确认了内在矩阵和外在矩阵是正确的。
然而,我没有将它们改为openGL。(我想在图像上使用OpenGL绘制CAD模型)。使用下面的代码,对象是在窗口外(通过改变openGL的投影矩阵,我检查了代码可以绘制对象。但它在错误的位置绘制对象)。
extern Matrix3f IntrinsicMatrix;
double fx = IntrinsicMatrix(0, 0);
double fy = IntrinsicMatrix(1, 1);
double cx = IntrinsicMatrix(0, 2);
double cy = IntrinsicMatrix(1, 2);
double alpha = IntrinsicMatrix(0, 1);
extern Mat InputImage;
double w = InputImage.cols;
double h = InputImage.rows;
double Znear = 0.1;
double Zfar = 500000;
extern MatrixXf ExtrinsicMatrix;
Matrix4f Extrinsic4f, Temp;
for (unsigned int i = 0; i < 3; i++)
for (unsigned int j = 0; j < 4; j++)
Extrinsic4f(i, j) = ExtrinsicMatrix(i, j);
Extrinsic4f(3, 0) = 0.0f;
Extrinsic4f(3, 1) = 0.0f;
Extrinsic4f(3, 2) = 0.0f;
Extrinsic4f(3, 3) = 1.0f;
for (unsigned int i = 0; i < 4; i++)
for (unsigned int j = 0; j < 4; j++)
Temp(i, j) = 0.0f;
Temp(0, 0) = 1.0f;
Temp(1, 1) = -1.0f;
Temp(2, 2) = -1.0f;
Temp(3, 3) = 1.0f;
Extrinsic4f = Temp*Extrinsic4f;
Matrix4f glViewMatrix = Extrinsic4f.transpose();
GLfloat model[16] = {
glViewMatrix(0, 0), glViewMatrix(0, 1), glViewMatrix(0, 2), glViewMatrix(0, 3),
glViewMatrix(1, 0), glViewMatrix(1, 1), glViewMatrix(1, 2), glViewMatrix(1, 3),
glViewMatrix(2, 0), glViewMatrix(2, 1), glViewMatrix(2, 2), glViewMatrix(2, 3),
glViewMatrix(3, 0), glViewMatrix(3, 1), glViewMatrix(3, 2), glViewMatrix(3, 3),
};
glMatrixMode(GL_MODELVIEW);
glLoadMatrixf(model);
glMatrixMode(GL_PROJECTION);
GLfloat perspMatrix[16]={
2*fx/w, 0.0 , (w-2*cx)/w, 0,
0, -2*fy/h, (-h+2*cy)/h, 0,
0, 0, (-Zfar-Znear)/(Zfar-Znear), -2*Zfar*Znear/(Zfar-Znear),
0, 0, -1, 0};
glLoadMatrixf(perspMatrix);
glColor3f(1.f, 1.f, 1.f);
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0); glVertex3f(-1.0, -1.0, 0.0);
glTexCoord2f(1.0, 0.0); glVertex3f(1.0, -1.0, 0.0);
glTexCoord2f(1.0, 1.0); glVertex3f(1.0, 1.0, 0.0);
glTexCoord2f(0.0, 1.0); glVertex3f(-1.0, 1.0, 0.0);
glEnd();
glDisable(GL_TEXTURE_2D);
double fov_y = 2 * atan(height / 2 / fy) * 180 / CV_PI;
gluPerspective(fov_y, (double)width / height, Znear, Zfar); // 39
glViewport(0, 0, width, height);
draw3Dobject();
glutSwapBuffers();
我有什么改变吗?