def box_nuscenes_to_kitti(box: Box, velo_to_cam_rot: Quaternion, velo_to_cam_trans: np.ndarray, r0_rect: Quaternion, kitti_to_nu_lidar_inv: Quaternion = Quaternion(axis=(0, 0, 1), angle=np.pi / 2).inverse) \ -> Box: """ Transform from nuScenes lidar frame to KITTI reference frame. :param box: Instance in nuScenes lidar frame. :param velo_to_cam_rot: Quaternion to rotate from lidar to camera frame. :param velo_to_cam_trans: <np.float: 3>. Translate from lidar to camera frame. :param r0_rect: Quaternion to rectify camera frame. :param kitti_to_nu_lidar_inv: Quaternion to rotate nuScenes to KITTI LIDAR. :return: Box instance in KITTI reference frame. """ # Copy box to avoid side-effects. box = box.copy() # Rotate to KITTI lidar. box.rotate(kitti_to_nu_lidar_inv) # Transform to KITTI camera. box.rotate(velo_to_cam_rot) box.translate(velo_to_cam_trans) # Rotate to KITTI rectified camera. box.rotate(r0_rect) # KITTI defines the box center as the bottom center of the object. # We use the true center, so we need to adjust half height in y direction. box.translate(np.array([0, box.wlh[2] / 2, 0])) return box
def project_kitti_box_to_image( box: Box, p_left: np.ndarray, imsize: Tuple[int, int]) -> Tuple[int, int, int, int]: """ Projects 3D box into KITTI image FOV. :param box: 3D box in KITTI reference frame. :param p_left: <np.float: 3, 4>. Projection matrix. :param imsize: (width , height). Image size. :return: (xmin, ymin, xmax, ymax). Bounding box in image plane. """ # Create a new box. box = box.copy() # KITTI defines the box center as the bottom center of the object. # We use the true center, so we need to adjust half height in negative y direction. box.translate(np.array([0, -box.wlh[2] / 2, 0])) # Project corners to 2d to get bbox in pixel coords. corners = np.array( [corner for corner in box.corners().T if corner[2] > 0]).T imcorners = view_points(corners, p_left, normalize=True)[:2] bbox = (np.min(imcorners[0]), np.min(imcorners[1]), np.max(imcorners[0]), np.max(imcorners[1])) # Crop bbox to prevent it extending outside image bbox_crop = (max(0, bbox[0]), max(0, bbox[1]), min(imsize[0], bbox[2]), min(imsize[1], bbox[3])) return bbox_crop
def project_kitti_box_to_image(box: Box, p_left: np.ndarray, imsize: Tuple[int, int]) \ -> Union[None, Tuple[int, int, int, int]]: """ Projects 3D box into KITTI image FOV. :param box: 3D box in KITTI reference frame. :param p_left: <np.float: 3, 4>. Projection matrix. :param imsize: (width, height). Image size. :return: (xmin, ymin, xmax, ymax). Bounding box in image plane or None if box is not in the image. """ # Create a new box. box = box.copy() # KITTI defines the box center as the bottom center of the object. # We use the true center, so we need to adjust half height in negative y direction. box.translate(np.array([0, -box.wlh[2] / 2, 0])) # Check that some corners are inside the image. corners = np.array( [corner for corner in box.corners().T if corner[2] > 0]).T if len(corners) == 0: return None # Project corners that are in front of the camera to 2d to get bbox in pixel coords. imcorners = view_points(corners, p_left, normalize=True)[:2] bbox = (np.min(imcorners[0]), np.min(imcorners[1]), np.max(imcorners[0]), np.max(imcorners[1])) # Crop bbox to prevent it extending outside image. bbox_crop = tuple(max(0, b) for b in bbox) bbox_crop = (min(imsize[0], bbox_crop[0]), min(imsize[0], bbox_crop[1]), min(imsize[0], bbox_crop[2]), min(imsize[1], bbox_crop[3])) # Detect if a cropped box is empty. if bbox_crop[0] >= bbox_crop[2] or bbox_crop[1] >= bbox_crop[3]: return None return bbox_crop