I managed to acquire camera's intrinsic and extrinsic parameters using OpenCV, thus I have fx, fy, cx and cy. And I also have the screen / image's width and height.
But how do I create an OpenGL perspective projection matrix from these parameters?
glFrustrum shows how to create projection matrix, given Z near, Z far and the image width and height. But how do I include focal points and camera centers in this matrix?
This how or when more precisely the perspective divide is performed when a point is multiplied by a projection matrix. It is important you understand this idea. Then divide all coordinates by w' to set the point's homogeneous coordinates back to Cartesian coordinates: x′=x′=xw′=−z,y′=y′=yw′=−z,z′=z′=−zw′=−z=1.
In computer vision a camera matrix or (camera) projection matrix is a. matrix which describes the mapping of a pinhole camera from 3D points in the world to 2D points in an image.
You can check the following links
Kyle Simek's explanation
My explanation
Here is the code to obtain the OpenGL projection matrix equivalent to a computer vision camera with camera matrix K=[fx, s, cx; 0, fy, cy; 0, 0, 1]
and image size [W, H]
:
glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
glLoadIdentity(); // Reset The Projection Matrix
GLdouble perspMatrix[16]={2*fx/W,0,0,0,2*s/W,2*fy/H,0,0,2*(cx/W)-1,2*(cy/H)-1,(zmax+zmin)/(zmax-zmin),1,0,0,2*zmax*zmin/(zmin-zmax),0};
glMultMatrixd(perspMatrix);
NB: zmin
and zmax
represent the near and far Z clipping planes. This formulation assumes that the OpenGL world coordinate frame is chosen as follows:
The OpenGL camera is assumed to be located at the origin, looking towards positive Z axis, with a down vector collinear and towards the positive Y axis.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With