对相机位姿 导出 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)
相关推荐
DP DPharness1 分钟前
拆开 dsh-knowledge 的检索链路,看 RRF 融合与锚点续读
人工智能·dpharness
I'm a winner13 分钟前
《AI 赋能嵌入式开发:从 0 到全栈工程师》模块1|第4课时
人工智能
johnsong18 分钟前
当决策成为免费商品:思维成本崩溃背后的治理真空
大数据·人工智能
ZhangJun9521 分钟前
在 32GB 内存电脑上本地搭建 Qwen3.6-35B-A3B 大模型踩坑实录
运维·人工智能·阿里云·ai·软件构建
南京码讯光电技术有限公司26 分钟前
How to Design an Antenna System for an Industrial WiFi Module
数据库·人工智能
秋名山码民29 分钟前
AI 用过一次,下一次怎样做得更好?——拙见 AI OS 的自适应、自迭代与受控演化
人工智能
回眸&啤酒鸭40 分钟前
DeepSeek 大模型落地应用与价值实现指南
人工智能
怕浪猫1 小时前
LLM 面试必问的 8 个问题,答不上来直接淘汰
人工智能·python·面试
deepseek231 小时前
Lathoa 拆解:让 AI 故意出错比答对更难,反向出题 harness 的三重校验与共模失效困局
人工智能·大模型·可靠性工程
红海云1 小时前
AI抬高了职场的第一道门槛
人工智能