3d bounding box投影到2d
写在前面
-
本文内容
使用内参和相机pose将3d bounding box投影到2d
-
平台/环境
python, open3d, opencv -
转载请注明出处:
https目录
- 写在前面
- 代码
- 完
代码
def project_box_to_2d2(bbox_3d, camera_pose, camera_intrinsics, width, height, image = None):"""投影3D边界框到2D图像平面Args:bbox_3d (o3d.geometry.AxisAlignedBoundingBox 或 o3d.geometry.OrientedBoundingBox): 3D边界框camera_pose (np.ndarray): 相机位姿矩阵 (4x4)camera_intrinsics (np.ndarray): 相机内参矩阵 (3x3)width (int): 图像宽度height (int): 图像高度image (np.ndarray, optional): 要绘制边界框的图像. Defaults to None."""# 获取边界框的最小和最大点min_bound = bbox_3d.get_min_bound()max_bound = bbox_3d.get_max_bound()# print("Min Bound:", min_bound)# print("Max Bound:", max_bound)# 3D空间中边界框的8个顶点bbox_points = np.array([[min_bound[0], min_bound[1], min_bound[2]],[max_bound[0], min_bound[1], min_bound[2]],[max_bound[0], max_bound[1], min_bound[2]],[min_bound[0], max_bound[1], min_bound[2]],[min_bound[0], min_bound[1], max_bound[2]],[max_bound[0], min_bound[1], max_bound[2]],[max_bound[0], max_bound[1], max_bound[2]],[min_bound[0], max_bound[1], max_bound[2]],])# 将3D点转换为齐次坐标bbox_points_homogeneous = np.hstack((bbox_points, np.ones((bbox_points.shape[0], 1))))bbox_points_camera = np.dot(np.linalg.inv(camera_pose), bbox_points_homogeneous.T).T# 通过相机内参矩阵将3D点投影到图像平面bbox_points_2d = np.dot(camera_intrinsics, bbox_points_camera[:, :3].T).Tbbox_points_2d = bbox_points_2d[:, :2] / bbox_points_2d[:, 2:]# 2D边界框的顶点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), # 连接顶部和底部]# 选择x和y的最小值和最大值作为2D边界框,并将边界限制在图像内x_min = int(np.clip(np.min(bbox_points_2d[:, 0]), 0, width - 1))x_max = int(np.clip(np.max(bbox_points_2d[:, 0]), 0, width - 1))y_min = int(np.clip(np.min(bbox_points_2d[:, 1]), 0, height - 1))y_max = int(np.clip(np.max(bbox_points_2d[:, 1]), 0, height - 1))bbox_2d = np.array([x_min, y_min, x_max, y_max])image_show = copy.deepcopy(image)if image_show is not None:# 在图像上绘制投影的3D边界框for line in lines:pt1 = tuple(bbox_points_2d[line[0]].astype(int))pt2 = tuple(bbox_points_2d[line[1]].astype(int))cv2.line(image_show, pt1, pt2, (0, 255, 0), 2)# 绘制2D边界框print("2D Bounding Box:", bbox_2d)cv2.rectangle(image_show, (x_min, y_min), (x_max, y_max), (0, 0, 255), 2)# 可视化cv2.namedWindow("Projected 3D Bounding Box", cv2.WINDOW_NORMAL)cv2.imshow("Projected 3D Bounding Box", image_show)cv2.waitKey(0)cv2.destroyAllWindows()return bbox_2d, image_show
完
主要做激光/影像三维重建,配准、分割等常用点云算法,熟悉open3d、pcl等开源点云库,技术交流、咨询可私信