Get camera matrix from OpenGL

dimart.sp

I render a 3D mesh model using OpenGL with perspective camera – gluPerspective(fov, aspect, near, far).

Then I use rendered image in a computer vision algorithm.

At some point that algorithm requires camera matrix K (along with several vertices on the model and their corresponding projections) in order to estimate camera position: rotation matrix R and translation vector t. I can estimate R and t by using any algorithm which solves Perspective-n-Point problem.

I construct K from the OpenGL projection matrix (see how here)

K = [fX, 0, pX | 0, fY, pY | 0, 0, 1]

If I want to project a model point 'by hand' I can compute:

X_proj = K*(R*X_model + t) x_pixel = X_proj[1] / X_proj[3] y_pixel = X_proj[2] / X_proj[3]

Anyway, I pass this camera matrix in a PnP algorithm and it works just fine.


But then I had to change perspective projection to orthographic one. As far as I understand when using orthographic projection the camera matrix becomes:

K = [1, 0, 0 | 0, 1, 0 | 0, 0, 0]

So I changed gluPerspective to glOrtho. Following the same way I constructed K from OpenGL projection matrix, and it turned out that fX and fY are not ones but 0.0037371. Is this a scaled orthographic projection or what?

Moreover, in order to project model vertices 'by hand' I managed to do the following:

X_proj = K*(R*X_model + t) x_pixel = X_proj[1] + width / 2 y_pixel = X_proj[2] + height / 2

Which is not what I expected (that plus width and hight divided by 2 seems strange...). I tried to pass this camera matrix to POSIT algorithm to estimate R and t, and it doesn't converge. :(

So here are my questions:

  1. How to get orthographic camera matrix from OpenGL?
  2. If the way I did it is correct then is it true orthographic? Why POSIT doesn't work?
Kamyar Infinity

Orthographic projection will not use the depth to scale down farther points. Though, it will scale the points to fit inside the NDC which means it will scale the values to fit inside the range [-1,1]. This matrix from Wikipedia shows what this means:

Orthographic Projection

So, it is correct to have numbers other than 1.

For your way of computing by hand, I believe it's not scaling back to screen coordinates and that makes it wrong. As I said, the output of projection matrices will be in the range [-1,1], and if you want to get the pixel coordinates, I believe you should do something similar to this:

X_proj = K*(R*X_model + t)
x_pixel = X_proj[1]*width/2 + width / 2
y_pixel = X_proj[2]*height/2 + height / 2

Anyway, I think you'd be better if you used modern OpenGL with libraries like GLM. In this case, you have the exact projection matrices used at hand.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Get camera matrix from OpenGL

From Dev

Get position, rotation and scale from matrix in OpenGL

From Dev

Get the camera position in opengl

From Dev

Get the camera position in opengl

From Dev

OpenGL – Get distance to farthest face from camera instead of closest face

From Dev

How to get current camera position from view matrix?

From Dev

How to get current camera position from view matrix?

From Dev

OpenGL ES Screen Projection Matrix and Camera Projection Matrix

From Dev

OpenGL - Can't get camera to work

From Dev

Intrinsic Camera Matrix to OpenGL Projection. Unexpected Scaling Factor

From Dev

how to get camera projection matrix using the matlab?

From Dev

how to get camera projection matrix using the matlab?

From Dev

determine camera rotation and translation matrix from essential matrix

From Dev

Get the LatLngBouds from a camera position

From Dev

C# OpenGL - Trying to get the light to move with the camera using shaders

From Dev

Capture video from camera on Raspberry Pi and filter in OpenGL before encoding

From Dev

From How far can the camera look an object in OpenGl in android?

From Dev

How do I get the projection matrix of a camera after stereo rectification?

From Dev

Get the maximum permutation matrix from logical matrix

From Dev

Get Image matrix from histogram matrix in matlab

From Dev

How to get an Adjacency matrix from count matrix

From Dev

OpenGL camera/direction vector

From Dev

Camera Space OpenGL Tutorial

From Dev

OpenGL JOGL camera perspective

From Dev

OpenGL camera/direction vector

From Dev

OpenGL application rotate Camera

From Dev

Eigen - get a matrix from a map?

From Dev

get matrix of vectors from a vector

From Dev

Eigen - get a matrix from a map?

Related Related

  1. 1

    Get camera matrix from OpenGL

  2. 2

    Get position, rotation and scale from matrix in OpenGL

  3. 3

    Get the camera position in opengl

  4. 4

    Get the camera position in opengl

  5. 5

    OpenGL – Get distance to farthest face from camera instead of closest face

  6. 6

    How to get current camera position from view matrix?

  7. 7

    How to get current camera position from view matrix?

  8. 8

    OpenGL ES Screen Projection Matrix and Camera Projection Matrix

  9. 9

    OpenGL - Can't get camera to work

  10. 10

    Intrinsic Camera Matrix to OpenGL Projection. Unexpected Scaling Factor

  11. 11

    how to get camera projection matrix using the matlab?

  12. 12

    how to get camera projection matrix using the matlab?

  13. 13

    determine camera rotation and translation matrix from essential matrix

  14. 14

    Get the LatLngBouds from a camera position

  15. 15

    C# OpenGL - Trying to get the light to move with the camera using shaders

  16. 16

    Capture video from camera on Raspberry Pi and filter in OpenGL before encoding

  17. 17

    From How far can the camera look an object in OpenGl in android?

  18. 18

    How do I get the projection matrix of a camera after stereo rectification?

  19. 19

    Get the maximum permutation matrix from logical matrix

  20. 20

    Get Image matrix from histogram matrix in matlab

  21. 21

    How to get an Adjacency matrix from count matrix

  22. 22

    OpenGL camera/direction vector

  23. 23

    Camera Space OpenGL Tutorial

  24. 24

    OpenGL JOGL camera perspective

  25. 25

    OpenGL camera/direction vector

  26. 26

    OpenGL application rotate Camera

  27. 27

    Eigen - get a matrix from a map?

  28. 28

    get matrix of vectors from a vector

  29. 29

    Eigen - get a matrix from a map?

HotTag

Archive