Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

OpenGL Camera to OpenCV Projection matrix


I am trying to retrieve an OpenGL camera's parameters and use them for image processing.

I have my 4x4 Modelview from which I extract the camera's position and orientation as such:

Matrix3 R = glModelview.to3x3()
R.rotate(1, 0, 0, M_PI) // flip z axis 
Vector3 camera_pos(glModelview[0][3], glModelview[1][3], glModelview[2][3]); // world position in camera's coordinates

I retrieve the focal length and principal point from the Projection matrix as follow:

glP = glProjectionMatrix;
double w = d_imageSize.x();
double h = d_imageSize.y();

double fx = 0.5 * w * glP[0][0];
double fy = 0.5 * h * (1.0 + glP[1][1]);
double cx = 0.5 * w * (1.0 - glP[0][3]);
double cy = 0.5 * h * (1.0 + glP[1][3]);

I now have my intrinsic and extrinsic parameters, and just compose the projection matrix. Though it seems that I am missing something, as depending on the size of my OpenGL window (viewport) I get different results. How could I integrate the viewport in my projection matrix?

Thanks in advance :)