|
""" |
|
the blender script to get the camera intrinsic matrix |
|
""" |
|
|
|
import bpy |
|
import numpy as np |
|
|
|
def get_calibration_matrix_K_from_blender(mode='simple'): |
|
|
|
scene = bpy.context.scene |
|
|
|
scale = scene.render.resolution_percentage / 100 |
|
width = scene.render.resolution_x * scale |
|
height = scene.render.resolution_y * scale |
|
|
|
camdata = scene.camera.data |
|
|
|
if mode == 'simple': |
|
aspect_ratio = width / height |
|
K = np.zeros((3,3), dtype=np.float32) |
|
K[0][0] = width / 2 / np.tan(camdata.angle / 2) |
|
K[1][1] = height / 2. / np.tan(camdata.angle / 2) * aspect_ratio |
|
K[0][2] = width / 2. |
|
K[1][2] = height / 2. |
|
K[2][2] = 1. |
|
K.transpose() |
|
|
|
if mode == 'complete': |
|
|
|
focal = camdata.lens |
|
sensor_width = camdata.sensor_width |
|
sensor_height = camdata.sensor_height |
|
pixel_aspect_ratio = scene.render.pixel_aspect_x / scene.render.pixel_aspect_y |
|
|
|
if (camdata.sensor_fit == 'VERTICAL'): |
|
|
|
|
|
s_u = width / sensor_width / pixel_aspect_ratio |
|
s_v = height / sensor_height |
|
else: |
|
|
|
|
|
pixel_aspect_ratio = scene.render.pixel_aspect_x / scene.render.pixel_aspect_y |
|
s_u = width / sensor_width |
|
s_v = height * pixel_aspect_ratio / sensor_height |
|
|
|
|
|
alpha_u = focal * s_u |
|
alpha_v = focal * s_v |
|
u_0 = width / 2 |
|
v_0 = height / 2 |
|
skew = 0 |
|
|
|
K = np.array([ |
|
[alpha_u, skew, u_0], |
|
[ 0, alpha_v, v_0], |
|
[ 0, 0, 1] |
|
], dtype=np.float32) |
|
|
|
return K |
|
|
|
K = get_calibration_matrix_K_from_blender('complete') |
|
|
|
|
|
np.savetxt('./camera_para.txt', K) |