【计算机视觉】旋转矩阵与欧拉角相互转换
问题
1. 旋转矩阵的定义;
2. 欧拉角的定义;
3. 旋转矩阵和欧拉角之间的关系;
4. 旋转矩阵和欧拉角之间的转换及其代码;
5. decomposeProjectionMatrix函数中的欧拉角的单位是弧度还是角度,pitch/yaw/roll三者的顺序以及方向性又是如何对应的?
6. 已知欧拉角,如何计算得到变换后的3D坐标。
pitch = eulerAngles[0]; yaw = eulerAngles[1]; roll = eulerAngles[2];
不知道上边的赋值是否正确;
欧拉角和旋转矩阵之间的转换:

/* * Copyright (c) 2016 Satya Mallick <spmallick@learnopencv.com> * All rights reserved. No warranty, explicit or implicit, provided. */ #include "opencv2/opencv.hpp" #include <stdlib.h> /* srand, rand */ #include <time.h> /* time */ using namespace cv; using namespace std; // 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 float 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; } return Vec3f(x, y, z); } // 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; } int main(int argc, char** argv) { // Initialize random number generator srand (time(NULL)); // Randomly generate Euler angles in Degrees. Vec3f eDegrees(rand() % 360 - 180.0, rand() % 360 - 180.0, rand() % 360 - 180.0); // Convert angles to radians Vec3f e = eDegrees * M_PI / 180.0; // Calculate rotation matrix Mat R = eulerAnglesToRotationMatrix(e); // Calculate Euler angles from rotation matrix Vec3f e1 = rotationMatrixToEulerAngles(R); // Calculate rotation matrix Mat R1 = eulerAnglesToRotationMatrix(e1); // Note e and e1 will be the same a lot of times // but not always. R and R1 should be the same always. cout << endl << "Input Angles" << endl << e << endl; cout << endl << "R : " << endl << R << endl; cout << endl << "Output Angles" << endl << e1 << endl; cout << endl << "R1 : " << endl << R1 << endl; }
参考
1. https://blog.csdn.net/lircsszz/article/details/80118051;
2. learnopencv_Rotation Matrix To Euler Angles;
3. https://blog.csdn.net/u012525096/article/details/78890463;
4. https://blog.csdn.net/qq_31806429/article/details/78844305;
5. http://answers.opencv.org/question/16796/computing-attituderoll-pitch-yaw-from-solvepnp/?answer=52913#post-id-52913;
6. https://github.com/lincolnhard/head-pose-estimation/issues/17;
完
各美其美,美美与共,不和他人作比较,不对他人有期待,不批判他人,不钻牛角尖。
心正意诚,做自己该做的事情,做自己喜欢做的事情,安静做一枚有思想的技术媛。
版权声明,转载请注明出处:https://www.cnblogs.com/happyamyhope/
心正意诚,做自己该做的事情,做自己喜欢做的事情,安静做一枚有思想的技术媛。
版权声明,转载请注明出处:https://www.cnblogs.com/happyamyhope/
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】