Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
get_camera_intrinsics.py
1import bpy
2from mathutils import Matrix
3
4# https://blender.stackexchange.com/questions/15102/what-is-blenders-camera-projection-matrix-model/38189#38189
6 """! Get the camera matrix from Blender
7 @param[in] cam_data Camera data.
8 @return Camera matrix K that contains intrinsic parameters.
9 """
10 f = cam_data.lens
11 scene = bpy.context.scene
12 resolution_x_in_px = scene.render.resolution_x
13 resolution_y_in_px = scene.render.resolution_y
14 scale = scene.render.resolution_percentage / 100
15 sensor_width = cam_data.sensor_width
16 sensor_height = cam_data.sensor_height
17 pixel_aspect_ratio = scene.render.pixel_aspect_x / scene.render.pixel_aspect_y
18 if (cam_data.sensor_fit == 'VERTICAL'):
19 # the sensor height is fixed (sensor fit is horizontal),
20 # the sensor width is effectively changed with the pixel aspect ratio
21 s_u = resolution_x_in_px * scale / sensor_width / pixel_aspect_ratio
22 s_v = resolution_y_in_px * scale / sensor_height
23 else: # 'HORIZONTAL' and 'AUTO'
24 # the sensor width is fixed (sensor fit is horizontal),
25 # the sensor height is effectively changed with the pixel aspect ratio
26 pixel_aspect_ratio = scene.render.pixel_aspect_x / scene.render.pixel_aspect_y
27 s_u = resolution_x_in_px * scale / sensor_width
28 s_v = resolution_y_in_px * scale * pixel_aspect_ratio / sensor_height
29
30 # Parameters of intrinsic calibration matrix K
31 p_x = f * s_u
32 p_y = f * s_v
33 u_0 = resolution_x_in_px*scale / 2
34 v_0 = resolution_y_in_px*scale / 2
35 skew = 0 # only use rectangular pixels
36
37 K = Matrix(
38 ((p_x, skew, u_0),
39 ( 0, p_y, v_0),
40 ( 0, 0, 1 )))
41 return K
42
43if __name__ == "__main__":
44 """! Python script that dials with Blender to retrieve camera intrinsic parameters.
45 """
46 # Modify your camera name below
47 # If you follow carefully the tutorial, you can set "Camera_L" for the color camera and "Camera_R" for the depth camera
48 camera_name = "Camera_R"
49 K = get_calibration_matrix_K_from_blender(bpy.data.objects[camera_name].data)
50 print(f"Intrinsics for {camera_name} are K = \n{K}")
get_calibration_matrix_K_from_blender(cam_data)
Get the camera matrix from Blender.