blender获取相机内外参

import bpy
import mathutils

def get_camera_params(camera_object):
    # 获取相机的外参(旋转矩阵 R 和平移向量 T)
    location = camera_object.location  # 相机的位置向量 T
    rotation_euler = camera_object.rotation_euler  # 相机的旋转(欧拉角)

    # 欧拉角转换为旋转矩阵 R
    rotation_matrix = rotation_euler.to_matrix()

    # 旋转矩阵和位置向量即为外参 [R | T]
    R = rotation_matrix
    T = location

    # 获取相机的内参 (焦距、传感器尺寸、图像尺寸等)
    scene = bpy.context.scene
    render = scene.render

    # 获取相机的传感器尺寸(以毫米为单位)
    sensor_width = camera_object.data.sensor_width  # 传感器宽度
    sensor_height = camera_object.data.sensor_height  # 传感器高度

    # 获取相机的焦距(以毫米为单位)
    focal_length = camera_object.data.lens

    # 获取图像分辨率(以像素为单位)
    resolution_x = render.resolution_x
    resolution_y = render.resolution_y

    # 计算内参矩阵 K
    f_x = focal_length * resolution_x / sensor_width
    f_y = focal_length * resolution_y / sensor_height
    c_x = resolution_x / 2  # 假设主点在图像中心
    c_y = resolution_y / 2  # 假设主点在图像中心

    K = mathutils.Matrix([
        [f_x, 0, c_x],
        [0, f_y, c_y],
        [0, 0, 1]
    ])

    return K, R, T

# 获取当前场景中的相机对象
camera_object = bpy.context.scene.camera

# 获取相机的内外参数
K, R, T = get_camera_params(camera_object)

# 打印结果
print("相机内参矩阵 K:")
print(K)
print("\n相机旋转矩阵 R:")
print(R)
print("\n相机平移向量 T:")
print(T)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值