Example #1
0
    def draw_bboxes(self,
                    bbox3d,
                    bbox_color=(0, 1, 0),
                    points_in_box_color=(1, 0, 0),
                    rot_axis=2,
                    center_mode='lidar_bottom',
                    cls_names=None):
        """Draw bbox on visualizer and change the color of points inside bbox3d.

        Args:
            bbox3d (numpy.array | torch.tensor, shape=[M, 7]):
                3d bbox (x, y, z, dx, dy, dz, yaw) to visualize.
            points_colors (numpy.array): color of each points.
            pcd (:obj:`open3d.geometry.PointCloud`): point cloud. Default: None.
            bbox_color (tuple[float]): the color of bbox. Default: (0, 1, 0).
            points_in_box_color (tuple[float], or list[tuple[float]]):
                the color of points inside each bbox3d. Default: (1, 0, 0).
            rot_axis (int): rotation axis of bbox. Default: 2.
            center_mode (bool): indicate the center of bbox is bottom center
                or gravity center. avaliable mode
                ['lidar_bottom', 'camera_bottom']. Default: 'lidar_bottom'.
        """
        if isinstance(bbox3d, torch.Tensor):
            bbox3d = bbox3d.cpu().numpy()
        bbox3d = bbox3d.copy()

        for i in range(len(bbox3d)):
            if isinstance(points_in_box_color, list):
                in_box_color = np.array(points_in_box_color[i])
            else:
                in_box_color = np.array(points_in_box_color)
            center = bbox3d[i, 0:3]
            dim = bbox3d[i, 3:6]
            yaw = np.zeros(3)
            yaw[rot_axis] = -bbox3d[i, 6]
            rot_mat = geometry.get_rotation_matrix_from_xyz(yaw)

            if center_mode == 'lidar_bottom':
                center[rot_axis] += dim[
                    rot_axis] / 2  # bottom center to gravity center
            elif center_mode == 'camera_bottom':
                center[rot_axis] -= dim[
                    rot_axis] / 2  # bottom center to gravity center
            box3d = geometry.OrientedBoundingBox(center, rot_mat, dim)

            line_set = geometry.LineSet.create_from_oriented_bounding_box(
                box3d)
            cls_name = cls_names[i]
            ps_line_set = ps.register_curve_network(
                f'box{self.bbox_count}-{cls_names[i]}',
                np.array(line_set.points),
                np.array(line_set.lines),
                radius=0.0003,
                color=np.array(bbox_color))
            self.bbox_count += 1

            # change the color of points which are in box
            indices = box3d.get_point_indices_within_bounding_box(
                o3d.utility.Vector3dVector(self.bg_points))
            points_in_box = self.bg_points[indices]
            self.draw_points(points_in_box, cls_name)
            self.bg_points = np.delete(self.bg_points, indices, axis=0)
            ps.register_point_cloud('background', self.bg_points)
Example #2
0
def _draw_bboxes(bbox3d,
                 vis,
                 points_colors,
                 pcd=None,
                 bbox_color=(0, 1, 0),
                 points_in_box_color=(1, 0, 0),
                 rot_axis=2,
                 center_mode='lidar_bottom',
                 mode='xyz'):
    """Draw bbox on visualizer and change the color of points inside bbox3d.

    Args:
        bbox3d (numpy.array | torch.tensor, shape=[M, 7]):
            3d bbox (x, y, z, dx, dy, dz, yaw) to visualize.
        vis (:obj:`open3d.visualization.Visualizer`): open3d visualizer.
        points_colors (numpy.array): color of each points.
        pcd (:obj:`open3d.geometry.PointCloud`): point cloud. Default: None.
        bbox_color (tuple[float]): the color of bbox. Default: (0, 1, 0).
        points_in_box_color (tuple[float]):
            the color of points inside bbox3d. Default: (1, 0, 0).
        rot_axis (int): rotation axis of bbox. Default: 2.
        center_mode (bool): indicate the center of bbox is bottom center
            or gravity center. avaliable mode
            ['lidar_bottom', 'camera_bottom']. Default: 'lidar_bottom'.
        mode (str):  indicate type of the input points, avaliable mode
            ['xyz', 'xyzrgb']. Default: 'xyz'.
    """
    if isinstance(bbox3d, torch.Tensor):
        bbox3d = bbox3d.cpu().numpy()
    bbox3d = bbox3d.copy()

    in_box_color = np.array(points_in_box_color)
    for i in range(len(bbox3d)):
        center = bbox3d[i, 0:3]
        dim = bbox3d[i, 3:6]
        yaw = np.zeros(3)
        yaw[rot_axis] = -bbox3d[i, 6]
        rot_mat = geometry.get_rotation_matrix_from_xyz(yaw)

        if center_mode == 'lidar_bottom':
            center[rot_axis] += dim[
                rot_axis] / 2  # bottom center to gravity center
        elif center_mode == 'camera_bottom':
            center[rot_axis] -= dim[
                rot_axis] / 2  # bottom center to gravity center
        box3d = geometry.OrientedBoundingBox(center, rot_mat, dim)

        line_set = geometry.LineSet.create_from_oriented_bounding_box(box3d)
        line_set.paint_uniform_color(bbox_color)
        # draw bboxes on visualizer
        vis.add_geometry(line_set)

        # change the color of points which are in box
        if pcd is not None and mode == 'xyz':
            indices = box3d.get_point_indices_within_bounding_box(pcd.points)
            points_colors[indices] = in_box_color

    # update points colors
    if pcd is not None:
        pcd.colors = o3d.utility.Vector3dVector(points_colors)
        vis.update_geometry(pcd)