0
votes

I got a question about how to use the EulerAngle to determine camera's orientation First, I used solvePnP function and got two output "rvec" and "tvec", then I use Rodrigues to convert rvec to rotation matrix "R". After that, I calculated EulerAngle by using the function below:

void getEulerAngles(cv::Mat matrix)

{

assert(isRotationMatrix(matrix));

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

bool singular = sy<1e-6;
float theta_x=0.0,theta_y=0.0,theta_z=0.0;//theta_x means rotation around X-Axis
if(!singular)
{
    theta_x=atan2(matrix.at<double>(2,1),matrix.at<double>(2,2));
    theta_x= theta_x*180.0/3.1416 ;
    theta_y=atan2(-matrix.at<double>(2,0), sy);
    theta_y= theta_y*180.0/3.1416 ;
    theta_z=atan2(matrix.at<double>(1,0), matrix.at<double>(0,0));
    theta_z= theta_z*180.0/3.1416 ;
}
else
{
    theta_x=atan2(-matrix.at<double>(1,2), matrix.at<double>(1,1));
    theta_x= theta_x*180.0/3.1416 ;
    theta_y=atan2(-matrix.at<double>(2,0), sy);
    theta_y= theta_y*180.0/3.1416 ;
    theta_z=0;
    theta_z= theta_z*180.0/3.1416 ;
}

I know that different rotation order can make different result.So if I want to get the camera's orientation what kind of rotation order should I choose?

1

1 Answers

0
votes

I think I kinda know how to solve this problem. The orientation order is always z-y-x.It means you just need to rotate your "tvec" around the Z-Axis, then around the y-axis, finally rotate around the x-axis. And remember to use negative Euler angles. There is my code:

    void calCamPose(cv::Mat t)
// the order of rotation is z-y-x
{
    cv::Point3f tvec(t);
    float x1=cos(-theta_z)*tvec.x-sin(-theta_z)*tvec.y;
    float y1=sin(-theta_z)*tvec.x+cos(-theta_z)*tvec.y;//first rotation

    float outx=cos(-theta_y)*x1+sin(-theta_y)*tvec.z;
    float z2=cos(-theta_y)*tvec.z+sin(-theta_y)*x1;//second rotation

    float outy=cos(-theta_x)*y1-sin(-theta_x)*z2;
    float outz=cos(-theta_x)*z2+sin(-theta_x)*y1;//third rotation
    cv::Point3f cam_pose=(0,0,0);
    cam_pose.x=outx,cam_pose.y=outy,cam_pose.z=outz;
    Debug("Cam_Pose");
    Debug(cam_pose);
}