可视化相机pose colmap形式的相机内参外参

目录

内参外参转换

[可视化相机pose colmap形式的相机内参外参](#可视化相机pose colmap形式的相机内参外参)


内参外参转换

python 复制代码
def visualize_cameras(cameras, images):
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')

    for image_id, image_data in images.items():
        qvec = image_data['qvec']
        tvec = image_data['tvec']

        # Convert quaternion to rotation matrix
        rotation = R.from_quat(qvec).as_matrix()

        # Plot camera position
        ax.scatter(tvec[0], tvec[1], tvec[2], c='r', marker='o')

        # Plot camera orientation
        camera_direction = rotation @ np.array([0, 0, 1])
        ax.quiver(tvec[0], tvec[1], tvec[2], camera_direction[0], camera_direction[1], camera_direction[2], length=0.5, normalize=True)

    ax.set_xlabel('X')
    ax.set_ylabel('Y')
    ax.set_zlabel('Z')
    plt.show()

这段代码用于在3D坐标系中可视化相机的位置和朝向。以下是逐行解释:

  1. 提取参数

    复制代码
    qvec = image_data['qvec']  # 相机的旋转四元数 (w, x, y, z 或 x, y, z, w,需确认顺序)
    tvec = image_data['tvec']  # 相机的平移向量 (x, y, z 坐标)
  2. 四元数转旋转矩阵

    python 复制代码
    rotation = R.from_quat(qvec).as_matrix()  # 将四元数转换为3x3旋转矩阵
    • 假设 R 来自 scipy.spatial.transform.Rotation

    • 需确认 qvec 的顺序是否为库预期的格式(通常 R.from_quat 接受 (x, y, z, w))。

  3. 绘制相机位置

    python 复制代码
    ax.scatter(tvec[0], tvec[1], tvec[2], c='r', marker='o')  # 在3D图中用红点标记相机位置
  4. 计算并绘制相机朝向

    python 复制代码
    camera_direction = rotation @ np.array([0, 0, 1])  # 旋转矩阵乘以Z轴单位向量,得到相机在世界坐标系中的朝向
    ax.quiver(tvec[0], tvec[1], tvec[2], 
              camera_direction[0], camera_direction[1], camera_direction[2], 
              length=0.5, normalize=True)
    • 原理:相机坐标系中默认朝向为Z轴正方向(通常指向拍摄方向),通过旋转矩阵将其转换到世界坐标系。

    • 箭头参数

      • 起点为相机位置 (tvec[0], tvec[1], tvec[2])

      • 方向向量为 camera_direction

      • length=0.5 控制箭头显示长度(实际长度可能因归一化调整)。

      • normalize=True 确保箭头方向正确,长度统一。

注意事项

  • 四元数顺序 :确认 qvec 是否与 R.from_quat 兼容(SciPy需 (x, y, z, w))。

  • 坐标系定义 :假设相机朝向为Z轴正方向,若实际定义相反(如OpenGL使用-Z),需调整为 [0, 0, -1]

  • 3D绘图设置 :确保 ax 是3D轴(例如通过 fig.add_subplot(111, projection='3d') 创建)。

效果:在3D图中,红色圆点表示相机位置,箭头指示其拍摄方向。

可视化相机pose colmap形式的相机内参外参

python 复制代码
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from scipy.spatial.transform import Rotation as R
def read_cameras(file_path):
    cameras = {}
    with open(file_path, 'r') as file:
        for line in file:
            if line[0] == '#':
                continue
            parts = line.strip().split()
            camera_id = int(parts[0])
            model = parts[1]
            width = int(parts[2])
            height = int(parts[3])
            params = np.array([float(p) for p in parts[4:]])
            cameras[camera_id] = {
                'model': model,
                'width': width,
                'height': height,
                'params': params
            }
    return cameras

def read_images(file_path):
    images = {}
    with open(file_path, 'r') as file:
        for line in file:
            if line[0] == '#':
                continue
            parts = line.strip().split()
            if len(parts) == 15:
                continue
            image_id = int(parts[0])
            qvec = np.array([float(p) for p in parts[1:5]])
            tvec = np.array([float(p) for p in parts[5:8]])
            camera_id = int(parts[8])
            file_name = parts[9]
            images[image_id] = {
                'qvec': qvec,
                'tvec': tvec,
                'camera_id': camera_id,
                'file_name': file_name
            }
    return images

def visualize_cameras(cameras, images):
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')

    for image_id, image_data in images.items():
        qvec = image_data['qvec']
        tvec = image_data['tvec']

        # Convert quaternion to rotation matrix
        rotation = R.from_quat(qvec).as_matrix()

        # Plot camera position
        ax.scatter(tvec[0], tvec[1], tvec[2], c='r', marker='o')

        # Plot camera orientation
        camera_direction = rotation @ np.array([0, 0, 1])
        ax.quiver(tvec[0], tvec[1], tvec[2], camera_direction[0], camera_direction[1], camera_direction[2], length=0.5, normalize=True)

    ax.set_xlabel('X')
    ax.set_ylabel('Y')
    ax.set_zlabel('Z')
    plt.show()

# 示例使用
cameras = read_cameras('./cameras.txt')
images = read_images('./images.txt')
visualize_cameras(cameras, images)
相关推荐
B站_计算机毕业设计之家30 分钟前
豆瓣电影数据采集分析推荐系统 | Python Vue Flask框架 LSTM Echarts多技术融合开发 毕业设计源码 计算机
vue.js·python·机器学习·flask·echarts·lstm·推荐算法
渣渣苏38 分钟前
Langchain实战快速入门
人工智能·python·langchain
lili-felicity1 小时前
CANN模型量化详解:从FP32到INT8的精度与性能平衡
人工智能·python
数据知道1 小时前
PostgreSQL实战:详解如何用Python优雅地从PG中存取处理JSON
python·postgresql·json
ZH15455891311 小时前
Flutter for OpenHarmony Python学习助手实战:面向对象编程实战的实现
python·学习·flutter
玄同7651 小时前
SQLite + LLM:大模型应用落地的轻量级数据存储方案
jvm·数据库·人工智能·python·语言模型·sqlite·知识图谱
User_芊芊君子1 小时前
CANN010:PyASC Python编程接口—简化AI算子开发的Python框架
开发语言·人工智能·python
白日做梦Q1 小时前
Anchor-free检测器全解析:CenterNet vs FCOS
python·深度学习·神经网络·目标检测·机器学习
喵手2 小时前
Python爬虫实战:公共自行车站点智能采集系统 - 从零构建生产级爬虫的完整实战(附CSV导出 + SQLite持久化存储)!
爬虫·python·爬虫实战·零基础python爬虫教学·采集公共自行车站点·公共自行车站点智能采集系统·采集公共自行车站点导出csv
喵手2 小时前
Python爬虫实战:地图 POI + 行政区反查实战 - 商圈热力数据准备完整方案(附CSV导出 + SQLite持久化存储)!
爬虫·python·爬虫实战·零基础python爬虫教学·地区poi·行政区反查·商圈热力数据采集