python 和c++实现旋转矩阵到欧拉角的变换方式


Posted in Python onDecember 04, 2019

在摄影测量学科中,国际摄影测量遵循OPK系统,即是xyz转角系统,而工业中往往使用zyx转角系统。

旋转矩阵的意义:描述相对地面的旋转情况,yaw-pitch-roll对应zyx对应k,p,w

#include <iostream>
#include<stdlib.h>
#include<eigen3/Eigen/Core>
#include<eigen3/Eigen/Dense>
#include<stdlib.h>
using namespace std;
Eigen::Matrix3d rotationVectorToMatrix(Eigen::Vector3d theta)
{
  Eigen::Matrix3d R_x=Eigen::AngleAxisd(theta(0),Eigen::Vector3d(1,0,0)).toRotationMatrix();
  Eigen::Matrix3d R_y=Eigen::AngleAxisd(theta(1),Eigen::Vector3d(0,1,0)).toRotationMatrix();
  Eigen::Matrix3d R_z=Eigen::AngleAxisd(theta(2),Eigen::Vector3d(0,0,1)).toRotationMatrix();
  return R_z*R_y*R_x;

}
bool isRotationMatirx(Eigen::Matrix3d R)
{
  int err=1e-6;//判断R是否奇异
  Eigen::Matrix3d shouldIdenity;
  shouldIdenity=R*R.transpose();
  Eigen::Matrix3d I=Eigen::Matrix3d::Identity();
  return (shouldIdenity-I).norm()<err?true:false;
}

int main(int argc, char *argv[])
{
  Eigen::Matrix3d R;
  Eigen::Vector3d theta(rand() % 360 - 180.0, rand() % 360 - 180.0, rand() % 360 - 180.0);
  theta=theta*M_PI/180;
  cout<<"旋转向量是:\n"<<theta.transpose()<<endl;
  R=rotationVectorToMatrix(theta);
  cout<<"旋转矩阵是:\n"<<R<<endl;
  if(! isRotationMatirx(R)){
   cout<<"旋转矩阵--->欧拉角\n"<<R.eulerAngles(2,1,0).transpose()<<endl;//z-y-x顺序,与theta顺序是x,y,z
  }
  else{
    assert(isRotationMatirx(R));
  }

  return 0;
}

python 和c++实现旋转矩阵到欧拉角的变换方式

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import cv2
import numpy as np
import math
import random

def isRotationMatrix(R) :
  Rt = np.transpose(R)
  shouldBeIdentity = np.dot(Rt, R)
  I = np.identity(3, dtype = R.dtype)
  n = np.linalg.norm(I - shouldBeIdentity)
  return n < 1e-6

def rotationMatrixToEulerAngles(R) :

  assert(isRotationMatrix(R))
  
  sy = math.sqrt(R[0,0] * R[0,0] + R[1,0] * R[1,0])
  
  singular = sy < 1e-6

  if not singular :
    x = math.atan2(R[2,1] , R[2,2])
    y = math.atan2(-R[2,0], sy)
    z = math.atan2(R[1,0], R[0,0])
  else :
    x = math.atan2(-R[1,2], R[1,1])
    y = math.atan2(-R[2,0], sy)
    z = 0

  return np.array([x, y, z])

def eulerAnglesToRotationMatrix(theta) :
  
  R_x = np.array([[1,     0,         0          ],
          [0,     math.cos(theta[0]), -math.sin(theta[0]) ],
          [0,     math.sin(theta[0]), math.cos(theta[0]) ]
          ])
    
    
          
  R_y = np.array([[math.cos(theta[1]),  0,   math.sin(theta[1]) ],
          [0,           1,   0          ],
          [-math.sin(theta[1]),  0,   math.cos(theta[1]) ]
          ])
        
  R_z = np.array([[math.cos(theta[2]),  -math.sin(theta[2]),  0],
          [math.sin(theta[2]),  math.cos(theta[2]),   0],
          [0,           0,           1]
          ])
          
          
  R = np.dot(R_z, np.dot( R_y, R_x ))

  return R


if __name__ == '__main__' :

  e = np.random.rand(3) * math.pi * 2 - math.pi
  
  R = eulerAnglesToRotationMatrix(e)
  e1 = rotationMatrixToEulerAngles(R)

  R1 = eulerAnglesToRotationMatrix(e1)
  print ("\nInput Euler angles :\n{0}".format(e))
  print ("\nR :\n{0}".format(R))
  print ("\nOutput Euler angles :\n{0}".format(e1))
  print ("\nR1 :\n{0}".format(R1))

以上这篇python 和c++实现旋转矩阵到欧拉角的变换方式就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python是编译运行的验证方法
Jan 30 Python
让python在hadoop上跑起来
Jan 27 Python
python学习教程之使用py2exe打包
Sep 24 Python
使用Python爬取最好大学网大学排名
Feb 24 Python
Python编写合并字典并实现敏感目录的小脚本
Feb 26 Python
python调用自定义函数的实例操作
Jun 26 Python
python算法与数据结构之单链表的实现代码
Jun 27 Python
python json.dumps() json.dump()的区别详解
Jul 14 Python
Python Opencv轮廓常用操作代码实例解析
Sep 01 Python
Python浮点型(float)运算结果不正确的解决方案
Sep 22 Python
Python新建项目自动添加介绍和utf-8编码的方法
Dec 26 Python
python lambda的使用详解
Feb 26 Python
python实现一个点绕另一个点旋转后的坐标
Dec 04 #Python
Django配置文件代码说明
Dec 04 #Python
python实现回旋矩阵方式(旋转矩阵)
Dec 04 #Python
在Django下创建项目以及设置settings.py教程
Dec 03 #Python
Django自带的加密算法及加密模块详解
Dec 03 #Python
python Opencv计算图像相似度过程解析
Dec 03 #Python
django 中使用DateTime常用的时间查询方式
Dec 03 #Python
You might like
php mysql 判断update之后是否更新了的方法
2012/01/10 PHP
PHP中仿制 ecshop验证码实例
2017/01/06 PHP
laravel 5.5 关闭token的3种实现方式
2019/10/24 PHP
js 优化次数过多的循环 考虑到性能问题
2011/03/05 Javascript
常见效果实现之返回顶部(结合淡入、淡出、减速滚动)
2012/01/04 Javascript
一个JQuery操作Table的代码分享
2012/03/30 Javascript
javascript学习笔记(一) 在html中使用javascript
2012/06/18 Javascript
js弹出模式对话框,并接收回传值的方法
2013/03/12 Javascript
javascript获取URL参数与参数值的示例代码
2013/12/20 Javascript
JS数组的遍历方式for循环与for...in
2014/07/31 Javascript
JS中关于事件处理函数名后面是否带括号的问题
2016/11/16 Javascript
jQuery实现可移动选项的左右下拉列表示例
2016/12/26 Javascript
浅谈js中function的参数默认值
2017/02/20 Javascript
利用angularjs1.4制作的简易滑动门效果
2017/02/28 Javascript
JQuery 选择器、DOM节点操作练习实例
2017/09/28 jQuery
jQuery UI实现动画效果代码分享
2018/08/19 jQuery
详解JavaScript中typeof与instanceof用法
2018/10/24 Javascript
webpack常用构建优化策略小结
2019/11/21 Javascript
node.js Promise对象的使用方法实例分析
2019/12/26 Javascript
python调用新浪微博API项目实践
2014/07/28 Python
python读写ini配置文件方法实例分析
2015/06/30 Python
老生常谈Python startswith()函数与endswith函数
2017/09/08 Python
如何利用Boost.Python实现Python C/C++混合编程详解
2018/11/08 Python
Python常用模块之requests模块用法分析
2019/05/15 Python
django之使用celery-把耗时程序放到celery里面执行的方法
2019/07/12 Python
jupyter notebook中新建cell的方法与快捷键操作
2020/04/22 Python
2020版Python学习路线图(附学习资料)
2020/09/15 Python
scrapy与selenium结合爬取数据(爬取动态网站)的示例代码
2020/09/28 Python
HTML5 Canvas之测试浏览器是否支持Canvas的方法
2015/01/01 HTML / CSS
详解html5 canvas常用api总结(二)--绘图API
2016/12/14 HTML / CSS
英国领先的鞋类零售商和顶级品牌的官方零售商:Wynsors
2020/02/17 全球购物
Ajax的工作原理
2015/12/04 面试题
市政工程技术专业自荐书
2014/07/06 职场文书
浅谈如何保证Mysql主从一致
2022/03/13 MySQL
nginx搭建NFS网络文件系统
2022/04/14 Servers
springboot创建的web项目整合Quartz框架的项目实践
2022/06/21 Java/Android