• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer

Learn OpenCV

OpenCV, PyTorch, Keras, Tensorflow examples and tutorials

  • Home
  • Getting Started
    • Installation
    • PyTorch
    • Keras & Tensorflow
    • Resource Guide
  • Courses
    • Opencv Courses
    • CV4Faces (Old)
  • Resources
  • AI Consulting
  • About

Rotation Matrix To Euler Angles

Satya Mallick
June 4, 2016 11 Comments
Tutorial

June 4, 2016 By 11 Comments

In this post I will share code for converting a 3×3 rotation matrix to Euler angles and vice-versa.

3D rotations matrices can make your head spin. I know it is a bad pun but truth can sometimes be very punny!

A rotation matrix has three degrees of freedom, and mathematicians have exercised their creative freedom to represent a 3D rotation in every imaginable way — using three numbers, using four numbers, using a 3×3 matrix. And there are a ton of different ways of representing a rotation as three numbers and a few ways to represent it as 4 numbers.

This article contains code you can try out. If you want to download test scripts that use the functions included in this article, please subscribe to our newsletter by clicking here.

For example, rotation in 3D can be represented as three angles that specify three rotations applied successively to the X, Y and Z axes. But you could also represent the same rotation as three angles applied successively to Z, Y and X axes. These angles are called Euler angles or Tait–Bryan angles. In the original Euler angle formulation, a rotation is described by successive rotations about the Z, X and again Z axes ( or for that matter Y-X-Y, or Z-Y-Z ). When the rotation is specified as rotations about three distinct axes ( e.g. X-Y-Z ) they should be called Tait–Bryan angles, but the popular term is still Euler angles and so we are going to call them Euler angles as well.

There are six possible ways you can describe rotation using Tait–Bryan angles — X-Y-Z, X-Z-Y, Y-Z-X, Y-X-Z, Z-X-Y, Z-Y-X. Now you are thinking, the choice is easy. Let’s just choose X-Y-Z. Right ? Wrong. The industry standard is Z-Y-X because that corresponds to yaw, pitch and roll.

There are additional ambiguities while defining rotation matrices. E.g. Given a point \mathbf{(x, y, z)}, you can think of this point as a row vector [ x, y, z] or a column vector [ x, y, z]^T. If you use a row vector, you have to post-multiply the 3×3 rotation matrix and if you use the column vector representation you have to pre-multiply the rotation matrix to rotate the point. These two rotation matrices are not the same ( they are the transpose of each other ).

My point is that there is no standard way to convert a rotation matrix to Euler angles. So, I decided to be (almost) consistent with the MATLAB implementation of rotm2euler.m. The only difference is that they return the Euler angles with the rotation about z first and x last. My code returns x first.

Euler Angles to Rotation Matrices

The easiest way to think about 3D rotation is the axis-angle form. Any arbitrary rotation can be defined by an axis of rotation and an angle the describes the amount of rotation. Let’s say you want to rotate a point or a reference frame about the x axis by angle \theta_x. The rotation matrix corresponding to this rotation is given by

    \[ \mathbf{R_x} = \begin{bmatrix}     1       & 0 & 0 \\     0 & \cos(\theta_x) & -\sin(\theta_x)\\     0 & \sin(\theta_x) & (\cos\theta_x) \end{bmatrix} \]

Rotations by \theta_y and \theta_z about the y and z axes can be written as

    \[ \mathbf{R_y} = \begin{bmatrix}     \cos(\theta_y)       & 0 & \sin(\theta_y) \\     0 & 1 & 0\\     -\sin(\theta_y) & 0 & (\cos\theta_y) \end{bmatrix} \]

    \[ \mathbf{R_z} = \begin{bmatrix}     \cos(\theta_z) & -\sin(\theta_z) & 0 \\     \sin(\theta_z) & (\cos\theta_z) & 0 \\      0       & 0 & 1 \\ \end{bmatrix} \]

A rotation \mathbf{R} about any arbitrary axis can be written in terms of successive rotations about the Z, Y, and finally X axes using the matrix multiplication shown below.

    \[ \mathbf{R} = \mathbf{R_z R_y R_x} \]

In this formulation \theta_x, \theta_y and \theta_z are the Euler angles. Given these three angles you can easily find the rotation matrix by first finding \mathbf{R_x}, \mathbf{R_y} and \mathbf{R_z} and then multiply them to obtain \mathbf{R}. The code below shows an example

C++

 

// Calculates rotation matrix given euler angles.
Mat eulerAnglesToRotationMatrix(Vec3f &theta)
{
    // Calculate rotation about x axis
    Mat R_x = (Mat_<double>(3,3) <<
               1,       0,              0,
               0,       cos(theta[0]),   -sin(theta[0]),
               0,       sin(theta[0]),   cos(theta[0])
               );
    
    // Calculate rotation about y axis
    Mat R_y = (Mat_<double>(3,3) <<
               cos(theta[1]),    0,      sin(theta[1]),
               0,               1,      0,
               -sin(theta[1]),   0,      cos(theta[1])
               );
    
    // Calculate rotation about z axis
    Mat R_z = (Mat_<double>(3,3) <<
               cos(theta[2]),    -sin(theta[2]),      0,
               sin(theta[2]),    cos(theta[2]),       0,
               0,               0,                  1);
    
    
    // Combined rotation matrix
    Mat R = R_z * R_y * R_x;
    
    return R;

}

Python

# Calculates Rotation Matrix given euler angles.
def eulerAnglesToRotationMatrix(theta) :
    
    R_x = np.array([[1,         0,                  0                   ],
                    [0,         math.cos(theta[0]), -math.sin(theta[0]) ],
                    [0,         math.sin(theta[0]), math.cos(theta[0])  ]
                    ])
        
        
                    
    R_y = np.array([[math.cos(theta[1]),    0,      math.sin(theta[1])  ],
                    [0,                     1,      0                   ],
                    [-math.sin(theta[1]),   0,      math.cos(theta[1])  ]
                    ])
                
    R_z = np.array([[math.cos(theta[2]),    -math.sin(theta[2]),    0],
                    [math.sin(theta[2]),    math.cos(theta[2]),     0],
                    [0,                     0,                      1]
                    ])
                    
                    
    R = np.dot(R_z, np.dot( R_y, R_x ))

    return R

Convert a Rotation Matrix to Euler Angles in OpenCV

Converting a rotation matrix to Euler angles is a bit tricky. The solution is not unique in most cases. Using the code in the previous section you can verify that rotation matrices corresponding to Euler angles [0.1920,  2.3736,   1.1170] ( or [[11, 136, 64] in degrees) and [-2.9496, 0.7679, -2.0246] ( or [-169, 44, -116] in degrees) are actually the same even though the Euler angles look very different. The code below shows a method to find the Euler angles given the rotation matrix. The output of the following code should exactly match the output of MATLAB’s rotm2euler but the order of x and z are swapped.

C++

// Checks if a matrix is a valid rotation matrix.
bool isRotationMatrix(Mat &R)
{
    Mat Rt;
    transpose(R, Rt);
    Mat shouldBeIdentity = Rt * R;
    Mat I = Mat::eye(3,3, shouldBeIdentity.type());
    
    return  norm(I, shouldBeIdentity) < 1e-6;
    
}

// Calculates rotation matrix to euler angles
// The result is the same as MATLAB except the order
// of the euler angles ( x and z are swapped ).
Vec3f rotationMatrixToEulerAngles(Mat &R)
{

    assert(isRotationMatrix(R));
    
    float sy = sqrt(R.at<double>(0,0) * R.at<double>(0,0) +  R.at<double>(1,0) * R.at<double>(1,0) );

    bool singular = sy < 1e-6; // If

    float x, y, z;
    if (!singular)
    {
        x = atan2(R.at<double>(2,1) , R.at<double>(2,2));
        y = atan2(-R.at<double>(2,0), sy);
        z = atan2(R.at<double>(1,0), R.at<double>(0,0));
    }
    else
    {
        x = atan2(-R.at<double>(1,2), R.at<double>(1,1));
        y = atan2(-R.at<double>(2,0), sy);
        z = 0;
    }
    return Vec3f(x, y, z);
    
    
    
}

Python

# Checks if a matrix is a valid rotation matrix.
def isRotationMatrix(R) :
    Rt = np.transpose(R)
    shouldBeIdentity = np.dot(Rt, R)
    I = np.identity(3, dtype = R.dtype)
    n = np.linalg.norm(I - shouldBeIdentity)
    return n < 1e-6


# Calculates rotation matrix to euler angles
# The result is the same as MATLAB except the order
# of the euler angles ( x and z are swapped ).
def rotationMatrixToEulerAngles(R) :

    assert(isRotationMatrix(R))
    
    sy = math.sqrt(R[0,0] * R[0,0] +  R[1,0] * R[1,0])
    
    singular = sy < 1e-6

    if  not singular :
        x = math.atan2(R[2,1] , R[2,2])
        y = math.atan2(-R[2,0], sy)
        z = math.atan2(R[1,0], R[0,0])
    else :
        x = math.atan2(-R[1,2], R[1,1])
        y = math.atan2(-R[2,0], sy)
        z = 0

    return np.array([x, y, z])

Subscribe & Download Code

If you liked this article and would like to download code (C++ and Python) and example images used in this blog, please subscribe to our newsletter. You will also receive a free Computer Vision Resource guide. In our newsletter we share OpenCV tutorials and examples written in C++/Python, and Computer Vision and Machine Learning algorithms and news.

Subscribe Now

Tags: euler geometry OpenCV rotation Tait–Bryan

Filed Under: Tutorial

About

I am an entrepreneur with a love for Computer Vision and Machine Learning with a dozen years of experience (and a Ph.D.) in the field.

In 2007, right after finishing my Ph.D., I co-founded TAAZ Inc. with my advisor Dr. David Kriegman and Kevin Barnes. The scalability, and robustness of our computer vision and machine learning algorithms have been put to rigorous test by more than 100M users who have tried our products. Read More…

Getting Started

  • Installation
  • PyTorch
  • Keras & Tensorflow
  • Resource Guide

Resources

Download Code (C++ / Python)

ENROLL IN OFFICIAL OPENCV COURSES

I've partnered with OpenCV.org to bring you official courses in Computer Vision, Machine Learning, and AI.
Learn More

Recent Posts

  • Making A Low-Cost Stereo Camera Using OpenCV
  • Optical Flow in OpenCV (C++/Python)
  • Introduction to Epipolar Geometry and Stereo Vision
  • Depth Estimation using Stereo matching
  • Classification with Localization: Convert any Keras Classifier to a Detector

Disclaimer

All views expressed on this site are my own and do not represent the opinions of OpenCV.org or any entity whatsoever with which I have been, am now, or will be affiliated.

GETTING STARTED

  • Installation
  • PyTorch
  • Keras & Tensorflow
  • Resource Guide

COURSES

  • Opencv Courses
  • CV4Faces (Old)

COPYRIGHT © 2020 - BIG VISION LLC

Privacy Policy | Terms & Conditions

We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.AcceptPrivacy policy