网上有各种各样的欧拉角和旋转矩阵转换的代码,可是运行发现结果不对,原因是c++的三角函数运算支持的是弧度角,所以在机器人示教器和相机标定的出的内参矩阵角度需要先转化为弧度制,将其存放在数组中进行计算转化,代码如下,已经测试可以运行并且结果正确,放心使用
// ConsoleApplication4.cpp : 定义控制台应用程序的入口点。
#include "stdafx.h"
#include<iostream>
#include <vector>
#include <functional>
#include <algorithm>
#include <string>
#include<map>
#include <opencv2/opencv.hpp>
#define pi 3.1415926
using namespace cv;
using namespace std;
Mat Robot_eulerAnglesToRotationMatrix(double a[3])
{
//角度转弧度
for (int i = 0; i < 3; i++)
a[i] = a[i] / 180 * pi;
// Calculate rotation about x axis
Mat R_x = (Mat_<double>(3, 3) <<
1, 0, 0,
0, cos(a[0]), -sin(a[0]),
0, sin(a[0]), cos(a[0])
);
//cout << "R_x" << R_x << endl;
// Calculate rotation about y axis
Mat R_y = (Mat_<double>(3, 3) <<
cos(a[1]), 0, sin(a[1]),
0, 1, 0,
-sin(a[1]), 0, cos(a[1])
);
//cout << "R_y" << R_y << endl;
// Calculate rotation about z axis
Mat R_z = (Mat_<double>(3, 3) <<
cos(a[2]), -sin(a[2]), 0,
sin(a[2]), cos(a[2]), 0,
0, 0, 1
);
//cout << "R_z" << R_z<< endl;
// Combined rotation matrix
Mat R_R = R_x * R_y *R_z;
return R_R;
}
int main()
{
double a[3] = { 30,60,30};
Mat R1 = (Mat_<double>(3, 3) = Robot_eulerAnglesToRotationMatrix(a));
cout << R1;
waitKey(0);
return 0;
}