MCPcopy Create free account
hub / github.com/TencentARC/FreeSplatter / create_camera_to_world

Function create_camera_to_world

freesplatter/utils/camera_util.py:35–73  ·  view source on GitHub ↗

Create OpenCV or OpenGL camera extrinsics from camera locations and look-at position. camera_position: (M, 3) or (3,) look_at: (3) up_world: (3) return: (M, 3, 4) or (3, 4)

(camera_position: torch.Tensor, look_at: torch.Tensor = None, up_world: torch.Tensor = None, camera_system: str = 'opencv')

Source from the content-addressed store, hash-verified

33
34
35def create_camera_to_world(camera_position: torch.Tensor, look_at: torch.Tensor = None, up_world: torch.Tensor = None, camera_system: str = 'opencv'):
36 """
37 Create OpenCV or OpenGL camera extrinsics from camera locations and look-at position.
38
39 camera_position: (M, 3) or (3,)
40 look_at: (3)
41 up_world: (3)
42 return: (M, 3, 4) or (3, 4)
43 """
44 # by default, looking at the origin and world up is z-axis
45 if look_at is None:
46 look_at = torch.tensor([0, 0, 0], dtype=torch.float32)
47 if up_world is None:
48 up_world = torch.tensor([0, 0, 1], dtype=torch.float32)
49 if camera_position.ndim == 2:
50 look_at = look_at.unsqueeze(0).repeat(camera_position.shape[0], 1)
51 up_world = up_world.unsqueeze(0).repeat(camera_position.shape[0], 1)
52
53 assert camera_system in ['opencv', 'opengl']
54 if camera_system == 'opencv':
55 # OpenCV camera: z-forward, x-right, y-down
56 z_axis = look_at - camera_position
57 z_axis = normalize_vecs(z_axis).float()
58 x_axis = torch.cross(z_axis, up_world)
59 x_axis = normalize_vecs(x_axis).float()
60 y_axis = torch.cross(z_axis, x_axis)
61 y_axis = normalize_vecs(y_axis).float()
62 else:
63 # OpenGL camera: z-backward, x-right, y-up
64 z_axis = camera_position - look_at
65 z_axis = normalize_vecs(z_axis).float()
66 x_axis = torch.cross(up_world, z_axis)
67 x_axis = normalize_vecs(x_axis).float()
68 y_axis = torch.cross(z_axis, x_axis)
69 y_axis = normalize_vecs(y_axis).float()
70
71 extrinsics = torch.stack([x_axis, y_axis, z_axis, camera_position], dim=-1)
72 extrinsics = pad_camera_extrinsics_4x4(extrinsics)
73 return extrinsics
74
75
76def FOV_to_intrinsics(fov, device='cpu'):

Callers 4

normalize_camerasFunction · 0.85
get_circular_camerasFunction · 0.85
save_gaussianFunction · 0.85

Calls 3

normalize_vecsFunction · 0.70
crossMethod · 0.45

Tested by

no test coverage detected