3D Detection: 3D Box和点云 快速可视化

3D检测,用于3D box,点云快速可视化,辅助debug和分析:
(Nuscenes,mmdet3d,OpenPCDet等适用)

注意:代码适用于多种模型,但是注意BEVDet系列和FCOS3D(front View)系列要选用不同的box转换。

3D box投影到bev可视化:

import matplotlib.pyplot as plt
import torch

def box3d2x0y0wh(boxes_3d):
	# BEVDet/CenterPoints
    import numpy as np
    n = boxes_3d.shape[0]
    box2d = np.zeros((n,4))
    # 3dbox --> xywh
    box2d[:,:2] = boxes_3d[:,:2]
    box2d[:,2] = boxes_3d[:,3]
    box2d[:,3] = boxes_3d[:,4] # 2xywh
    # 
    # xyxy = np.ones_like(box2d)
    box2d[:,0] = box2d[:, 0] - box2d[:, 2] / 2 
    box2d[:,1] = box2d[:, 1] + box2d[:, 3] / 2 # NOTE: 左下角点
    
    return box2d

def box3d2x0y0wh_2(boxes_3d):
    # FCOS3D: front view--> BEV
    import numpy as np
    n = boxes_3d.shape[0]
    box2d = np.zeros((n,4))
    # 3dbox --> xywh 左下角点
    box2d[:, 0] = boxes_3d[:, 0]
    box2d[:, 1] = boxes_3d[:, 2]
    box2d[:, 2] = boxes_3d[:, 4]
    box2d[:, 3] = boxes_3d[:, 5] # 2xywh
    # 
    # xyxy = np.ones_like(box2d)
    box2d[:,0] = box2d[:, 0] - box2d[:, 2] / 2 
    box2d[:,1] = box2d[:, 1] + box2d[:, 3] / 2 # NOTE
    
    return box2d

# 根据坐标作图
def draw_boxes(pred_boxes_3d, target_boxes_3d, path):
    # pred_boxes xywh
    
    import matplotlib.pyplot as plt
    import matplotlib.patches as patches
    
    pred_boxes = box3d2x0y0wh(pred_boxes_3d)
    target_boxes = box3d2x0y0wh(target_boxes_3d)

    fig, ax = plt.subplots()
    ax.plot()
    # ax.add_patch(patches.Rectangle((1, 1),0.5,0.5,edgecolor = 'blue',facecolor = 'red',fill=True) )
    
    #
    for index, coord in enumerate(pred_boxes):
        rect = patches.Rectangle((coord[0], coord[1]), coord[2], coord[3], 
        						linewidth=1, edgecolor='r',facecolor='none')
        ax.add_patch(rect)
    for index, coord in enumerate(target_boxes):
        rect = patches.Rectangle((coord[0], coord[1]), coord[2], coord[3], 
        						linewidth=1, edgecolor='g',facecolor='none')
        ax.add_patch(rect)
    
    # plt.legend(loc='best',edgecolor='g')
	if os.path.exists(path):
        os.remove(path)
    fig.savefig(path, dpi=90, bbox_inches='tight')
    # print(0)
    plt.close(fig)
    print('Successfully saved')

点云快速可视化:

def draw_pts(points, save_path, show=False):
    '''
    points: [N,3+c]
    '''
    assert len(points.shape) == 2
    if isinstance(points, torch.Tensor):
        points = points.cpu().numpy()

    points = points.copy()

    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    # point_range = range(0, points.shape[0], skip) # skip points to prevent crash
    point_range = range(0, points.shape[0])
    ax.scatter(points[point_range, 0],   # x
            points[point_range, 1],   # y
            points[point_range, 2],   # z
            c=points[point_range, 2], # height data for color
            cmap=plt.get_cmap("Spectral"),
            marker="x")
    ax.axis('auto')  # {equal, scaled}
    if show:
        plt.show()

    if save_path is not None:
        fig.savefig(save_path, dpi=90, bbox_inches='tight')
    plt.close(fig)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

烤粽子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值