Search code examples
c++opencvcameraaugmented-realityogre

OpenCV camera intrinsics matrix to Ogre projection matrix for Augmented reality


I've written a simple Augmented Reality application using Ogre + OpenCV. All is working, but I need to match Ogre's virtual camera to my real camera

I don't know camera parameters (it's a cheap webcam and vendor is not providing them). I'm able to get camera intrinsic with cvCalibrateCamera2 (after some cvFindChessboardCorners as described here) and I save them with cvSave.

cvCalibrateCamera2(objectPoints_, imagePoints_, pointCounts_,
        imageSize, cameraMatrix, distortionCoeffs_, NULL,
        NULL, CV_CALIB_FIX_ASPECT_RATIO );
cvSave(INTRINSIC_XML, intrinsicMatrix_);

AR application loads the intrinsic matrix:

CvMat* intrinsic = (CvMat*) cvLoad(INTRINSIC_XML);

So intrinsic->data.db is a pointer to a 9 length array of double (3x3 matrix)
How can I translate this to a Ogre::Matrix4 such I can use it with setCustomProjectionMatrix.

I've found something in Ogre's Forum but this is not working. I guess some other step is needed between the 3x3 matrix found by OpenCV and the 4x4 matrix needed by Ogre::Camera.

I hope there's some way of doing this with intrinsics given by cvCalibrateCamera2. If none, I'll need to take a ruler and a protractor and get parameters (roughly) by hand.

This is the 3x3 "intrinsics" matrix:

836.391     0.000   460.430 
  0.000   836.391   281.440
  0.000     0.000     1.000

Solution

  • Create the projection matrix as follows.

    mat[0,0] = fx

    mat[1,1] = fy

    mat[2,2] = +- .01

    mat[3,2] = +- 1

    mat[3,3] = 0

    where fx = intrinsic [0] / imageSize.Width;

    fy = intrinsic [1*3 + 1] / imageSize.Height;

    Try combinations of negative and positive values for mat[2,2] and mat[3,2].

    Typically both will have negative values. Other parameters can be ignored