Skip to content

Instantly share code, notes, and snippets.

@davegreenwood
Created July 25, 2019 06:56
Show Gist options
  • Save davegreenwood/3a32d779f81f08dce32f3bb423672191 to your computer and use it in GitHub Desktop.
Save davegreenwood/3a32d779f81f08dce32f3bb423672191 to your computer and use it in GitHub Desktop.
OpenCV to OpenGL projection
How to get the proper OpenGL projection matrix from the OpenCV camera calibration values?
For the simple common case where the OpenCV camera matrix has the form:
|fx 0 cx|
|0 fy cy|
|0 0 1|
The corresponding OpenGL projection matrix can be computed like this:
m[0][0] = 2.0 * fx / width;
m[0][1] = 0.0;
m[0][2] = 0.0;
m[0][3] = 0.0;
m[1][0] = 0.0;
m[1][1] = -2.0 * fy / height;
m[1][2] = 0.0;
m[1][3] = 0.0;
m[2][0] = 1.0 - 2.0 * cx / width;
m[2][1] = 2.0 * cy / height - 1.0;
m[2][2] = (zfar + znear) / (znear - zfar);
m[2][3] = -1.0;
m[3][0] = 0.0;
m[3][1] = 0.0;
m[3][2] = 2.0 * zfar * znear / (znear - zfar);
m[3][3] = 0.0;
Where height and width are the size of the captured image ; znear and zfar are the clipping value for the projection.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment