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)
blender获取相机内外参
最新推荐文章于 2025-02-03 00:35:22 发布