python将四元数变换为旋转矩阵的实例

yipeiwu_com5年前Python基础

如下所示:

import numpy as np
from autolab_core import RigidTransform

# 写上用四元数表示的orientation和xyz表示的position
orientation = {'y': -0.6971278819736084, 'x': -0.716556549511624, 'z': -0.010016582945017661, 'w': 0.02142651612120239}
position = {'y': -0.26022684372145516, 'x': 0.6453529828252734, 'z': 1.179122068068349}

rotation_quaternion = np.asarray([orientation['w'], orientation['x'], orientation['y'], orientation['z']])
translation = np.asarray([position['x'], position['y'], position['z']])
# 这里用的是UC Berkeley的autolab_core,比较方便吧,当然可以自己写一个fuction来计算,计算公式在https://www.cnblogs.com/flyinggod/p/8144100.html
T_qua2rota = RigidTransform(rotation_quaternion, translation)

print(T_qua2rota)
 
# 以下是打印的结果
Tra: [ 0.64535298 -0.26022684 1.17912207]
   Rot: [[ 0.02782477 0.99949234 -0.01551915]
   [ 0.99863386 -0.02710724 0.0446723 ]
   [ 0.04422894 -0.01674094 -0.99888114]]
   Qtn: [-0.02142652 0.71655655 0.69712788 0.01001658]
   from unassigned to world

自己写的话

def quaternion_to_rotation_matrix(quat):
  q = quat.copy()
  n = np.dot(q, q)
  if n < np.finfo(q.dtype).eps:
    return np.identity(4)
  q = q * np.sqrt(2.0 / n)
  q = np.outer(q, q)
  rot_matrix = np.array(
    [[1.0 - q[2, 2] - q[3, 3], q[1, 2] + q[3, 0], q[1, 3] - q[2, 0], 0.0],
     [q[1, 2] - q[3, 0], 1.0 - q[1, 1] - q[3, 3], q[2, 3] + q[1, 0], 0.0],
     [q[1, 3] + q[2, 0], q[2, 3] - q[1, 0], 1.0 - q[1, 1] - q[2, 2], 0.0],
     [0.0, 0.0, 0.0, 1.0]],
    dtype=q.dtype)
  return rot_matrix

描述有两种方式,即XYZABC和XYZ+quaternion:

https://doc.rc-visard.com/latest/de/pose_formats.html?highlight=format

以上这篇python将四元数变换为旋转矩阵的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python中for循环输出列表索引与对应的值方法

python中for循环输出列表索引与对应的值方法

如下所示: list = [‘a','b','c'] 想用for循环输出list的元素以及对应的索引。 代码及结果如下: 以上这篇python中for循环输出列表索引与对应的值...

python实现车牌识别的示例代码

python实现车牌识别的示例代码

某天回家之时,听到有个朋友说起他正在做一个车牌识别的项目 于是对其定位车牌的位置算法颇有兴趣,今日有空得以研究,事实上车牌识别算是比较成熟的技术了, 这里我只是简单实现。 我的思路为:...

Python随机函数库random的使用方法详解

Python随机函数库random的使用方法详解

前言 众所周知,python拥有丰富的内置库,还支持众多的第三方库,被称为胶水语言,随机函数库random,就是python自带的标准库,他的用法极为广泛,除了生成比较简单的随机数外,还...

python读取word文档的方法

本文实例讲述了python读取word文档的方法。分享给大家供大家参考。具体如下: 首先下载安装win32com from win32com import client as wc...

用python + hadoop streaming 分布式编程(一) -- 原理介绍,样例程序与本地调试

用python + hadoop streaming 分布式编程(一) -- 原理介绍,样例程序与本地调试

MapReduce与HDFS简介 什么是Hadoop? Google为自己的业务需要提出了编程模型MapReduce和分布式文件系统Google File System,并发布了相关论文...