This post shows how to decompose a 3×3 rotation matrix into the 3 elementary Euler angles, sometimes referred to as yaw/pitch/roll, and going the other way around. The technique I’m presenting is based off http://planning.cs.uiuc.edu/node102.html.

If you have ever seen Wikipedia’s entry on Rotation matrix or Euler angles, you will have no doubt been swamped to your neck with maths equations all over the place, depending how tall your neck is. It turns out there is no single correct answer on defining a rotation matrix in terms of Euler angles. There are a few ways to accomplish it and all of them are valid. But that’s okay, I’ll just show one way, which should be adequate for most applications. I won’t go into any maths derivation, aiming to keep this post implementation friendly.

# Decomposing a rotation matrix

Given a 3×3 rotation matrix

The 3 Euler angles are

Here atan2 is the same arc tangent function, with quadrant checking, you typically find in C or Matlab.

# Composing a rotation matrix

Given 3 Euler angles , the rotation matrix is calculated as follows:

# Note on angle ranges

The Euler angles returned when doing a decomposition will be in the following ranges:

If you keep your angles within these ranges, then you will get the same angles on decomposition. Conversely, if your angles are outside these ranges you will still get the correct rotation matrix, but the decomposed values will be different to your original angles.

Code

Download rotation_matrix_demo.m

The Octave/Matlab script contains the decompose/compose function and a demo on using it. It picks random Euler angles, makes a rotation matrix, decomposes it and verifies the results are the same. An example output

octave:1> rotation_matrix_demo Picking random Euler angles (radians) x = -2.6337 y = -0.47158 z = -1.2795 Rotation matrix is: R = 0.25581 -0.77351 0.57986 -0.85333 -0.46255 -0.24057 0.45429 -0.43327 -0.77839 Decomposing R x2 = -2.6337 y2 = -0.47158 z2 = -1.2795 err = 0 Results are correct!

Hi Nghia,

I believe you misplaced number 1 in the Y rotation matrix. Number 1 should be at the centre of Y. As a result, your derivation of the angles is not generally correct. From the RHS of equation 3.42 of the link you cited (http://planning.cs.uiuc.edu/node102.html), one can easily obtain yaw, pitch and roll angles as follows:

\theta_x = arctan(r_{3,2}/r_{3,3})

\theta_y = -arcsin(r_{3,1})

\theta_z = arctan(r_{2,1}/r_{1,1})

By the way, you other posts and source codes on computer vision are amazing. They are really useful to me.

Cheers,

Chuong

Hi,

Well spotted!

I came to the same result for theta_y, using arcsin instead of atan2. I wasn’t sure if there was any ‘gotchas’ at the time, so I left it using the original equation I cited.

Glad you found something useful on the site

Hi Nghia,

I think this approach has one singularity. If theta_y is +-90 degrees, it will come that r_{1,1}, r_{2,1}, r_{3,2} and r_{3,3} will evaluate to zero due to the cos(theta_y) term. So you end up to atan(0) which, will return always 0, independent of theta_x and theta_z.

If you try this in your matlab code, make sure that you do not have numerical errors on r_{1,1}, r_{2,1}, r_{3,2} and r_{3,3}, after generating the rotation matrix.

Do you have the solution for that case also?

Cheers,

Andre

I’ve tried putting 90 degrees in my demo program, at line 6

y = pi;

x,z are still random between -180,180 degrees. The matrix changes accordingly. The returned values however in the x,z axis are out by 180 degrees.

Try editing the demo code and see if you get the same results as me.

I’ve tried to put in line 6:

y = pi*0.5;

and after the line “R = compose_rotation(x,y,z)”, the lines:

R(1,1) = 0;

R(2,1) = 0;

R(3,2) = 0;

R(3,3) = 0

to kill numerical errors of the composition process. If you read these values before, you will get values very close to zero (e-17 in my machine). They actually should be zero, due to the “cos(pi*0.5)” term in that specific case.

You can let x and z random.

Ooops my bad, wasn’t thinking straight. You’re very right.

I guess the only one around it is to explicitly check for the +- 90 degree situation and use a different rotation composition. Probably one of the many reasons why people hate using matrices to represent rotation.

Indeed. By the way, thank you for answering and congrats for the blog.

Hi Nghia!

Thank you very much for your article. It helped me very much. For about 2 days I couldn’t solve my problem, I’ve messed up in these rotation-conventions. But when I’ve used your formulas, it worked fine!

It’s important to note that the Euler angles described above are applied in the order theta_X -> theta_Y -> theta_Z relative to the world axis, due to the non-commutativity of 3D rotation.

thank you nghiaho, you excelent, i see much your paper. your’s result very good.

Hello: I have a visualization program which expects 3 values for rotation (x, y, z) between 0 and 360 degrees. Now, my fusion library (reading data from sensors) provides either euler angles or quaternions. I didin’t know how to use quaternions to feed the visualization application, so I ‘m working with euler angles.

The problem is I see euler angles values range from (0 to 360), (-180 to 180), and (-90 to 90). For the first case is Ok. For the second, I can add 180 and I’m done (range 0 to 360). Now for the last one, I see if doing a 360 movement with the board (sensors), the values are changing from -90 to +90 and +90 to -90 again, so in this case I’m not sure what to do. Adding 90 will end in (0 to 180) range.

How can I convert the euler angles ranges to the right (0 to 360) value range the visualization application requires?

Many thanks in advance.

Gus

If you want to convert all your angles to [0,360] correctly then I would simply do:

if(angle < 0)

angle += 360;

You are aware I hope, that what you’re describing here are actually the Tait-Bryan angles (or the xyz convention) not the Euler angles (cf. Goldstein p. 154 and appendix)? It really doen’t matter which one you use as long as you are precise about which definition you use (which you are in a sense since you give a link to the maths calculations).

Thanks for the clarification! I didn’t actually know that, my maths background is a bit limited

Hey Nghia,

i also wanna thank you for your helpful tutorial.

I am working with opencv and visp.

I tried to take an angle alpha in radians, compose the matrix for pitch roll and yaw

then multiplied them like Z*Y*X and then tried to decompose the matrix again to

get the angles for x,y and z. After that i compared the angles as degrees.

It seems that the new calculated angles always are 180°-alpha.

What am i doing wrong?

Here is my sourcecode:

#include

#include

#include

#include

#include

#include

#include

using namespace std;

using namespace cv;

int main() {

for (double angleOld = -M_PI; angleOld <= M_PI; angleOld+=(2*M_PI)/360) {

double a = angleOld;

// ——————————————————————-

// composing rotation matrix

// ——————————————————————-

Mat xRotationOld = *(Mat_(3, 3) << 1, 0, 0,

0, cos(a), -sin(a),

0, sin(a), cos(a) );

Mat yRotationOld = *(Mat_(3, 3) << cos(a), 0, sin(a),

0, 1, 0,

-sin(a),0, cos(a) );

Mat zRotationOld = *(Mat_(3, 3) << cos(a), -sin(a),0,

sin(a), cos(a), 0,

0, 0, 1 );

Mat rotationMat = zRotationOld * yRotationOld * xRotationOld;

// ——————————————————————-

// decomposing rotation matrix

// ——————————————————————-

/*

rotationMat:

(m_00 m_01 m_02)

(m_10 m_11 m_12)

(m_20 m_21 m_22)

*/

double m_00 = rotationMat.at(0, 0);

double m_10 = rotationMat.at(1, 0);

double m_20 = rotationMat.at(2, 0);

double m_21 = rotationMat.at(2, 1);

double m_22 = rotationMat.at(2, 2);

// calculate the euler angles

double xRotation = atan2(m_21, m_22); // range: (-pi, pi)

double yRotation = atan2(- m_20, sqrt((m_21*m_21) + (m_22*m_22))); // range: (-(pi/2), pi/2)

double zRotation = atan2(m_10, m_00); // range: (-pi, pi)

// convert radians to degree

double oldAngleDegree = (angleOld * 180) / M_PI;

double xDegree = (xRotation * 180) / M_PI;

double yDegree = (yRotation * 180) / M_PI;

double zDegree = (zRotation * 180) / M_PI;

// check wether angles are the same as before

if (xRotation != angleOld || yRotation != angleOld || zRotation != angleOld) {

cout << oldAngleDegree << "° wrong calculated " << endl;

cout << "x angle = ";

cout << xDegree << "° :: ";

if (xRotation != angleOld) {

cout << "wrong" << endl;

}else {

cout << "correct" << endl;

}

cout << "y angle = ";

cout << yDegree << "° :: ";

if (yRotation != angleOld) {

cout << "wrong" << endl;

}else {

cout << "correct" << endl;

}

cout << "z angle = ";

cout << zDegree << "° :: ";

if (zRotation != angleOld) {

cout << "wrong" << endl;

}else {

cout << "correct" << endl;

}

}

}

}

I hope anyone can help me with that. i guess it is some simple thing that i am overseeing.

Much thx in advance