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 相关文章推荐
在Django中限制已登录用户的访问的方法
Jul 23 Python
python实现备份目录的方法
Aug 03 Python
Python 专题一 函数的基础知识
Mar 16 Python
django中的setting最佳配置小结
Nov 21 Python
Python快速排序算法实例分析
Nov 29 Python
详解Python if-elif-else知识点
Jun 11 Python
flask中的wtforms使用方法
Jul 21 Python
对python中大文件的导入与导出方法详解
Dec 28 Python
使用 Supervisor 监控 Python3 进程方式
Dec 05 Python
Python求平面内点到直线距离的实现
Jan 19 Python
Pytorch环境搭建与基本语法
Jun 03 Python
MATLAB 全景图切割及盒图显示的实现步骤
May 14 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 友好URL的实现(吐血推荐)
2008/10/04 PHP
WordPress自定义时间显示格式
2015/03/27 PHP
php实现的mongodb操作类实例
2015/04/03 PHP
Jquery中children与find之间的区别详细解析
2013/11/29 Javascript
jQuery中$.extend()用法实例
2015/06/24 Javascript
js变形金刚文字特效代码分享
2015/08/20 Javascript
JQueryEasyUI框架下的combobox的取值和绑定的方法
2017/01/22 Javascript
bootstrap datetimepicker日期插件超详细使用方法介绍
2017/02/23 Javascript
详解在vue-cli项目中使用mockjs(请求数据删除数据)
2017/10/23 Javascript
利用HBuilder打包前端开发webapp为apk的方法
2017/11/13 Javascript
Vue.js搭建移动端购物车界面
2020/06/28 Javascript
JavaScript 中定义函数用 var foo = function () {} 和 function foo()区别介绍
2018/03/01 Javascript
原生JS使用Canvas实现拖拽式绘图功能
2019/06/05 Javascript
vue项目中常见问题及解决方案(推荐)
2019/10/21 Javascript
js实现简单点赞操作
2020/03/17 Javascript
Javascript类型判断相关例题及解析
2020/08/26 Javascript
[18:32]DOTA2 HEROS教学视频教你分分钟做大人-谜团
2014/06/12 DOTA
Python中的对象,方法,类,实例,函数用法分析
2015/01/15 Python
Python selenium 三种等待方式详解(必会)
2016/09/15 Python
python实现视频分帧效果
2019/05/31 Python
Python基础学习之基本数据结构详解【数字、字符串、列表、元组、集合、字典】
2019/06/18 Python
pygame实现贪吃蛇游戏(下)
2019/10/29 Python
PyCharm使用Docker镜像搭建Python开发环境
2019/12/26 Python
python GUI库图形界面开发之PyQt5多行文本框控件QTextEdit详细使用方法实例
2020/02/28 Python
Python使用pyyaml模块处理yaml数据
2020/04/14 Python
软件测试工程师结构化面试题库
2016/11/23 面试题
2014道德模范事迹材料
2014/02/16 职场文书
前台文员职责范本
2014/03/07 职场文书
售后求职信范文
2014/03/15 职场文书
五一口号
2014/06/19 职场文书
小学生我的梦想演讲稿
2014/08/21 职场文书
创先争优个人承诺书
2014/08/30 职场文书
科技活动周标语
2014/10/08 职场文书
工作检讨书大全
2015/01/26 职场文书
高二语文教学反思
2016/02/16 职场文书
导游词之吉林吉塔
2019/11/11 职场文书