点云检测框投影到图像上(mmdetection3d)

这篇具有很好参考价值的文章主要介绍了点云检测框投影到图像上(mmdetection3d)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

原模型检测时候只有点云的检测框,本文主要是将demo文件中的pcd_demo.py中的代码,将点云检测出的3d框投影到图像上面显示。 

# Copyright (c) OpenMMLab. All rights reserved.
from argparse import ArgumentParser
# import sys
# sys.path
# sys.path.append('D:\Aware_model\mmdetection3d\mmdet3d')
import os
import sys
dir_mytest = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, dir_mytest)

from mmdet3d.apis import inference_detector, init_model, show_result_meshlab

os.environ['CUDA_LAUNCH_B LOCKING'] = '1'  # 下面老是报错 shape 不一致

import numpy as np
import cv2


def project_velo2rgb(velo, calib):
    T = np.zeros([4, 4], dtype=np.float32)
    T[:3, :] = calib['Tr_velo2cam']
    T[3, 3] = 1
    R = np.zeros([4, 4], dtype=np.float32)
    R[:3, :3] = calib['R0']
    R[3, 3] = 1
    num = len(velo)
    projections = np.zeros((num, 8, 2), dtype=np.int32)
    for i in range(len(velo)):
        box3d = np.ones([8, 4], dtype=np.float32)
        box3d[:, :3] = velo[i]
        M = np.dot(calib['P2'], R)
        M = np.dot(M, T)
        box2d = np.dot(M, box3d.T)
        box2d = box2d[:2, :].T / box2d[2, :].reshape(8, 1)
        projections[i] = box2d
    return projections

def draw_rgb_projections(image, projections, color=(255, 255, 255), thickness=2, darker=1):

    img = image.copy() * darker
    num = len(projections)
    forward_color = (255, 255, 0)
    for n in range(num):
        qs = projections[n]
        for k in range(0, 4):
            i, j = k, (k + 1) % 4
            cv2.line(img, (qs[i, 0], qs[i, 1]), (qs[j, 0], qs[j, 1]), color, thickness, cv2.LINE_AA)
            i, j = k + 4, (k + 1) % 4 + 4
            cv2.line(img, (qs[i, 0], qs[i, 1]), (qs[j, 0], qs[j, 1]), color, thickness, cv2.LINE_AA)
            i, j = k, k + 4
            cv2.line(img, (qs[i, 0], qs[i, 1]), (qs[j, 0], qs[j, 1]), color, thickness, cv2.LINE_AA)
        cv2.line(img, (qs[3, 0], qs[3, 1]), (qs[7, 0], qs[7, 1]), forward_color, thickness, cv2.LINE_AA)
        cv2.line(img, (qs[7, 0], qs[7, 1]), (qs[6, 0], qs[6, 1]), forward_color, thickness, cv2.LINE_AA)
        cv2.line(img, (qs[6, 0], qs[6, 1]), (qs[2, 0], qs[2, 1]), forward_color, thickness, cv2.LINE_AA)
        cv2.line(img, (qs[2, 0], qs[2, 1]), (qs[3, 0], qs[3, 1]), forward_color, thickness, cv2.LINE_AA)
        cv2.line(img, (qs[3, 0], qs[3, 1]), (qs[6, 0], qs[6, 1]), forward_color, thickness, cv2.LINE_AA)
        cv2.line(img, (qs[2, 0], qs[2, 1]), (qs[7, 0], qs[7, 1]), forward_color, thickness, cv2.LINE_AA)
    return img


def load_kitti_calib(calib_file):
    """
    load projection matrix
    """
    with open(calib_file) as fi:
        lines = fi.readlines()
        assert (len(lines) == 8)
    obj = lines[0].strip().split(' ')[1:]
    P0 = np.array(obj, dtype=np.float32)
    obj = lines[1].strip().split(' ')[1:]
    P1 = np.array(obj, dtype=np.float32)
    obj = lines[2].strip().split(' ')[1:]
    P2 = np.array(obj, dtype=np.float32)
    obj = lines[3].strip().split(' ')[1:]
    P3 = np.array(obj, dtype=np.float32)
    obj = lines[4].strip().split(' ')[1:]
    R0 = np.array(obj, dtype=np.float32)
    obj = lines[5].strip().split(' ')[1:]
    Tr_velo_to_cam = np.array(obj, dtype=np.float32)
    obj = lines[6].strip().split(' ')[1:]
    Tr_imu_to_velo = np.array(obj, dtype=np.float32)
    return {'P2': P2.reshape(3, 4),
            'R0': R0.reshape(3, 3),
            'Tr_velo2cam': Tr_velo_to_cam.reshape(3, 4)}

def box3d_cam_to_velo(box3d,tr):

    tx, ty, tz, l, w, h, ry = [float(i) for i in box3d]     #都是激光坐标系下的参数
    cam = np.ones([3, 1])
    cam[0] = tx
    cam[1] = ty
    cam[2] = tz
    t_lidar = cam.reshape(1,3)#摄像头下转点云坐标系
    Box = np.array([[-l / 2, -l / 2, l / 2, l / 2, -l / 2, -l / 2, l / 2, l / 2],
                    [w / 2, -w / 2, -w / 2, w / 2, w / 2, -w / 2, -w / 2, w / 2],
                    [0, 0, 0, 0, h, h, h, h]])

    rotMat = np.array([
        [np.cos(ry), -np.sin(ry), 0.0],
        [np.sin(ry), np.cos(ry), 0.0],
        [0.0, 0.0, 1.0]])
    velo_box = np.dot(rotMat, Box)
    print(np.tile(t_lidar, (8, 1)).T)
    print(velo_box)
    cornerPosInVelo = velo_box + np.tile(t_lidar, (8, 1)).T
    box3d_corner = cornerPosInVelo.transpose()
    return box3d_corner.astype(np.float32)

def load_kitti_label(box, Tr):

    gt_boxes3d_corner=[]
    for j in range(len(box)):
        box3d_corner = box3d_cam_to_velo(box[j], Tr)
        gt_boxes3d_corner.append(box3d_corner)
    gt_boxes3d_corner = np.array(gt_boxes3d_corner).reshape(-1, 8, 3)
    return gt_boxes3d_corner


if __name__ == '__main__':
    parser = ArgumentParser()
    parser.add_argument('--pcd', default=r'D:\Aware_model\mmdetection3d\data\kitti\training\velodyne\000016.bin',
                        help='Point cloud file')
    parser.add_argument('--config',
                        default=r'D:\Aware_model\mmdetection3d\work_dirs\hv_pointpillars_secfpn_6x8_160e_kitti-3d-car\hv_pointpillars_secfpn_6x8_160e_kitti-3d-car.py',
                        help='Config file')
    parser.add_argument('--checkpoint',
                        default=r'D:\Aware_model\mmdetection3d\checkpoints\hv_pointpillars_secfpn_6x8_160e_kitti-3d-car_20220331_134606-d42d15ed.pth',
                        help='Checkpoint file')
    parser.add_argument('--img', default=r'D:\Aware_model\mmdetection3d\data\kitti\training\image_2\000016.png')
    parser.add_argument('--calib', default=r'D:\Aware_model\mmdetection3d\data\kitti\training\calib\000016.txt')
    parser.add_argument(
        '--device', default='cuda:0', help='Device used for inference')
    parser.add_argument(
        '--score-thr', type=float, default=0.0, help='bbox score threshold')
    parser.add_argument(
        '--out-dir', type=str, default='demo', help='dir to save results')
    parser.add_argument(
        '--show',
        action='store_true',
        help='show online visualization results')
    parser.add_argument(
        '--snapshot',
        action='store_true',
        help='whether to save online visualization results')
    args = parser.parse_args()
    # build the model from a config file and a checkpoint file
    model = init_model(args.config, args.checkpoint, device=args.device)
    # test a single image
    result, data = inference_detector(model, args.pcd)
    # show the results
    show_result_meshlab(
        data,
        result,
        args.out_dir,
        args.score_thr,
        # show=args.show,
        show=True,
        snapshot=args.snapshot,
        task='det')


    lidar = data['points'][0][0].cpu().numpy()
    image = cv2.imread(args.img)
    calib = load_kitti_calib(args.calib)
    gt_box3d = result[0]['boxes_3d'].tensor.numpy()

    # 在激光坐标系下预测框的八个顶点
    gt_box3d = load_kitti_label(gt_box3d, calib['Tr_velo2cam'])

    # view in point cloud,可视化
    gt_3dTo2D = project_velo2rgb(gt_box3d, calib)
    img_with_box = draw_rgb_projections(image, gt_3dTo2D, color=(0, 0, 255), thickness=1)
    cv2.imwrite('img_with_box.png', img_with_box)
    cv2.imshow('img_with_box.png', img_with_box)
    cv2.waitKey(0)

 点云投影到图像,计算机视觉,opencv,python文章来源地址https://www.toymoban.com/news/detail-639768.html

原作者:https://blog.csdn.net/suiyingy

到了这里,关于点云检测框投影到图像上(mmdetection3d)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 基于mmdetection3d的单目3D目标检测模型,效果远超CenterNet3D

    使用 mmDetection3D 进行 单目3D 目标检测:基于 KITTI 数据集的实践 在计算机视觉领域,3D 目标检测一直是一个备受关注的研究方向。随着深度学习的发展,越来越多的工具和框架涌现出来,为研究者和开发者提供了更多的选择。本文将介绍如何使用 mmDetection3D 这一强大的框架进

    2024年04月23日
    浏览(25)
  • MMDetection3D简单学习

    我们通常把模型的各个组成成分分成 6 种类型: 编码器(encoder):包括 voxel encoder 和 middle encoder 等进入 backbone 前所使用的基于体素的方法,如  HardVFE  和  PointPillarsScatter 。 骨干网络(backbone):通常采用 FCN 网络来提取特征图,如  ResNet  和  SECOND 。 颈部网络(neck):

    2024年02月13日
    浏览(35)
  • MMDetection3D框架环境配置

    MMDetection3D是一个基于PyTorch的开源3D目标检测框架。下面是MMDetection3D的环境配置步骤: 安装Anaconda,教程很多不在说明。 1.创建Python环境 使用以下命令创建一个Python 3.8环境: 使用以下命令激活Python环境:  2.安装gpu版本的torch、torchvision 2.1 下载对应的torch、torchvision安装包:

    2024年02月09日
    浏览(36)
  • mmdetection3d nuScenes (持续更新)

    本文为博主原创文章,未经博主允许不得转载。 本文为专栏《python三维点云从基础到深度学习》系列文章,地址为“https://blog.csdn.net/suiyingy/article/details/124017716”。         Mmdetection3d集成了大量3D深度学习算法,其中很大一部分可以在智能驾驶nuScenes数据集上运行。在算法

    2023年04月15日
    浏览(32)
  • 【MMDetection3D】MVXNet踩坑笔记

    原文 代码 MVXNet(CVPR2019) 最近许多关于3D target detection的工作都集中在设计能够使用点云数据的神经网络架构上。虽然这些方法表现出令人鼓舞的性能,但它们通常基于单一模态,无法利用其他模态(如摄像头和激光雷达)的信息。尽管一些方法融合了来自不同模式的数据,这些方

    2024年01月18日
    浏览(35)
  • win10 mmdetection3d环境搭建

    官网:mmdetection3d/README_zh-CN.md at master · open-mmlab/mmdetection3d · GitHub 安装过程参照:win10 mmdetection3d 训练KITTI_树和猫的博客-CSDN博客_pointnet训练kitti 官网安装过程 3D目标检测框架综述-知乎中描述了当前3D目标检测的数据和模型状况,为了能将数据和评价标准等统一,介绍了4个比

    2023年04月18日
    浏览(28)
  • MMdetection3D学习系列(一)——环境配置安装

    MMdetion3D是是mmlab在3d目标检测方面提供的相关检测模型,可以实现点云、图像或者多模态数据上的3D目标检测以及点云语义分割。 GitHub地址:https://github.com/open-mmlab/mmdetection3d/ 目前mmdetection3d 支持21种不同的算法,100多个预训练模型,7个数据集: mmdetection3D安装比较简单,之前

    2024年02月01日
    浏览(35)
  • mmdetection3d系列--(1)安装步骤(无坑版)

      最近在看一些基于点云3d目标检测的文章,需要复现甚至修改一些算法,就找到了mmlab开源的mmdetection3d目标检测框架,方便后续学习。     在安装的时候遇到一点坑,比如环境问题,安装完能跑demo但是不能跑训练测试问题等。在解决问题后还是完成了安装。在这里记录一

    2024年02月02日
    浏览(30)
  • MMDetection3D库中的一些模块介绍

    本文目前仅包含2个体素编码器、2个中间编码器、1个主干网络、1个颈部网络和1个检测头。如果有机会,会继续补充更多模型。 若发现内容有误,欢迎指出。   MMDetection3D的点云数据一般会经历如下步骤/模块:   下面分别介绍每个部分的一些典型模型。   在介绍体素

    2023年04月17日
    浏览(34)
  • mmdetection3d可视化多模态模型推理结果

    参考文献: 带你玩转 3D 检测和分割 (三):有趣的可视化 - 知乎 (zhihu.com) Welcome to MMDetection3D’s documentation! — MMDetection3D 1.0.0rc4 文档 让我们看一下ChatGPT的回答[手动狗头]: mmdetection3D是基于PyTorch框架的3D目标检测工具包,它是mmdetection的3D扩展版本。它提供了一个灵活且高效的

    2024年02月16日
    浏览(32)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包