Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/apps/interactive-calibration/rotationConverters.cpp
16337 views
1
// This file is part of OpenCV project.
2
// It is subject to the license terms in the LICENSE file found in the top-level directory
3
// of this distribution and at http://opencv.org/license.html.
4
5
#include "rotationConverters.hpp"
6
7
#include <cmath>
8
9
#include <opencv2/calib3d.hpp>
10
#include <opencv2/core.hpp>
11
12
#define CALIB_PI 3.14159265358979323846
13
#define CALIB_PI_2 1.57079632679489661923
14
15
void calib::Euler(const cv::Mat& src, cv::Mat& dst, int argType)
16
{
17
if((src.rows == 3) && (src.cols == 3))
18
{
19
//convert rotation matrix to 3 angles (pitch, yaw, roll)
20
dst = cv::Mat(3, 1, CV_64F);
21
double pitch, yaw, roll;
22
23
if(src.at<double>(0,2) < -0.998)
24
{
25
pitch = -atan2(src.at<double>(1,0), src.at<double>(1,1));
26
yaw = -CALIB_PI_2;
27
roll = 0.;
28
}
29
else if(src.at<double>(0,2) > 0.998)
30
{
31
pitch = atan2(src.at<double>(1,0), src.at<double>(1,1));
32
yaw = CALIB_PI_2;
33
roll = 0.;
34
}
35
else
36
{
37
pitch = atan2(-src.at<double>(1,2), src.at<double>(2,2));
38
yaw = asin(src.at<double>(0,2));
39
roll = atan2(-src.at<double>(0,1), src.at<double>(0,0));
40
}
41
42
if(argType == CALIB_DEGREES)
43
{
44
pitch *= 180./CALIB_PI;
45
yaw *= 180./CALIB_PI;
46
roll *= 180./CALIB_PI;
47
}
48
else if(argType != CALIB_RADIANS)
49
CV_Error(cv::Error::StsBadFlag, "Invalid argument type");
50
51
dst.at<double>(0,0) = pitch;
52
dst.at<double>(1,0) = yaw;
53
dst.at<double>(2,0) = roll;
54
}
55
else if( (src.cols == 1 && src.rows == 3) ||
56
(src.cols == 3 && src.rows == 1 ) )
57
{
58
//convert vector which contains 3 angles (pitch, yaw, roll) to rotation matrix
59
double pitch, yaw, roll;
60
if(src.cols == 1 && src.rows == 3)
61
{
62
pitch = src.at<double>(0,0);
63
yaw = src.at<double>(1,0);
64
roll = src.at<double>(2,0);
65
}
66
else{
67
pitch = src.at<double>(0,0);
68
yaw = src.at<double>(0,1);
69
roll = src.at<double>(0,2);
70
}
71
72
if(argType == CALIB_DEGREES)
73
{
74
pitch *= CALIB_PI / 180.;
75
yaw *= CALIB_PI / 180.;
76
roll *= CALIB_PI / 180.;
77
}
78
else if(argType != CALIB_RADIANS)
79
CV_Error(cv::Error::StsBadFlag, "Invalid argument type");
80
81
dst = cv::Mat(3, 3, CV_64F);
82
cv::Mat M(3, 3, CV_64F);
83
cv::Mat i = cv::Mat::eye(3, 3, CV_64F);
84
i.copyTo(dst);
85
i.copyTo(M);
86
87
double* pR = dst.ptr<double>();
88
pR[4] = cos(pitch);
89
pR[7] = sin(pitch);
90
pR[8] = pR[4];
91
pR[5] = -pR[7];
92
93
double* pM = M.ptr<double>();
94
pM[0] = cos(yaw);
95
pM[2] = sin(yaw);
96
pM[8] = pM[0];
97
pM[6] = -pM[2];
98
99
dst *= M;
100
i.copyTo(M);
101
pM[0] = cos(roll);
102
pM[3] = sin(roll);
103
pM[4] = pM[0];
104
pM[1] = -pM[3];
105
106
dst *= M;
107
}
108
else
109
CV_Error(cv::Error::StsBadFlag, "Input matrix must be 1x3, 3x1 or 3x3" );
110
}
111
112
void calib::RodriguesToEuler(const cv::Mat& src, cv::Mat& dst, int argType)
113
{
114
CV_Assert((src.cols == 1 && src.rows == 3) || (src.cols == 3 && src.rows == 1));
115
cv::Mat R;
116
cv::Rodrigues(src, R);
117
Euler(R, dst, argType);
118
}
119
120
void calib::EulerToRodrigues(const cv::Mat& src, cv::Mat& dst, int argType)
121
{
122
CV_Assert((src.cols == 1 && src.rows == 3) || (src.cols == 3 && src.rows == 1));
123
cv::Mat R;
124
Euler(src, R, argType);
125
cv::Rodrigues(R, dst);
126
}
127
128