I have an marker detection system for AR at the moment, it detects markers in the scene and gives the transform matrix of the camera for each marker in the scene.
Let's say I have found 2 markers. I am trying to find the rotation matrix I will have to apply to one of the markers in order to get it to match the orientation of the other marker.
I figured it should be the same as computing the transform matrix of one marker to another and decomposing the transform to obtain the x,y,z euler rotation matrix but I cannot seem to get this to work. I am using C# with XNA.
In code:
Matrix marker1 = markerTransforms[0];
Matrix marker2 = markerTransforms[1];
Matrix relativeTransform = Matrix.Invert(marker1) * marker2;
Quaternion rotation;
Vector3 scale;
Vector3 translation;
relativeTransform.Decompose(out scale, out rotation, out translation);
Matrix rotationMatrix = Matrix.CreateFromQuaternion(rotation);
This above doesn't seem to work.
Another question would be how to extract out the x,y,z euler rotations from the rotation matrix?
EDIT:
I found a function to convert the quaternion to euler following x,y,z order here: http://forums.create.msdn.com/forums/p/4574/23763.aspx
Applying this to my code I got the following results:
The actual rotation should be: x:0 y:0 z:-0.52
I also noticed that the y and z changed a lot depending on how I positioned the camera.
The two transform matrices I obtain from the marker detector contain the orientation and translation of the camera relative to one of the markers as explained here: http://www.hitl.washington.edu/artoolkit/documentation/tutorialcamera.htm I have converted them to XNA format and I know them to work correctly as I can draw the corners onto the screen and it matches up with what the camera is seeing.
A rotation matrix and a translation matrix can be combined into a single matrix as follows, where the r's in the upper-left 3-by-3 matrix form a rotation and p, q and r form a translation vector. This matrix represents rotations followed by a translation.
Rotation matrices are square matrices, with real entries. More specifically, they can be characterized as orthogonal matrices with determinant 1; that is, a square matrix R is a rotation matrix if and only if RT = R−1 and det R = 1.
The solution I like the most is using quaternions. If you have one orientation described by q1, and other described by q2, you can get from one orientation to the other by
q1=q*q2
being q the rotation you are looking for.
q = q1 * (q2)^-1; q = q1 * conj(q2);
You just have to convert from rotation to quaternion and quaternion to rotation.
Just make sure that you normalize quaternions so the equivalences are true. In the pages I linked you have all the necessary formulas, explanations, even code in Java, c++. Really worth adding to favorites.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With