对相机位姿 导出 Tum 格式的位姿

需求:针对 N,4,4 格式的 poses np.darray 导出其 Tum 格式 的位姿。

时间戳根据 N 的值,线性得到。

python 复制代码
import numpy as np
import os
import torch
from scipy.spatial.transform import Rotation

def rotation_matrix_to_tum_format(rotation_matrix):
    rotation = Rotation.from_matrix(rotation_matrix)
    quaternion = rotation.as_quat()
    return quaternion

def convert_to_tum_format(poses, timestamps):
    tum_poses = []
    for i in range(poses.shape[0]):
        pose = poses[i]
        quaternion = rotation_matrix_to_tum_format(pose[:3, :3])
        tum_timestamp = timestamps[i] * 0.1  # Scaling factor of 0.1 to convert timestamps to seconds
        tum_pose = f"{tum_timestamp:.6f} {' '.join(map(str, pose[:3, 3]))} {' '.join(map(str, quaternion))}" 
        tum_poses.append(tum_pose)
    return tum_poses
def write_tum_poses_to_file(file_path, tum_poses):
    with open(file_path, 'w') as f:
        for pose in tum_poses:
            f.write(pose + '\n')
def convert_and_write_tum_poses(c2w_variable, output_filename, timestamps):
    # 调用适当的函数将变量转换为 TUM 格式
    tum_poses = convert_to_tum_format(c2w_variable, timestamps)
    
    # 将 TUM 格式的位姿写入文件
    write_tum_poses_to_file(output_filename, tum_poses)
n_poses = c2w_GT_traj.shape[0]
custom_timestamps = np.arange(n_poses)
convert_and_write_tum_poses(c2w_GT_traj, 'tum_c2w_GT_traj.txt', custom_timestamps)
相关推荐
延凡科技1 分钟前
数字孪生全域智慧园区综合可视化运营平台|延凡科技智园视界解决方案
数据库·人工智能·物联网·数据可视化
GeekArch7 分钟前
第5讲:嵌入式场景二范式决策树:什么情况快写、什么情况严写
人工智能·stm32·单片机·mcu·物联网·决策树·机器学习
MobotStone8 分钟前
AI写的代码看着全对、逻辑也自洽,为什么就是“不是我们想要的”?
人工智能
火山引擎开发者社区9 分钟前
Agent 时代的 Serverless 入口:veFaaS CLI 实战
人工智能
aneasystone本尊13 分钟前
学习 OpenMontage 的工具发现与打分选择器
人工智能
火山引擎开发者社区15 分钟前
Computer Use Agent 新能力上线,开启企业电脑自动化的新范式
人工智能
火山引擎开发者社区7 小时前
VeOps CLI:你的火山引擎可观测排障助手
人工智能
To_OC9 小时前
调用远程MCP,手搓一个能查酒店、自动打开浏览器的 Agent
人工智能·agent·mcp
启雀AI9 小时前
生物医疗行业如何建设合规、安全、可复用的知识库?
人工智能·安全·软件构建·知识图谱·知识库
x-cmd9 小时前
Mac 涨价后,本地 AI 还能千元入门吗?
linux·人工智能·macos·ai·agent·amd·本地ai入门