Search code examples
c++opencvquaternions

Transforming Quaternion to Camera rotation matrix - OpenCV


I've been trying to warp a couple of photos with exact Quaternions for weeks without luck. The equation below doesn't seem to rotate the camera position as well as I'd expect from exact positions. Am I doing it right? Is there something I'm missing?

I understand that simply passing a Quaternion is not enough to stitch a photo, but it should be enough to align the photos?

Here's the formula:

double divmult = 2.0 / lsq;
double xx = divmult * x * x;
double yy = divmult * y * y;
double zz = divmult * z * z;

double wx = divmult * w * x;
double wy = divmult * w * y;
double wz = divmult * w * z;
double xy = divmult * x * y;
double xz = divmult * x * z;
double yz = divmult * y * z;

cameras_global[imageCounter].R = (Mat_<float>(3, 3) << ( 1 - yy - zz ),    -( xy - wz ),   -( xz + wy ),
                                  ( xy + wz ),        -( 1 -xx -zz ), -( yz - wx ),
                                  ( xz - wy ),        -( yz + wx ),   -( 1 -xx -yy )   );

Solution

  • OpenCV does not directly support quaternions. Rather than fixing your calculations, I would go with a simpler conversion. The closest thing it has is axis-angle vectors to represent 3D rotations (also named Rodrigues angles). I would convert to axis-angle, then multiply the axis by the angle to obtain Rodrigues angles. After that, you can use OpenCV's built-in functions, for instance cv::Rodrigues to convert to a 3x3 rotation matrix.