Rotation matrix to euler angles with opencv c++ correct value wrong place

asked 2017-04-12 05:03:32 -0500

Brent Convens gravatar image

I am working on a project wich involves Aruco markers and opencv. I am quite far in the project progress. I can read the rotation vectors and convert them to a rodrigues matrix using rodrigues() from opencv.

This is a example of a rodrigues matrix I get:

[0,1,0;

1,0,0;

0,0,-1]

I use the following code.

Mat m33(3, 3, CV_64F);
Mat measured_eulers(3, 1, CV_64F);
Rodrigues(rotationVectors, m33);

measured_eulers = rot2euler(m33);

Degree_euler = measured_eulers * 180 / CV_PI;

I use the predefined rot2euler to convert from rodrigues matrix to euler angles. And I convert the received radians to degrees.

rot2euler looks like the following.

Mat rot2euler(const Mat & rotationMatrix)
{
    Mat euler(3, 1, CV_64F);

    double m00 = rotationMatrix.at<double>(0, 0);
    double m02 = rotationMatrix.at<double>(0, 2);
    double m10 = rotationMatrix.at<double>(1, 0);
    double m11 = rotationMatrix.at<double>(1, 1);
    double m12 = rotationMatrix.at<double>(1, 2);
    double m20 = rotationMatrix.at<double>(2, 0);
    double m22 = rotationMatrix.at<double>(2, 2);

    double x, y, z;

    // Assuming the angles are in radians.
    if (m10 > 0.998) { // singularity at north pole
         x = 0;
         y = CV_PI / 2;
         z = atan2(m02, m22);
    }
    else if (m10 < -0.998) { // singularity at south pole
        x = 0;
        y = -CV_PI / 2;
        z = atan2(m02, m22);
    }
    else
    {
         x = atan2(-m12, m11);
        y = asin(m10);
        z = atan2(-m20, m00);
   }

    euler.at<double>(0) = x;
    euler.at<double>(1) = y;
    euler.at<double>(2) = z;

    return euler;
}

If I use the rodrigues matrix I give as an example I get the following euler angles.

[0; 90; -180]

But I am suppose to get the following.

[-180; 0; 90]

When is use this tool

[danceswithcode.net/engineeringnotes/rotations_in_3d/demo3D/rotations_in_3d_tool.html]

You can see that [0; 90; -180] doesn't match the rodrigues matrix but [-180; 0; 90] does. (I am aware of the fact that the tool works with ZYX coordinates)

So the problem is I get the correct values but in a wrong order.

Another problem is that this isn't always the case. For example rodrigues matrix:

[1,0,0;

0,-1,0;

0,0,-1]

Provides me the correct euler angles.

If someone knows a solution to the problem or can provide me with a explanation how the rot2euler function works exactly. It will be higly appreciated.

Kind Regards

Brent Convens

edit retag flag offensive close merge delete

Comments