Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get rotation matrix between two two transform matrices (XNA)

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:

enter image description here

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.

like image 237
Jkh2 Avatar asked Jun 03 '12 16:06

Jkh2


People also ask

How do you combine rotation and translation matrix?

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.

How do you show a matrix is a rotation matrix?

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.


1 Answers

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.

like image 84
Jav_Rock Avatar answered Sep 27 '22 19:09

Jav_Rock