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 21 Python
python 对象和json互相转换方法
Mar 22 Python
pycharm重置设置,恢复默认设置的方法
Oct 22 Python
Python实现高斯函数的三维显示方法
Dec 29 Python
pyQt5实时刷新界面的示例
Jun 25 Python
python 并发编程 多路复用IO模型详解
Aug 20 Python
浅谈pytorch grad_fn以及权重梯度不更新的问题
Aug 20 Python
django自带的权限管理Permission用法说明
May 13 Python
python中 _、__、__xx__()区别及使用场景
Jun 30 Python
详解Python中第三方库Faker
Sep 25 Python
python爬虫scrapy框架之增量式爬虫的示例代码
Feb 26 Python
pytorch中F.avg_pool1d()和F.avg_pool2d()的使用操作
May 22 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
我的论坛源代码(八)
2006/10/09 PHP
php 操作数组(合并,拆分,追加,查找,删除等)
2012/07/20 PHP
php排序算法实例分析
2016/10/17 PHP
PHP实现留言板功能的详细代码
2017/03/25 PHP
Laravel 队列使用的实现
2019/01/08 PHP
javascript编程起步(第四课)
2007/01/10 Javascript
JavaScript RegExp方法获取地址栏参数(面向对象)
2009/03/10 Javascript
10个基于浏览器的JavaScript调试工具分享
2013/02/07 Javascript
JavaScript中instanceof与typeof运算符的用法及区别详细解析
2013/11/19 Javascript
jQuery实现在下拉列表选择时获取json数据的方法
2015/04/16 Javascript
javascript设计模式之对象工厂函数与构造函数详解
2015/07/30 Javascript
javascript运动框架用法实例分析(实现放大与缩小效果)
2016/01/08 Javascript
vue2.0 keep-alive最佳实践
2017/07/06 Javascript
Javascript中 toFixed四舍六入方法
2017/08/21 Javascript
基于AngularJS的简单使用详解
2017/09/10 Javascript
JS设计模式之状态模式概念与用法分析
2018/02/05 Javascript
Chart.js 轻量级HTML5图表绘制工具库(知识整理)
2018/05/22 Javascript
小程序getLocation需要在app.json中声明permission字段
2019/04/04 Javascript
[42:23]完美世界DOTA2联赛PWL S3 Forest vs Rebirth 第二场 12.10
2020/12/13 DOTA
Python实现遍历windows所有窗口并输出窗口标题的方法
2015/03/13 Python
剖析Django中模版标签的解析与参数传递
2015/07/21 Python
浅谈pycharm出现卡顿的解决方法
2018/12/03 Python
python可视化实现代码
2019/01/15 Python
Python Web程序搭建简单的Web服务器
2019/07/31 Python
python numpy库np.percentile用法说明
2020/06/08 Python
CSS3不透明度实例讲解
2016/04/26 HTML / CSS
HTML5 form标签之解放表单验证、增加文件上传、集成拖放的使用方法
2013/04/24 HTML / CSS
Baby Tulai澳大利亚:美国婴儿背带品牌
2018/10/15 全球购物
德国50岁以上交友网站:Lebensfreunde
2020/03/18 全球购物
可贵的沉默教学反思
2014/02/06 职场文书
《生命 生命》教学反思
2014/04/19 职场文书
软件工程毕业生自荐信
2014/07/04 职场文书
红色旅游心得体会
2014/09/03 职场文书
奥巴马上海演讲稿
2014/09/10 职场文书
保外就医申请书范文
2015/08/06 职场文书
学习《中小学教师职业道德规范》心得体会
2016/01/18 职场文书