对相机位姿 导出 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)
相关推荐
raoxiaoya1 分钟前
ADK-Go:Golang开发AI Agent
开发语言·人工智能·golang
Jay20021114 分钟前
【机器学习】28-29 推荐系统 & 推荐系统实现
人工智能·python·机器学习
_oP_i5 分钟前
常见、主流、可靠的机器学习与深度学习训练集网站
人工智能·深度学习·机器学习
我很哇塞耶6 分钟前
Gemini手搓3D粒子特效,体验现实版钢铁侠
人工智能·ai·大模型
AI 搜索引擎技术7 分钟前
智能电网中的AI Agent负载均衡
运维·人工智能·ai·负载均衡
AI大模型产品经理10 分钟前
混合专家模型MoE的全面指南(二)路由机制、负载均衡
人工智能·ai·语言模型·大模型·llm·ai大模型
Dev7z13 分钟前
基于薄板样条与自由形变的非刚体图像拼接系统设计与实现
人工智能·计算机视觉
玖日大大14 分钟前
Cherry Studio:全场景 AI 生产力平台深度解析 —— 从个人助手到企业级解决方案
人工智能
Java中文社群15 分钟前
崩溃了!N8N升级后插件全变红?教你2招完美解决!
人工智能
wshzd16 分钟前
LLM之Agent(三十九)|AI Agents(八):构建Multi-Agent系统
人工智能·microsoft