119 lines
3.3 KiB
Python
119 lines
3.3 KiB
Python
import os
|
||
import cv2
|
||
|
||
|
||
camera_names = ["front", "back", "left", "right"]
|
||
|
||
# --------------------------------------------------------------------
|
||
# (shift_width, shift_height): 鸟瞰图在水平和垂直方向上超出标定图案的距离
|
||
shift_w = 100
|
||
shift_h = 100
|
||
|
||
|
||
# 标定图案与车辆之间在水平和垂直方向上的间隙大小
|
||
inn_shift_w = 8
|
||
inn_shift_h = 30
|
||
|
||
# 拼接图像的总宽度/高度
|
||
total_w = 260 + 2 * shift_w
|
||
total_h = 350 + 2 * shift_h
|
||
|
||
|
||
# 计算车辆在全景图中的位置
|
||
xl = shift_w + 55 + inn_shift_w
|
||
xr = total_w - xl
|
||
print(xl, xr)
|
||
yt = shift_h + 55 + inn_shift_h
|
||
yb = total_h - yt
|
||
# --------------------------------------------------------------------
|
||
|
||
project_shapes = {
|
||
"front": (total_w, yt),
|
||
"back": (total_w, yt),
|
||
"left": (total_h, xl),
|
||
"right": (total_h, xl)
|
||
}
|
||
|
||
# 要选取的四个像素点的位置。
|
||
# 运行 get_projection_map.py 脚本时,必须按相同顺序点击这些像素点。
|
||
project_keypoints = {
|
||
"front": [(shift_w + 0, shift_h),
|
||
(shift_w + 260, shift_h),
|
||
(shift_w + 0, shift_h + 100),
|
||
(shift_w + 260, shift_h + 100)],
|
||
|
||
"back": [(shift_w + 0, shift_h),
|
||
(shift_w + 260, shift_h),
|
||
(shift_w + 0, shift_h + 80),
|
||
(shift_w + 260, shift_h + 80)],
|
||
|
||
"left": [(shift_h + 60, shift_w),
|
||
(shift_h + 300, shift_w),
|
||
(shift_h + 60, shift_w + 50),
|
||
(shift_h + 300, shift_w + 50)],
|
||
|
||
"right": [(shift_h + 80, shift_w),
|
||
(shift_h + 350, shift_w),
|
||
(shift_h + 80, shift_w + 50),
|
||
(shift_h + 350, shift_w + 50)]
|
||
}
|
||
|
||
# car_image = cv2.imread(os.path.join(os.getcwd(), "images", "car.png"))
|
||
# car_image = cv2.resize(car_image, (xr - xl, yb - yt))
|
||
|
||
# 放大系数(调整这个值控制车辆图大小,1.0 = 原始大小,1.3 = 放大30%)
|
||
CAR_SCALE = 1.3
|
||
|
||
car_w = xr - xl
|
||
car_h = yb - yt
|
||
|
||
# 放大后的车辆图尺寸
|
||
car_display_w = int(car_w * CAR_SCALE)
|
||
car_display_h = int(car_h * CAR_SCALE)
|
||
|
||
# 放大后车辆图在全景图中的起始坐标(居中对齐)
|
||
car_x = xl - (car_display_w - car_w) // 2
|
||
car_y = yt - (car_display_h - car_h) // 2
|
||
|
||
def _make_car_image_rgba(path, w, h, tolerance=30):
|
||
# 用 IMREAD_UNCHANGED 保留 PNG 透明通道
|
||
img = cv2.imread(path, cv2.IMREAD_UNCHANGED)
|
||
|
||
if img is None:
|
||
return np.zeros((h, w, 4), np.uint8)
|
||
|
||
img = cv2.resize(img, (w, h))
|
||
|
||
# 如果图片有 alpha 通道,直接用
|
||
if img.shape[2] == 4:
|
||
rgba = img.copy()
|
||
else:
|
||
# 没有 alpha 通道,白色转透明
|
||
rgba = cv2.cvtColor(img, cv2.COLOR_BGR2BGRA)
|
||
white_mask = (
|
||
(rgba[:, :, 0] >= 255 - tolerance) &
|
||
(rgba[:, :, 1] >= 255 - tolerance) &
|
||
(rgba[:, :, 2] >= 255 - tolerance)
|
||
)
|
||
rgba[white_mask, 3] = 0
|
||
|
||
# 边缘平滑
|
||
alpha = rgba[:, :, 3].astype(np.uint8)
|
||
alpha = cv2.GaussianBlur(alpha, (5, 5), 0)
|
||
rgba[:, :, 3] = alpha
|
||
|
||
return rgba
|
||
|
||
import numpy as np
|
||
|
||
car_image_path = os.path.join(os.getcwd(), "images", "car.png")
|
||
|
||
# 原始尺寸(保持兼容)
|
||
car_image = cv2.imread(car_image_path)
|
||
if car_image is not None:
|
||
car_image = cv2.resize(car_image, (car_w, car_h))
|
||
|
||
# 放大版带透明通道
|
||
car_image_rgba = _make_car_image_rgba(car_image_path, car_display_w, car_display_h)
|
||
|