python 和c++实现旋转矩阵到欧拉角的变换方式

 更新时间:2019年12月04日 09:10:51   作者:爰采麦矣  
今天小编就为大家分享一篇python 和c++实现旋转矩阵到欧拉角的变换方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

在摄影测量学科中,国际摄影测量遵循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;
}

#!/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 matplotlib 画dataframe的时间序列图实例

    python matplotlib 画dataframe的时间序列图实例

    今天小编就为大家分享一篇python matplotlib 画dataframe的时间序列图实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-11-11
  • Python通过内置函数和自写算法DFS实现排列组合

    Python通过内置函数和自写算法DFS实现排列组合

    这篇文章主要介绍了Python通过内置函数和自写算法DFS实现排列组合,排列组合是数学中的一种常见的计算方法,用于求出从给定的元素中选取若干个元素的所有可能的排列或组合。在Python中,有多种方式可以实现排列组合的计算,需要的朋友可以参考下
    2023-05-05
  • Python 3.8中实现functools.cached_property功能

    Python 3.8中实现functools.cached_property功能

    这篇文章主要介绍了Python 3.8中实现functools.cached_property功能,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-05-05
  • python中的数据结构比较

    python中的数据结构比较

    这篇文章主要介绍了python中的数据结构比较,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-05-05
  • Python中的collections集合与typing数据类型模块

    Python中的collections集合与typing数据类型模块

    这篇文章介绍了Python中的collections集合与typing数据类型模块,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-05-05
  • Python的装饰器模式与面向切面编程详解

    Python的装饰器模式与面向切面编程详解

    这篇文章主要介绍了Python的装饰器模式与面向切面编程详解,概括的讲,装饰器的作用就是为已经存在的对象添加额外的功能,本文详细了装饰器模式的方方面面,然后引出面向切面编程知识,需要的朋友可以参考下
    2015-06-06
  • Python灰度变换中伽马变换分析实现

    Python灰度变换中伽马变换分析实现

    灰度变换是指根据某种目标条件按一定变换关系逐点改变源图像中每个像素灰度值的方法。目的是改善画质,使图像显示效果更加清晰。图像的灰度变换处理是图像增强处理技术中的一种非常基础、直接的空间域图像处理方法,也是图像数字化软件和图像显示软件的一个重要组成部分
    2022-10-10
  • Window10下python3.7 安装与卸载教程图解

    Window10下python3.7 安装与卸载教程图解

    本文通过图文并茂的形式给大家介绍了WINDOW10下PYTHON3.7 安装与卸载,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-09-09
  • python基础教程之对象和类的实际运用

    python基础教程之对象和类的实际运用

    这篇文章主要介绍了python基础教程之对象和类的实际运用,本文讲解对象和类的一方法技巧,例如属性、内置方法、self关键字的运用等,需要的朋友可以参考下
    2014-08-08
  • Python动态创建类实例详解

    Python动态创建类实例详解

    这篇文章主要为大家介绍了Python动态创建类实例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-12-12

最新评论