0

I have a problem that has been puzling me for the last few days. I have a camera pose obtained with Opencv that is right handed (X-right, Y-up, Z-back) and I would like to visualize in Unity (X-right, Y-up, Z-forward) but I cannot really manage to get it right.

I tried to do it using either quaternions or matrices and it should be just mirroring the Z axis and set the rotation of the transform of the camera in Unity to the computed transformation however I cannot get the right conversion.

With quaternions I tried to mirror by negating the Z and W term and I achieved a coordinate system (X-right, Y-down, Z-forward), it makes sense but it is not what I want to achieve. With matrices I think I should multiply my right hand camera by an identity matrix with the element [2,2] set to -1, however I don't get what I want.

I am definitely missing something, probably something really stupid I forgot :)

Has anybody a suggestion?

CoffeeRobot
  • 41
  • 1
  • 5
  • Possible duplicate of [OpenCV rotation (Rodrigues) and translation vectors for positioning 3D object in Unity3D](http://stackoverflow.com/questions/36561593/opencv-rotation-rodrigues-and-translation-vectors-for-positioning-3d-object-in) – ilke444 Jan 24 '17 at 04:00

1 Answers1

4

A Quaternion can be thought of as a rotation around an axis a = (ax, ay, az) by an angle theta

qx = ax * sin(theta/2)
qy = ay * sin(theta/2)
qz = az * sin(theta/2)
qw = cos(theta/2)

In a right-handed coordinate system a rotation of theta will be counter-clockwise, while in a left-handed coordinate system a rotation of theta will be clockwise (depending on your point-of-view, of course).

So to get your quaternion from a right-handed system to Unity's Left-Handed system you have to account for two factors:

  1. The Z-Axis is negated
  2. The direction of rotation is flipped from CCW to CW

We first factor is accounted for by negating the qz component of the quaternion. The second factor is accounted for by flipping the axis of rotation (rotating by 90 degrees around 1,0,0 is the inverse of rotating 90 degrees around -1,0,0).

If your original right-handed quaternion is q and your left-handed quaternion is q'That means you end up with:

q'=(-qx, -qy, qz, qw)

Additional Note

  • Quaternions don't inherently have a handedness. A quaterion q applies equally well in RH or LH coordinate systems. However, when you apply the quaternion to a spatial vector, the resulting transformation takes on the handedness of the vector's space.
Ricky R
  • 236
  • 1
  • 3
  • Thanks for the exahustivre explanation, I tried the transformation today but what I got is a coordinate system with X-right, Y-down and Z-back. From this coordinate system I achieve what I want by rotating 180 degrees on the X axis however if I apply more rotations on the Z axis of the object I get inverted rotations. – CoffeeRobot Jan 24 '17 at 19:32
  • It is certainly possible that I got something wrong. The fact that you end up with a rotation that is 180 degrees off makes me think that the openCV camera's rotation may be given in a coordinate system with X-Right, Y-Down, and Z-Forward. (See: https://i.stack.imgur.com/4iFEV.png) That would put the look direction of the open-cv camera facing 180 degrees opposite and should change the conversion to `q'=(-qx, qy, -qz, qw)`. – Ricky R Jan 24 '17 at 20:07