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 相关文章推荐
17个Python小技巧分享
Jan 23 Python
Python全局变量操作详解
Apr 14 Python
python中子类继承父类的__init__方法实例
Dec 15 Python
python检索特定内容的文本文件实例
Jun 05 Python
Django实战之用户认证(初始配置)
Jul 16 Python
Python字符串、整数、和浮点型数相互转换实例
Aug 04 Python
详解django自定义中间件处理
Nov 21 Python
100行Python代码实现每天不同时间段定时给女友发消息
Sep 27 Python
Python小程序之在图片上加入数字的代码
Nov 26 Python
Django多数据库配置及逆向生成model教程
Mar 28 Python
python字典按照value排序方法
Dec 28 Python
利用Python实现最小二乘法与梯度下降算法
Feb 21 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 文章调用类代码
2011/08/11 PHP
php Ubb代码编辑器函数代码
2012/07/05 PHP
怎样使用php与jquery设置和读取cookies
2013/08/08 PHP
使用phpQuery采集网页的方法
2013/11/13 PHP
PHP网页游戏学习之Xnova(ogame)源码解读(一)
2014/06/23 PHP
php基于mcrypt的加密解密实例
2014/10/27 PHP
深入浅出php socket编程
2015/05/13 PHP
laravel5.2实现区分前后台用户登录的方法
2017/01/11 PHP
详细解读php的命名空间(二)
2018/02/21 PHP
原型方法的不同写法居然会影响调试的解决方法
2007/03/08 Javascript
javascript cookies 设置、读取、删除实例代码
2010/04/12 Javascript
30分钟就入门的正则表达式基础教程
2013/02/25 Javascript
使用node.js 制作网站前台后台
2014/11/13 Javascript
js下拉选择框与输入框联动实现添加选中值到输入框的方法
2015/08/17 Javascript
深入学习js瀑布流布局
2016/10/14 Javascript
video.js 实现视频只能后退不能快进的思路详解
2018/08/09 Javascript
利用 JavaScript 实现并发控制的示例代码
2020/12/31 Javascript
[59:08]Ti4 冒泡赛第二天 NEWBEE vs Titan 2
2014/07/15 DOTA
[01:12]快闪回顾DOTA2亚洲邀请赛(DAC) 静候2018新征程开启
2018/03/11 DOTA
用Python将一个列表分割成小列表的实例讲解
2018/07/02 Python
利用pandas进行大文件计数处理的方法
2018/07/25 Python
Python 数值区间处理_对interval 库的快速入门详解
2018/11/16 Python
用Python解决x的n次方问题
2019/02/08 Python
详解python校验SQL脚本命名规则
2019/03/22 Python
selenium+python环境配置教程详解
2019/05/28 Python
Python绘图之二维图与三维图详解
2020/08/04 Python
html5中监听canvas内部元素点击事件的三种方法
2019/04/28 HTML / CSS
Marriott中国:万豪国际酒店查询预订
2016/09/02 全球购物
雅高酒店中国:Accorhotels.com China
2018/03/26 全球购物
中华在我心中演讲稿
2014/09/13 职场文书
晋江市人民政府党组群众路线教育实践活动整改方案
2014/10/25 职场文书
2014年环境整治工作总结
2014/12/10 职场文书
2016年国陪研修感言
2015/11/18 职场文书
“学党章、守党纪、讲党规”学习心得体会
2016/01/14 职场文书
详解Python+OpenCV绘制灰度直方图
2022/03/22 Python