-
Notifications
You must be signed in to change notification settings - Fork 0
/
rotMat2euler_cv.cpp
114 lines (105 loc) · 3.13 KB
/
rotMat2euler_cv.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <iostream>
#include <sstream>
#include <time.h>
#include <stdio.h>
#include <fstream>
#include <cmath>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/calib3d/calib3d.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
using namespace std;
// 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;
}
// 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
double 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;
}
#if 1
x = x*180.0f / 3.141592653589793f;
y = y*180.0f / 3.141592653589793f;
z = z*180.0f / 3.141592653589793f;
#endif
return Vec3f(x, y, z);
}
int main()
{
Vec3f eulerAngles;
//int i;
////double r_vec[3]={-2.100418,-2.167796,0.273330};[[0.78520514][0.0233998 ][0.00969251
//double r_vec[3] = { 2.174810320117,-0.004623494847,0.010459004693 };//eulerAngles[45,1,1]
//double R_matrix[9];
////CvMat *pr_vec;
////CvMat *pR_matrix;
//CvMat *pr_vec = cvCreateMat(1, 3, CV_64FC1);
//CvMat *pR_matrix = cvCreateMat(3, 3, CV_64FC1);
//cvInitMatHeader(pr_vec, 1, 3, CV_64FC1, r_vec, CV_AUTOSTEP);
//cvInitMatHeader(pR_matrix, 3, 3, CV_64FC1, R_matrix, CV_AUTOSTEP);
//cvRodrigues2(pr_vec, pR_matrix, 0);
//Mat rotation_matrix(pR_matrix->rows, pR_matrix->cols, pR_matrix->type, pR_matrix->data.fl);
//eulerAngles = rotationMatrixToEulerAngles(rotation_matrix);
//cout << "pR_matrix = " << endl;
//cout << rotation_matrix << endl;
//cout << "eulerAngles = " << endl;
//cout << eulerAngles << endl;
Mat_<double> r_l = (Mat_<double>(3, 1) << -0.002865326065, -2.841691129650, 1.256884419023);//ºóÉãÏñ»úµÄÐýתÏòÁ¿
Mat R_L;
Rodrigues(r_l, R_L);
eulerAngles = rotationMatrixToEulerAngles(R_L);
cout << "R_L = " << endl;
cout << R_L << endl;
cout << "eulerAngles = " << endl;
cout << eulerAngles << endl;
//cvRodrigues2(&pr_vec,&pR_matrix,0);
return 0;
}