在制作bundlefusion時,想測試TUM數據集,并且將groundtruth寫入到數據集中,TUM中給定的groundtruth中的旋轉是使用四元數表示的,而bundlefusion中需要SE3的形式,所以我需要首先將四元數轉換為旋轉矩陣,然后再將其與平移向量合并在一起,因為我之前關于生成bundlefusion數據集寫了一些python腳本,所以這次仍然想在原來的腳本上完成這項任務,所以我就在網上搜索,實現方法,最終我成功解決這個問題的方法如下:
最近買了個vpn,所以我可以瀟灑的在google中搜索 quaternion to rotation matrix python, 然后 就找到了這個
https://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.transform.Rotation.html
我安裝了conda,所以首先我使用下面這條指令安裝scipy
conda install scipy
安裝之后,我在pycharm上編輯代碼,運行一下,提示說找不到scipy這個模塊,我很納悶,這是咋回事,我命名安裝了呀,
我下意識的感覺到了什么, 我的pycharm鏈接到的是本地的python3.7,而我是在conda的base的環境下安裝的scipy,所以我在python 的setting中更改了,連接的python版本
from scipy.spatial.transform import Rotation as R
?就是這個地方,改了之后,pycharm就可以識別scipy了.
下面是我的測試代碼,里面還包括了,numpy中的矩陣的合并,設置數據類型等知識點,這些知識正好是今天早晨看morvan python學到的.
https://morvanzhou.github.io/tutorials/data-manipulation/np-pd/2-6-np-concat/?
?
from scipy.spatial.transform import Rotation as R
import numpy as np
print('test')
# use [:, np.newaxis] to transform from row vector to col vector
position = np.array([0.6453529828252734, -0.26022684372145516, 1.179122068068349])[:, np.newaxis]
share_vector = np.array([0,0,0,1], dtype=float)[np.newaxis, :]
print('share_vector:\n', share_vector)
print('position:\n',position)
r = R.from_quat([-0.716556549511624,-0.6971278819736084, -0.010016582945017661, 0.02142651612120239])
r.as_matrix()
print('rotation:\n',r.as_matrix())
rotation_matrix = r.as_matrix()
print(rotation_matrix)#combine three matrix or vector together
m34 = np.concatenate((rotation_matrix, position), axis = 1)
print(m34)
m44 = np.concatenate((m34, share_vector), axis=0)
# m44 = np.hstack((m34, share_vector))print(m44)rot_vec = r.as_rotvec()
print('rot_vec:\n', rot_vec)
rot_euler = r.as_euler('zyx', degrees = False)
print('rot_euler:\n',rot_euler)r = R.from_matrix(rotation_matrix)
print('as_quat():\n',r.as_quat())
print('as_rotvec():\n', r.as_rotvec())
print('as_euler():\n', r.as_euler('zyx', degrees=True))
?