|
- """
- # -*- coding: utf-8 -*-
- -----------------------------------------------------------------------------------
- """
-
- import math
- import os
- import sys
-
- import cv2
- import numpy as np
- src_dir = os.path.dirname(os.path.realpath(__file__))
- # while not src_dir.endswith("sfa"):
- # src_dir = os.path.dirname(src_dir)
- if src_dir not in sys.path:
- sys.path.append(src_dir)
-
- import config.kitti_config as cnf
-
-
- def makeBEVMap(PointCloud_, boundary):
- Height = cnf.BEV_HEIGHT + 1
- Width = cnf.BEV_WIDTH + 1
-
- # Discretize Feature Map
- PointCloud = np.copy(PointCloud_)
- # PointCloud[:, 0] = np.int_(np.floor(PointCloud[:, 0] / cnf.DISCRETIZATION))
- # PointCloud[:, 1] = np.int_(np.floor(PointCloud[:, 1] / cnf.DISCRETIZATION) + Width / 2)
-
- # 针对Apollo数据集,检测360°
- PointCloud[:, 0] = np.int_(np.floor(PointCloud[:, 0] / cnf.DISCRETIZATION_Y) + Height / 2)
- PointCloud[:, 1] = np.int_(np.floor(PointCloud[:, 1] / cnf.DISCRETIZATION_X) + Width / 2)
-
- # sort-3times
- indices = np.lexsort((-PointCloud[:, 2], PointCloud[:, 1], PointCloud[:, 0]))
- PointCloud = PointCloud[indices]
-
- # Height Map
- heightMap = np.zeros((Height, Width))
-
- _, indices = np.unique(PointCloud[:, 0:2], axis=0, return_index=True)
- PointCloud_frac = PointCloud[indices]
-
- # some important problem is image coordinate is (y,x), not (x,y)
- max_height = float(np.abs(boundary['maxZ'] - boundary['minZ']))
-
- heightMap[np.int_(PointCloud_frac[:, 0]), np.int_(PointCloud_frac[:, 1])] = PointCloud_frac[:, 2] / max_height #(1217,609)
- # Intensity Map & DensityMap
- intensityMap = np.zeros((Height, Width))
- densityMap = np.zeros((Height, Width))
-
- _, indices, counts = np.unique(PointCloud[:, 0:2], axis=0, return_index=True, return_counts=True)
- PointCloud_top = PointCloud[indices]
-
- normalizedCounts = np.minimum(1.0, np.log(counts + 1) / np.log(64))
-
- intensityMap[np.int_(PointCloud_top[:, 0]), np.int_(PointCloud_top[:, 1])] = PointCloud_top[:, 3] / 255.0 # hesai40p的反射强度0~255
- densityMap[np.int_(PointCloud_top[:, 0]), np.int_(PointCloud_top[:, 1])] = normalizedCounts
- RGB_Map = np.zeros((3, Height - 1, Width - 1))
- RGB_Map[2, :, :] = densityMap[:cnf.BEV_HEIGHT, :cnf.BEV_WIDTH] # r_map
- RGB_Map[1, :, :] = heightMap[:cnf.BEV_HEIGHT, :cnf.BEV_WIDTH] # g_map
- RGB_Map[0, :, :] = intensityMap[:cnf.BEV_HEIGHT, :cnf.BEV_WIDTH] # b_map
-
- return RGB_Map
-
-
- # bev image coordinates format
- def get_corners(x, y, w, l, yaw):
- bev_corners = np.zeros((4, 2), dtype=np.float32)
- cos_yaw = np.cos(yaw)
- sin_yaw = np.sin(yaw)
- # front left
- bev_corners[0, 0] = x - w / 2 * cos_yaw - l / 2 * sin_yaw
- bev_corners[0, 1] = y - w / 2 * sin_yaw + l / 2 * cos_yaw
-
- # rear left
- bev_corners[1, 0] = x - w / 2 * cos_yaw + l / 2 * sin_yaw
- bev_corners[1, 1] = y - w / 2 * sin_yaw - l / 2 * cos_yaw
-
- # rear right
- bev_corners[2, 0] = x + w / 2 * cos_yaw + l / 2 * sin_yaw
- bev_corners[2, 1] = y + w / 2 * sin_yaw - l / 2 * cos_yaw
-
- # front right
- bev_corners[3, 0] = x + w / 2 * cos_yaw - l / 2 * sin_yaw
- bev_corners[3, 1] = y + w / 2 * sin_yaw + l / 2 * cos_yaw
-
- return bev_corners
-
-
- def drawRotatedBox(img, x, y, w, l, yaw, color):
- img_cp = img.copy()
- bev_corners = get_corners(x, y, w, l, yaw)
- corners_int = bev_corners.reshape(-1, 1, 2).astype(int)
- cv2.polylines(img, [corners_int], True, color, 2)
- corners_int = bev_corners.reshape(-1, 2)
- cv2.line(img, (int(corners_int[0, 0]), int(corners_int[0, 1])), (int(corners_int[3, 0]), int(corners_int[3, 1])), (255, 255, 0), 2)
- # return img_cp
|