MMDet3D——可视化库:3D Lidar Boxes坐标投影到Image上并可视化

本代码适用于mmdet3d默认生成的pkl获取到的数据,并使用类似于DETR3D pipline的代码。
代码参考:from mmdet3d.core.visualizer.image_vis import plot_rect3d_on_img, draw_lidar_bbox3d_on_img

可视化主函数

  • 需要img_metasgt_bboxes_3d即可完成画图功能;
  • 可以插在任意部分的代码中,只要有以上两个变量即可。
# -------------------------- vis -----------------------------
import cv2
img = cv2.imread(kwargs['img_metas'][0]['filename'][0]) # front 
r_h, r_w,_ = kwargs['img_metas'][0]['img_shape'][0]
img = cv2.resize(img, (r_w, r_h))
box = kwargs['gt_bboxes_3d'][0]
lidar2img_rt = kwargs['img_metas'][0]['lidar2img'][0]
plot_img = draw_lidar_bbox3d_on_img(box,
                                    raw_img=img,
                                    lidar2img_rt=lidar2img_rt,
                                    img_metas=None,
                                    color=(0, 255, 0),
                                    thickness=1)
cv2.imwrite("plot_img.jpg", plot_img)
# ------------------------------------------------------------

Lidar Box可视化功能函数

import cv2
def plot_rect3d_on_img(img,
                       num_rects,
                       rect_corners,
                       color=(0, 255, 0),
                       thickness=1):
    """Plot the boundary lines of 3D rectangular on 2D images.

    Args:
        img (numpy.array): The numpy array of image.
        num_rects (int): Number of 3D rectangulars.
        rect_corners (numpy.array): Coordinates of the corners of 3D
            rectangulars. Should be in the shape of [num_rect, 8, 2].
        color (tuple[int]): The color to draw bboxes. Default: (0, 255, 0).
        thickness (int, optional): The thickness of bboxes. Default: 1.
    """
    line_indices = ((0, 1), (0, 3), (0, 4), (1, 2), (1, 5), (3, 2), (3, 7),
                    (4, 5), (4, 7), (2, 6), (5, 6), (6, 7))
    # filter boxes out of range
    h,w,c = img.shape
    for i in range(num_rects):
        corners = rect_corners[i].astype(np.int)
        for idx, corner in enumerate(corners):
            corners[idx][0] = w if corner[0] > w else corner[0]
            corners[idx][0] = 0 if corner[0] < 0 else corner[0]
            corners[idx][1] = w if corner[1] > h else corner[1]
            corners[idx][1] = 0 if corner[1] < 0 else corner[1]
        # draw
        for start, end in line_indices:
            cv2.line(img, (corners[start, 0], corners[start, 1]),
                     (corners[end, 0], corners[end, 1]), color, thickness,
                     cv2.LINE_AA)

    return img.astype(np.uint8)

def draw_lidar_bbox3d_on_img(bboxes3d,
                             raw_img,
                             lidar2img_rt,
                             img_metas,
                             color=(0, 255, 0),
                             thickness=1):
    """Project the 3D bbox on 2D plane and draw on input image.

    Args:
        bboxes3d (:obj:`LiDARInstance3DBoxes`):
            3d bbox in lidar coordinate system to visualize.
        raw_img (numpy.array): The numpy array of image.
        lidar2img_rt (numpy.array, shape=[4, 4]): The projection matrix
            according to the camera intrinsic parameters.
        img_metas (dict): Useless here.
        color (tuple[int]): The color to draw bboxes. Default: (0, 255, 0).
        thickness (int, optional): The thickness of bboxes. Default: 1.
    """
    img = raw_img.copy()
    corners_3d = bboxes3d.corners
    num_bbox = corners_3d.shape[0]
    pts_4d = np.concatenate(
        [corners_3d.reshape(-1, 3),
         np.ones((num_bbox * 8, 1))], axis=-1)
    lidar2img_rt = copy.deepcopy(lidar2img_rt).reshape(4, 4)
    if isinstance(lidar2img_rt, torch.Tensor):
        lidar2img_rt = lidar2img_rt.cpu().numpy()
    pts_2d = pts_4d @ lidar2img_rt.T

    pts_2d[:, 2] = np.clip(pts_2d[:, 2], a_min=1e-5, a_max=1e5)
    pts_2d[:, 0] /= pts_2d[:, 2]
    pts_2d[:, 1] /= pts_2d[:, 2]
    imgfov_pts_2d = pts_2d[..., :2].reshape(num_bbox, 8, 2)

    return plot_rect3d_on_img(img, num_bbox, imgfov_pts_2d, color, thickness)

在这里插入图片描述

Image Pts可视化

def draw_pts_on_img(img, imgfov_pts_2d, thickness=3, color=[0,255,0]):
	'''
	imgfov_pts_2d: [n, 2] 
	'''
    if isinstance(imgfov_pts_2d, torch.Tensor):
        imgfov_pts_2d = imgfov_pts_2d.detach().cpu().numpy()
    
    h,w,c = img.shape
    for i in range(imgfov_pts_2d.shape[0]):
        if imgfov_pts_2d[i, 0] > w or imgfov_pts_2d[i, 0] < 0:
            continue
        if imgfov_pts_2d[i, 1] > h or imgfov_pts_2d[i, 1] < 0:
            continue
        cv2.circle(
            img,
            center=(int(np.round(imgfov_pts_2d[i, 0])),
                    int(np.round(imgfov_pts_2d[i, 1]))),
            radius=thickness,
            color=color,
            thickness=thickness,
        )
    return img

在这里插入图片描述

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ECharts-X是 ECharts 团队推出的全新 3D 可视化,它是基于 ECharts 的扩展,底层深度整合了 WebGL QTEK和 Canvas2D ZRender。特色混搭ECharts 里的混搭功能很强大,作为 ECharts 的扩展,ECharts-X 自然也需要支持。ECharts-X 能跟 ECharts 中的折柱饼地图等图表混搭,可以有更丰富的可视化效果,同时 ECharts-X 也能够直接使用 ECharts 中的 legend, dataRange 等组件。3D大规模标注ECharts-X 中的标注在效果和使用上都跟 ECharts 中的标注(markPoint)类似,但是由于WebGL的强大能力,对于几万甚至几十万的markPoint也能进行实时的动画和交互3D大规模标柱标柱(markBar)是 ECharts-X 中新定义的一个概念,它是标注(markPoint)的衍生,在三维空间扩展了高度维度表达更丰富的数据信息。3D大规模标线同样 ECharts-X 中的标线在使用上和 ECharts 类似,但是展现效果从 2D 变成 3D 的曲线,支持几万条 markLine 的实时展现,动画以及交互风场,洋流等向量场的可视化NASA之前发布过全球洋流图,用梵高风格的表现使得可视化也充满了艺术感,ECharts-X 也提供了对洋流,风场这种向量场可视化的便捷配置。同样的,也是实时的展现和交互。自定义底图这个功能比较简单但是非常实用,能够配置地球的底图纹理图片,使得展现更有质感,以后也会在 ECharts 的 map 中加入。下面截图是将地图换成木星纹理的效果。 标签:ECharts
### 回答1: Kitti数据集是一个用于自动驾驶以及计算机视觉的数据集,包含了许多不同类型的数据,包括激光雷达和相机的数据。如果想要将激光雷达数据可视化到图像上,可以使用Python编写代码来实现。 首先需要加载点云数据,在Python中可以使用PCL或Open3D。在加载点云数据之后,需要将其转换成图像数据,这可以通过将点云数据投影到一个平面上来实现,这个平面是车辆朝向的平面。在投影点云数据时可以根据需求选择投影的范围和分辨率。然后使用opencv投影的数据从灰度图像转换为彩色图像,并将图像保存在本地或者在屏幕上播放。 当然,对于初学者来说,上述代码并不容易理解和实现。因此,建议学习和掌握以下内容: 1. Python基础知识,包括语法、控制流和函数等。 2. PCL和Open3D的基本使用方法。 3. Opencv的基本使用方法。 4. 点云数据转换成图像数据的方法和参数。 最后,建议查阅相关的代码示例和文档,因为这有助于更好地理解代码和算法。通过不断练习,您可以将点云数据可视化到图像上,并得到更深层次的理解。 ### 回答2: Kitti数据集是一个基于激光雷达的自动驾驶数据集,里面包括了多种不同场景下的点云数据、图像数据和各种传感器数据。其中,点云数据对于自动驾驶系统的实现具有重要意义。以下是使用Python将点云数据可视化到图像上的步骤: 1. 安装必要的Python: ```python pip install numpy matplotlib open3d opencv-python ``` 2. 加载点云数据: 从Kitti数据集中选取某个场景下的点云数据,使用Python加载点云数据文件: ```python import numpy as np pcd = np.fromfile("path_to_point_cloud_file.bin", dtype=np.float32).reshape(-1, 4) ``` 3. 可视化点云数据: 使用Open3D可视化点云数据,实现点云数据在三维空间内的展示: ```python import open3d as o3d pcd = o3d.geometry.PointCloud() pcd.points = o3d.utility.Vector3dVector(points) o3d.visualization.draw_geometries([pcd]) ``` 4. 投影点云数据到图像平面: 将点云数据投影到图像平面上,通过OpenCV将点云数据可视化到图像上: ```python import cv2 from kitti_projection import KittiProjection range_image = KittiProjection().project_lidar_to_image(points, image_size=(1242, 375)) cv2.imshow("Range Image", range_image) cv2.waitKey(0) ``` 5. 结论: 通过上述步骤,我们可以将Kitti数据集中的点云数据可视化到图像上,实现自动驾驶系统中点云数据和图像数据的结合,为自动驾驶系统的实现提供支持。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值