原模型检测时候只有点云的检测框,本文主要是将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_meshlabos.environ['CUDA_LAUNCH_B LOCKING'] = '1'  # 下面老是报错 shape 不一致import numpy as np
import cv2def project_velo2rgb(velo, calib):T = np.zeros([4, 4], dtype=np.float32)T[:3, :] = calib['Tr_velo2cam']T[3, 3] = 1R = np.zeros([4, 4], dtype=np.float32)R[:3, :3] = calib['R0']R[3, 3] = 1num = 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] = box2dreturn projectionsdef draw_rgb_projections(image, projections, color=(255, 255, 255), thickness=2, darker=1):img = image.copy() * darkernum = 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) % 4cv2.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 + 4cv2.line(img, (qs[i, 0], qs[i, 1]), (qs[j, 0], qs[j, 1]), color, thickness, cv2.LINE_AA)i, j = k, k + 4cv2.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 imgdef 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] = txcam[1] = tycam[2] = tzt_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)).Tbox3d_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_cornerif __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 filemodel = init_model(args.config, args.checkpoint, device=args.device)# test a single imageresult, data = inference_detector(model, args.pcd)# show the resultsshow_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)

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

点云检测框投影到图像上(mmdetection3d)相关推荐

  1. 2022TGRS/云检测:用于遥感图像云检测的无监督域不变特征学习Unsupervised Domain-Invariant Feature Learning for Cloud Detection

    2022TGRS/云检测:Unsupervised Domain-Invariant Feature Learning for Cloud Detection of Remote Sensing Im ...

  2. 把激光点投影到图像上并融合显示

    把激光点投影到图像上并融合显示 示图: 全部代码如下(python): # -*- coding: utf-8 -*- import pcl import numpy as np import cv2 ...

  3. TGRS2020/云检测:Deep Matting for Cloud Detection in Remote Sensing Images深度抠图在遥感图像云检测中的应用

    TGRS2020/云检测:Deep Matting for Cloud Detection in Remote Sensing Images深度抠图在遥感图像云检测中的应用 0.摘要 1.概述 2.云 ...

  4. 带你读AI论文丨用于目标检测的高斯检测框与ProbIoU

    摘要:本文解读了<Gaussian Bounding Boxes and Probabilistic Intersection-over-Union for Object Detection&g ...

  5. RSE2022/云检测:A hybrid generative adversarial network for weakly-supervised cloud detection 多光谱图像弱监督云检

    RSE2022/云检测:A hybrid generative adversarial network for weakly-supervised cloud detection in multisp ...

  6. python实现——在图像上放置虚拟物

    前言:虚拟物是指一些3D模型,在图像上放置虚拟物就是以一张图片为背景,在把绘制的3D图放置在指定的位置上.除此之外还可以用于视频中放置虚拟物,实现动图放置3D模型.在这个例子当中,是通过照相机标定获得 ...

  7. 目标检测:Anchor【就是在图像上预设好的不同大小,不同长宽比的参照框】

    anchor到底是什么呢?如果我们用一句话概括--就是在图像上预设好的不同大小,不同长宽比的参照框.(其实非常类似于上面的滑窗法所设置的窗口大小) 下图来自<动手学深度学习>中的例子,假设 ...

  8. 【目标检测】在图像上画bounding box框,生成带真实标签gt的图片

    [目标检测]在图像上画bounding box框,生成带真实标签gt的图片 问题/Motivation 数据格式 用到的库 实际代码` 结果展示 问题/Motivation 在制作完数据集后,想看一下 ...

  9. RS2022/云检测:考虑域偏移问题的卫星图像半监督云检测Semi-Supervised Cloud Detection in Satellite Images by Considering the

    Semi-Supervised Cloud Detection in Satellite Images by Considering the Domain Shift Problem考虑区域偏移问题的 ...

最新文章

  1. 软件测试艺术一:程序正确性证明
  2. java图像分类源码_在Android设备上使用NCNN图像分类的demo
  3. Pycharm 折叠展开代码快捷键
  4. 解决wps与matlab不兼容的问题
  5. emacs python 缩进问题_Emacs python mode的问题
  6. python实现单张图像拼接与批量图片拼接
  7. 数组的 sort() 方法详解
  8. Jquery特殊属性
  9. Java版点餐小程序2022最新版笔记,Springboot+Mysql+Freemarker+Bootstrap+微信小程序实现扫码点餐小程序
  10. 说话干嘛要“之道”啦?(持续更新中)
  11. php 只打印某个区域,PHP打印代码页面固定区域
  12. CSU - 2078 查找第k大(O(n)区间第k大 快排思想)
  13. 20162312Java结对编程之挑战出题
  14. c语言横版格斗游戏,2.5D横版格斗游戏DEMO附素材
  15. Eigrp恶意插入路由和致瘫攻击测试(一)
  16. STC单片机驱动BLDC无刷直流电机(无HALL)官方示例
  17. Linux gunzip 命令实例讲解
  18. DS1337 时钟芯片在 C8051F 上的实现
  19. 手机行业通用名词解释大全
  20. Java个人技术知识点总结(业务场景篇,java高级多线程面试

热门文章

  1. 500个Python模块(库)的详细分类介绍
  2. 线性代数(二十四) : 行列式的展开式—拉普拉斯公式
  3. linux服务器配置磁盘阵列,Linux下的RAID配置和管理
  4. R语言ggplot2可视化:使用ggpubr包的arrangeGrob函数将多个可视化结果整合为gtable对象、使用as_ggplot函数将gtable对象转化为ggplot对象
  5. 记一次mysql启动失败问题.
  6. Android Studio 4.1没有GsonFormat插件
  7. 全景拼接python旗舰版
  8. Educational Codeforces Round 121 (Rated for Div. 2) unr场 A B C
  9. Web自动化之Pytest测试框架
  10. chap0x10 实战fail2ban防止Basic认证暴力破解和SSH口令爆破