@@ -135,33 +135,42 @@ def capture_depth(self, in_meters=False) -> np.ndarray:
135135 self ._handle , self .resolution , in_meters )
136136
137137 def capture_pointcloud (self ) -> np .ndarray :
138- """Retrieves pointcloud in word frame.
138+ """Retrieves point cloud in word frame.
139139
140140 :return: A numpy array of size (width, height, 3)
141141 """
142+ d = self .capture_depth (in_meters = True )
143+ return self .pointcloud_from_depth (d )
142144
143- res = np .array (self .get_resolution ())
145+ def pointcloud_from_depth (self , depth : np .ndarray ) -> np .ndarray :
146+ """Converts depth (in meters) to point cloud in word frame.
144147
148+ :return: A numpy array of size (width, height, 3)
149+ """
145150 intrinsics = self .get_intrinsic_matrix ()
146- d = self .capture_depth (in_meters = True )
147- upc = _create_uniform_pixel_coords_image (d .shape )
148- pc = upc * np .expand_dims (d , - 1 )
151+ return VisionSensor .pointcloud_from_depth_and_camera_params (
152+ depth , self .get_matrix (), intrinsics )
149153
150- # Get extrinsics
151- C = np .expand_dims (self .get_position (), 0 ).T
152- R = self .get_matrix ()[:- 1 , :- 1 ]
154+ @staticmethod
155+ def pointcloud_from_depth_and_camera_params (
156+ depth : np .ndarray , extrinsics : np .ndarray ,
157+ intrinsics : np .ndarray ) -> np .ndarray :
158+ """Converts depth (in meters) to point cloud in word frame.
159+ :return: A numpy array of size (width, height, 3)
160+ """
161+ upc = _create_uniform_pixel_coords_image (depth .shape )
162+ pc = upc * np .expand_dims (depth , - 1 )
163+ C = np .expand_dims (extrinsics [:3 , 3 ], 0 ).T
164+ R = extrinsics [:3 , :3 ]
153165 R_inv = R .T # inverse of rot matrix is transpose
154166 R_inv_C = np .matmul (R_inv , C )
155167 extrinsics = np .concatenate ((R_inv , - R_inv_C ), - 1 )
156-
157168 cam_proj_mat = np .matmul (intrinsics , extrinsics )
158169 cam_proj_mat_homo = np .concatenate (
159170 [cam_proj_mat , [np .array ([0 , 0 , 0 , 1 ])]])
160171 cam_proj_mat_inv = np .linalg .inv (cam_proj_mat_homo )[0 :3 ]
161-
162172 world_coords_homo = np .expand_dims (_pixel_to_world_coords (
163173 pc , cam_proj_mat_inv ), 0 )
164-
165174 world_coords = world_coords_homo [..., :- 1 ][0 ]
166175 return world_coords
167176
0 commit comments