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判断文本中消息重复次数的方法
Apr 27 Python
Python迭代器定义与简单用法分析
Apr 30 Python
python 提取key 为中文的json 串方法
Dec 31 Python
postman模拟访问具有Session的post请求方法
Jul 15 Python
python删除列表元素的三种方法(remove,pop,del)
Jul 22 Python
Pytorch 神经网络—自定义数据集上实现教程
Jan 07 Python
Python selenium抓取虎牙短视频代码实例
Mar 02 Python
tensorflow图像裁剪进行数据增强操作
Jun 30 Python
Python文件夹批处理操作代码实例
Jul 21 Python
Pycharm中如何关掉python console
Oct 27 Python
python上下文管理器异常问题解决方法
Feb 07 Python
Python IO文件管理的具体使用
Mar 20 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之第二天
2006/10/09 PHP
PHP运行出现Notice : Use of undefined constant 的完美解决方案分享
2012/03/05 PHP
smarty巧妙处理iframe中内容页的代码
2012/03/07 PHP
ThinkPHP验证码和分页实例教程
2014/08/22 PHP
PHP基于SMTP协议实现邮件发送实例代码
2017/04/27 PHP
PHP封装的数据库模型Model类完整示例【基于PDO】
2019/03/14 PHP
jQuery 位置插件
2008/12/25 Javascript
使用jQuery UI的tooltip函数修饰title属性的气泡悬浮框
2013/06/24 Javascript
javascript异步编程的4种方法
2014/02/19 Javascript
JS函数重载的解决方案
2014/05/13 Javascript
jQuery 获取/设置/删除DOM元素的属性以a元素为例
2014/05/23 Javascript
js实现鼠标滑过文字链接色彩变化的效果
2015/05/06 Javascript
JS实现可自定义大小,可双击关闭的弹出层效果
2015/10/16 Javascript
jquery插件jquery.dragscale.js实现拖拽改变元素大小的方法(附demo源码下载)
2016/02/25 Javascript
jQuery表单事件实例代码分享
2016/08/18 Javascript
js监听input输入框值的实时变化实例
2017/01/26 Javascript
微信小程序表单验证错误提示效果
2017/05/19 Javascript
Node.js操作redis实现添加查询功能
2017/05/25 Javascript
微信小程序动态显示项目倒计时效果
2017/06/13 Javascript
利用JS判断客户端类型你应该知道的四种方法
2017/12/22 Javascript
Vue2.0子同级组件之间数据交互方法
2018/02/28 Javascript
Vue.directive 自定义指令的问题小结
2018/03/04 Javascript
用Python编写一个基于终端的实现翻译的脚本
2015/04/24 Python
python实现识别手写数字 python图像识别算法
2020/03/23 Python
Python Pandas批量读取csv文件到dataframe的方法
2018/10/08 Python
python requests指定出口ip的例子
2019/07/25 Python
Python面向对象程序设计之类和对象、实例变量、类变量用法分析
2020/03/23 Python
关于探究python中sys.argv时遇到的问题详解
2021/02/23 Python
德国电子产品购物网站:TechInTheBasket德国
2018/12/07 全球购物
阿拉伯书店:Jamalon
2019/07/24 全球购物
社会学专业求职信
2014/02/24 职场文书
事业单位人员的自我评价范文
2014/09/21 职场文书
数学教师求职信范文
2015/03/20 职场文书
红色电影观后感
2015/06/18 职场文书
妇联2016年六一国际儿童节活动总结
2016/04/06 职场文书
Win11任务栏无法正常显示 资源管理器不停重启的解决方法
2022/07/07 数码科技