Kitti-传感器配置
KITTI数据集的数据采集平台装配
有2个灰度摄像机,2个彩色摄像机,(编号如下)
0 左边灰度相机
1 右边灰度相机
2 左边彩色相机
3 右边彩色相机
一个Velodyne64线3D激光雷达,
4个光学镜头,
以及1个GPS导航系统
KITTI标定
KITTI标定校准文件主要作用是把激光雷达坐标系测得的点云坐标转换到相机坐标中去
相关参数存在data object calib中,共包含7481个训练标定文件和7518个测试标定文件。
标定文件的存储方式为txt文本文件。
calib训练集存储为data_object_calib/training/calib/xxxxxx.txt,共7481个文件。
calib测试集存储为data_object_calib/testing/calib/xxxxxx.txt,共7518个文件
标定矩阵详情
Tr_velo_to_cam 外参矩阵为,大小为3x4,包含了旋转矩阵 R 和 平移向量 T
将相机的外参矩阵乘以点云坐标即可得到点云在世界坐标系中的坐标
P0-P3分别表示4个相机的内参矩阵,或投影矩阵, 大小为 3x4。
相机内参矩阵是为了计算点云空间位置坐标在相机坐标系下的坐标,即把点云坐标投影到相机坐标系。
将相机的内参矩阵乘以点云在世界坐标系中的坐标即可得到点云在相机坐标系中的坐标。
R0校准矩阵
R0_rect 为0号相机的修正矩阵,大小为3x3,目的是为了使4个相机成像达到共面的效果,保证4个相机光心在同一个xoy平面上。
在进行外参矩阵变化之后,需要于R0_rect相乘得到相机坐标系下的坐标
外参矩阵 Tr_velo_to_cam(3x4)需要增加一行[0, 0, 0, 1]变成4x4的矩阵
将Velodyne激光雷达坐标系中的点x投影到左侧的彩色图像中y,使用公式:
y = P2 * R0_rect *Tr_velo_to_cam * x
当计算出z<0的时候表明该点在相机的后面 。
按照上述过程得到的结果是点云在相机坐标系中的坐标,如果需要将点云坐标投影到像平面还需要除以Z
标注文件
kitti标注文件label_2中,三维目标标注的结果是包括中心坐标、尺寸和旋转角度等三个部分,其中中心坐标和旋转角度是在相机坐标系下的结果
维物体的尺寸(dimensions),分别对应高度、宽度、长度,以米为单位
kitti标签里的中心点位置在物体下表面
观测角度(alpha),取值范围为(-pi, pi)。是在相机坐标系下,以相机原点为中心,相机原点到物体中心的连线为半径,将物体绕相机y轴旋转至相机z轴,此时物体方向与相机x轴的夹角。这相当于将物体中心旋转到正前方后,计算其与车身方向的夹角。
nuScenes数据集3Dbonding box
Box是标注信息的3Dbox:一个Box信息为(center,size,orientation,label,score,velocity,name,token)
center: sample_annotation中的‘translation’,x,y,z;
size:sample_annotation中的‘size’,w、l、h
orientation:sample_annotation中的’rotation‘。
label:为可选项,int类型。
score:分类的得分,可选
velocity:center的各自变化速度。
name:box的名字,类别的名字。
token: sample_annotation的唯一token。
nuScenes数据集存在四个坐标系:全局坐标系,车身坐标系,相机坐标系,雷达(Rader,Lidar)坐标系。
因为标注信息是在全局坐标系下,所以需要进行坐标转换才能得到对应图像的box信息
图像的3Dbox转换到2Dbox https://blog.csdn.net/CSDNxiaoh/article/details/124231504
说明
def bev(self):
"""torch.Tensor: 2D BEV box of each box with rotation
in XYWHR format."""
return self.tensor[:, [0, 1, 3, 4, 6]]
XYZHWLR format
mmboxes[:, :6] = mmboxes[:, [0, 1, 2, 4, 3, 5]]
# 设bboxes为大小(M,7)的Tensor,其中M为边界框数量,7代表x,y,z坐标,x,y,z尺寸以及朝向角,若有速度等其它参数,需放置在最后
centers, dims, angles = bboxes[:, :3], bboxes[:, 3:6], bboxes[:, 6]
示例
def points_lidar2image(points, tr_velo_to_cam, P2):
'''
points: shape=(N, 8, 3)
tr_velo_to_cam: shape=(4, 4)
r0_rect: shape=(4, 4)
P2: shape=(4, 4)
return: shape=(N, 8, 2)
'''
extended_points = np.pad(points, ((0, 0), (0, 0), (0, 1)), 'constant', constant_values=1.0) # (N, 8, 4)
camera_points = extended_points @ tr_velo_to_cam.T # (N, 8, 4)
image_points = camera_points @ P2.T # (N, 8, 4)
##以行为基准, 删除投影图像点中深度z<0(在投影图像后方)的点 axis=0
image_p = np.delete(image_points,np.where(image_points[:, :, 2:3]<0),axis=0)
# 归一化到相机坐标系z=1平面 --> 像平面坐标系
image_points = image_p[:, :, :2] / image_p[:, :, 2:3]
return image_points
说明
if calib_info is not None and img is not None:
bboxes2d, camera_bboxes = result_filter['bboxes2d'], result_filter['camera_bboxes']
bboxes_corners = bbox3d2corners_camera(camera_bboxes)
image_points = points_camera2image(bboxes_corners, P2)
img = vis_img_3d(img, image_points, labels, rt=True)
坐标系。
世界坐标Pw—>相机坐标Pc—>归一化平面坐标—>物理平面坐标—>像素平面坐标p(像素坐标用小p)
1.Pc = R * Pw + t
2.p = K * Pc
(一般计算不考虑归一化平面和物理平面坐标,因为已经融合到内参了)
归一化平面位于Z=1的位置;物理平面位于Z= f 的位置 他们仅仅相差一个倍数 f
###mmdet3d
mmdet3d提供了定义好的边界框数据类型 LiDARInstance3DBoxes(位于mmdet3d/core/bbox/structure/lidar_box3d.py下)
以及CameraInstance3DBoxes(位于mmdet3d/core/bbox/structure/cam_box3d.py下),
分别是激光雷达坐标系下的3D边界框以及相机坐标系下的3D边界框数据类型,许多方法已经内置于数据结构中,可直接调用。
以LiDARInstance3DBoxes类为例,首先需要创建该类的实例:
bboxes = LiDARInstance3DBoxes(bboxes)
# 另有三项输入box_dim(每个边界框的参数数量),with_yaw(包含朝向角为True)和origin(含义见前文介绍,指示3D坐标在边界框中的相对位置)
# 可使用默认值box_dim=7, with_yaw=True, origin=(0.5, 0.5, 0)
NuScenes 3dbox点云数据可视化
import os
import open3d as o3d
import numpy as np
from pyquaternion import Quaternion
from nuscenes.nuscenes import NuScenes
# q, t->T
def get_T(lidar_calibrator_data, inverse = False):
T = np.eye(4, 4)
T[:3, :3] = Quaternion(lidar_calibrator_data['rotation']).rotation_matrix
T[:3, 3] = lidar_calibrator_data['translation']
if inverse:
T = np.linalg.inv(T)
return T
def convert_object_corners(obj):
orientation=Quaternion(obj["rotation"])
center = [obj["translation"][0], obj["translation"][1], obj["translation"][2]]
size = [obj["size"][0], obj["size"][1], obj["size"][2]]
w, l, h = size
x_corners = l / 2 * np.array([1, 1, 1, 1, -1, -1, -1, -1])
y_corners = w / 2 * np.array([1, -1, -1, 1, 1, -1, -1, 1])
z_corners = h / 2 * np.array([1, 1, -1, -1, 1, 1, -1, -1])
corners = np.vstack((x_corners, y_corners, z_corners))
# Rotate
corners = np.dot(orientation.rotation_matrix, corners)
# Translate
x, y, z = center
corners[0, :] = corners[0, :] + x
corners[1, :] = corners[1, :] + y
corners[2, :] = corners[2, :] + z
#print("center ",center,corners)
return np.transpose(corners)
def create_box_from_corners(corners):
lines = [[0, 1], [1, 2], [2, 3], [3, 0],
[4, 5], [5, 6], [6, 7], [7, 4],
[0, 4], [1, 5], [2, 6], [3, 7]]
#colors = [color for i in range(len(lines))]
line_set = o3d.geometry.LineSet()
line_set.points = o3d.utility.Vector3dVector(corners)
line_set.lines = o3d.utility.Vector2iVector(lines)
#line_set.colors = o3d.utility.Vector3dVector(colors)
return line_set
if __name__ =="__main__":
dataroot = r"/data/v1.0-mini"
nusc = NuScenes(version='v1.0-mini', dataroot='/data/v1.0-mini', verbose=True)
my_scene = nusc.scene[0]
samples = nusc.sample
for sample in nusc.sample[0:1]:
# 1 雷达处理
# 1.1 根据token取出lidar的信息
lidar_token = sample['data']["LIDAR_TOP"]
lidar_sample_data = nusc.get('sample_data', lidar_token)
# 根据token取出雷达的文件名字
lidar_file_name = lidar_sample_data['filename']
lidar_path = os.path.join(dataroot, lidar_file_name)
print(lidar_file_name)
## 1.2 获取点云数据
points = np.fromfile(lidar_path, dtype=np.float32).reshape([-1, 5])
## 1.3 齐次坐标
'''
点云转齐次坐标
cloud原本是 n * 5(x,y,z,i,r) 取前3维度这里要去xyz并转成齐次坐标系
np.ones(len(cloud), 1) 创建n行一列的1向量
'''
points_hom = np.concatenate([points[:, :3], np.ones((len(points), 1))], axis=1)
# # 1.4 获取T_ego_lidar, T_global_ego, T_global_lidar
# 获取lidar标定的token
lidar_calibrator_data = nusc.get("calibrated_sensor", lidar_sample_data['calibrated_sensor_token'])
# 雷达在ego系下pose Tego_lidar lidar2ego
T_ego_lidar = get_T(lidar_calibrator_data)
ego_pose_data = nusc.get("ego_pose", lidar_sample_data['ego_pose_token'])
# ego在global系的坐标T_global_ego ego2global
T_global_ego = get_T(ego_pose_data)
# T_global_lidar lidar2global
T_global_lidar = T_global_ego @ T_ego_lidar
points_global = points_hom @ T_global_lidar.T
# 3 anno 标注信息解析 global系下
### 每个样本都有n个标注, 每个sample的['anns']存储的n个token, 根据token就能确认标志信息
print("anno_num:", len(sample['anns']))
boxes_o3d = []
vis = o3d.visualization.Visualizer()
vis.create_window(window_name='open3d')
pc = o3d.geometry.PointCloud()
pc.points = o3d.utility.Vector3dVector(points_global[:,:3])
# pc.colors = o3d.utility.Vector3dVector(points_color)
vis.add_geometry(pc)
for token in sample['anns']:
annotation = nusc.get("sample_annotation", token)
T_ego_anno = get_T(annotation)
anno_size = annotation['size']
category_name = annotation['category_name'].split('.')[1]
box_conners = convert_object_corners(annotation)
box_o3d = create_box_from_corners(box_conners)
boxes_o3d.append(box_o3d)
[vis.add_geometry(element) for element in boxes_o3d]
print("boxes_o3d",len(boxes_o3d))
vis.get_render_option().point_size = 2
vis.run()
vis.destroy_window()
参考
3D点云 (Lidar)检测入门篇 - PointPillars PyTorch实现 https://zhuanlan.zhihu.com/p/521277176
https://github.com/zhulf0804/PointPillars/blob/b9948e73505c8d6bfa631ffdf76c7148e82c5942/utils/process.py
https://github.com/zhulf0804/PointPillars/blob/b9948e73505c8d6bfa631ffdf76c7148e82c5942/test.py
第五讲中相机坐标系,像素平面坐标系,世界坐标系,归一化坐标系总结 https://blog.csdn.net/CxC2333/article/details/108336064
https://github.com/zhulf0804/PointPillars/blob/main/test.py
https://zhuanlan.zhihu.com/p/521277176
https://github.com/zhulf0804/PointPillars/blob/main/utils/process.py
【KITTI】KITTI数据集简介(二) — 标注数据label_2
3D框corner计算 https://zhuanlan.zhihu.com/p/151777998