0

I'm having quite a hard time to render a .stl model with the same dimensions and proportions visualized in the picture of the real object. I put the real object at the centre of the view area and I would like to flip between the model and the real object (the real object is 9 times smaller than one in the model).

First, I calibrate the camera using OpenGL like in the tutorial, when the focus is optimal to the real object. After I get the cx, cy, fx and fy values, I create a perspective matrix with these values and I use the glMultMatrixd function. Finally, I resize the photo to be the same as my OpenGL window and compare the photo with the rendered model.

But I have some problems with the result:

  1. there is proportion distortion (wider than tall)
  2. there is a perspective distortion (The camera is perpendicular in relation to the object, so I should view only the top of the object, but it is showing the lateral of the object )
  3. the size is not compatible with the real object (a little smaller)

My relation matrix is below:

GLdouble perspMatrix[16] = { fx / cx,     0   ,   0    ,    0,
    0,   fy / cy ,   0    ,    0,
    0,     0   ,  -(znear + zfar) / (znear - zfar), 2 * zfar*znear / (zfar - znear),
    0,     0   ,  -1    ,   0 };

1 Answers1

0

OpenGL expects matrices in column-major memory order. The data structure you show above is in row-major memory order (the normal writing order, which OpenCV also uses as memory order) so you need to transpose it before passing to OpenGL.

You're also constructing a symmetrical viewing frustum (right==left==cx and top==bottom==cy), which assumes the optical axis (cx,cy) is at the centre of the camera's imager. Real cameras are never so perfect, which is why we have to calibrate them. You need an asymmetric frustum, using left==cx, right==(width-cx), etc; this guide should help.

Note that in OpenGL, +Y is up, and +Z is behind the camera (in OpenCV +Y is down, +Z is in front of the camera), so check the signs on your matrix.

Chungzuwalla
  • 1,038
  • 6
  • 17