1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
| import numpy as np import cv2 from typing import Tuple
class SyntheticCabinDataGenerator: """ 合成座舱数据生成器 论文: 纯合成数据训练,无需真实座舱图像 流程: 1. 3D座舱场景建模 (Blender/Omniverse) 2. 鱼眼相机模型渲染 3. 多视角图像对生成 4. 位姿标注自动生成 """ def __init__(self, cabin_model_path: str, camera_params: dict = None): """ Args: cabin_model_path: 3D座舱模型路径 camera_params: 鱼眼相机参数 """ self.camera_params = camera_params or { 'focal_length': 280, 'principal_point': (320, 240), 'distortion': 'fisheye', 'fov': 180, 'image_size': (640, 480) } self.cabin_model = self._load_cabin_model(cabin_model_path) def generate_pair(self, pose1: np.ndarray, pose2: np.ndarray) -> Tuple[np.ndarray, np.ndarray, dict]: """ 生成一对图像及标注 Args: pose1: 相机1位姿 [R|t] (4x4) pose2: 相机2位姿 [R|t] (4x4) Returns: img1, img2, annotation """ img1 = self._render_view(pose1) img2 = self._render_view(pose2) R1, t1 = pose1[:3, :3], pose1[:3, 3] R2, t2 = pose2[:3, :3], pose2[:3, 3] delta_R = R2 @ R1.T delta_t = t2 - delta_R @ t1 delta_q = self._rotation_to_quaternion(delta_R) annotation = { 'rotation': delta_q, 'translation': delta_t, 'pose1': pose1.tolist(), 'pose2': pose2.tolist() } return img1, img2, annotation def _render_view(self, pose: np.ndarray) -> np.ndarray: """渲染鱼眼视角""" img = np.random.randint(0, 255, (480, 640, 3), dtype=np.uint8) return img def _rotation_to_quaternion(self, R: np.ndarray) -> np.ndarray: """旋转矩阵转四元数""" trace = R[0, 0] + R[1, 1] + R[2, 2] if trace > 0: S = 2 * np.sqrt(trace + 1) w = 0.25 * S x = (R[2, 1] - R[1, 2]) / S y = (R[0, 2] - R[2, 0]) / S z = (R[1, 0] - R[0, 1]) / S else: if R[0, 0] > R[1, 1] and R[0, 0] > R[2, 2]: S = 2 * np.sqrt(1 + R[0, 0] - R[1, 1] - R[2, 2]) w = (R[2, 1] - R[1, 2]) / S x = 0.25 * S y = (R[0, 1] + R[1, 0]) / S z = (R[0, 2] + R[2, 0]) / S elif R[1, 1] > R[2, 2]: S = 2 * np.sqrt(1 + R[1, 1] - R[0, 0] - R[2, 2]) w = (R[0, 2] - R[2, 0]) / S x = (R[0, 1] + R[1, 0]) / S y = 0.25 * S z = (R[1, 2] + R[2, 1]) / S else: S = 2 * np.sqrt(1 + R[2, 2] - R[0, 0] - R[1, 1]) w = (R[1, 0] - R[0, 1]) / S x = (R[0, 2] + R[2, 0]) / S y = (R[1, 2] + R[2, 1]) / S z = 0.25 * S return np.array([w, x, y, z]) def generate_dataset(self, num_pairs: int = 10000) -> list: """生成完整数据集""" dataset = [] for i in range(num_pairs): pose1 = self._random_pose() pose2 = self._random_pose() img1, img2, ann = self.generate_pair(pose1, pose2) dataset.append({ 'image1': img1, 'image2': img2, 'annotation': ann }) return dataset def _random_pose(self) -> np.ndarray: """生成随机相机位姿""" R = self._random_rotation() t = np.random.uniform([-0.5, -0.3, 0.2], [0.5, 0.3, 0.8]) pose = np.eye(4) pose[:3, :3] = R pose[:3, 3] = t return pose def _random_rotation(self) -> np.ndarray: """生成随机旋转矩阵""" rx = np.random.uniform(-0.5, 0.5) ry = np.random.uniform(-0.3, 0.3) rz = np.random.uniform(-0.5, 0.5) Rx = np.array([ [1, 0, 0], [0, np.cos(rx), -np.sin(rx)], [0, np.sin(rx), np.cos(rx)] ]) Ry = np.array([ [np.cos(ry), 0, np.sin(ry)], [0, 1, 0], [-np.sin(ry), 0, np.cos(ry)] ]) Rz = np.array([ [np.cos(rz), -np.sin(rz), 0], [np.sin(rz), np.cos(rz), 0], [0, 0, 1] ]) return Rz @ Ry @ Rx
|