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算法学习之计数排序实例
Dec 18 Python
Python中pip安装非PyPI官网第三方库的方法
Jun 02 Python
python开发之文件操作用法实例
Nov 13 Python
Python入门_浅谈数据结构的4种基本类型
May 16 Python
机器学习的框架偏向于Python的13个原因
Dec 07 Python
Django 2.0版本的新特性抢先看!
Jan 05 Python
《Python学习手册》学习总结
Jan 17 Python
python+mysql实现学生信息查询系统
Feb 21 Python
Python实现Wordcloud生成词云图的示例
Mar 30 Python
python 负数取模运算实例
Jun 03 Python
pandas处理csv文件的方法步骤
Oct 16 Python
pytest进阶教程之fixture函数详解
Mar 29 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
SONY ICF-F10中波修复记
2021/03/02 无线电
生成php程序的php代码
2008/04/07 PHP
php防止sql注入之过滤分页参数实例
2014/11/03 PHP
thinkphp的URL路由规则与配置实例
2014/11/26 PHP
php绘制一个矩形的方法
2015/01/24 PHP
WordPress中用于获取搜索表单的PHP函数使用解析
2016/01/05 PHP
PHP在线调试执行的实现方法(附demo源码)
2016/04/28 PHP
php封装的图片(缩略图)处理类完整实例
2016/10/19 PHP
jQuery 操作option的实现代码
2011/03/03 Javascript
js 编程笔记 无名函数
2011/06/28 Javascript
JavaScript 原型继承之构造函数继承
2011/08/26 Javascript
js调用AJAX时Get和post的乱码解决方法
2013/06/04 Javascript
利用js读取动态网站从服务器端返回的数据
2014/02/10 Javascript
js实现网页随机切换背景图片的方法
2014/11/01 Javascript
js获取字符串字节数方法小结
2015/06/09 Javascript
JavaScript使用FileSystemObject对象写入文本文件内容的方法
2015/08/05 Javascript
jQuery实现form表单基于ajax无刷新提交方法详解
2015/12/08 Javascript
JS中的数组方法笔记整理
2016/07/26 Javascript
BootStrap的table表头固定tbody滚动的实例代码
2016/08/24 Javascript
JavaScript编写一个贪吃蛇游戏
2017/03/09 Javascript
ajax+node+request爬取网络图片的实例(宅男福利)
2017/08/28 Javascript
node文字生成图片的示例代码
2017/10/26 Javascript
JS实现的新闻列表自动滚动效果示例
2019/01/30 Javascript
在vue项目中 实现定义全局变量 全局函数操作
2020/10/26 Javascript
[28:42]Ti4正赛VG vs NEWBEE1
2014/07/19 DOTA
[32:30]夜魇凡尔赛茶话会 第一期01:谁是卧底
2021/03/11 DOTA
django使用LDAP验证的方法示例
2018/12/10 Python
python 自动批量打开网页的示例
2019/02/21 Python
Python实现转换图片背景颜色代码
2020/04/30 Python
Keras load_model 导入错误的解决方式
2020/06/09 Python
聊聊Python pandas 中loc函数的使用,及跟iloc的区别说明
2021/03/03 Python
CSS3.0实现霓虹灯按钮动画特效的示例代码
2021/01/12 HTML / CSS
店长岗位职责
2013/11/21 职场文书
《盘古开天地》教学反思
2014/02/28 职场文书
运输服务质量承诺书
2014/03/27 职场文书
意外伤害赔偿协议书
2014/09/16 职场文书