File size: 6,861 Bytes
77731f3 | 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 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 | """
ERP 投影模块
ERPT_native ERP投影约定:
- 经度:lon = atan2(x, z),范围 [-π, π]
- 纬度:lat = asin(y),范围 [-π/2, π/2]
- 像素坐标:u ∈ [0, W), v ∈ [0, H)
- 图像中心 (u=W/2, v=H/2) 对应 (lon=0, lat=0),看向 +Z
- 图像顶部 (v=0) 对应 lat=+π/2,看向 +Y(上)
- 图像底部 (v=H-1) 对应 lat=-π/2,看向 -Y(下)
- 图像右边 lon增加,对应 +X 方向
像素到经纬度映射:
lon = (u / W) * 2π - π
lat = π/2 - (v / (H-1)) * π
方向向量(相机坐标系,也是世界坐标系当无旋转时):
x = sin(lon) * cos(lat) # 右
y = sin(lat) # 上
z = cos(lon) * cos(lat) # 前
"""
import math
import numpy as np
import torch
from typing import Tuple, Union
def erp_to_lonlat(
u: Union[np.ndarray, torch.Tensor],
v: Union[np.ndarray, torch.Tensor],
H: int,
W: int,
) -> Tuple[Union[np.ndarray, torch.Tensor], Union[np.ndarray, torch.Tensor]]:
"""
ERP像素坐标转经纬度
Args:
u: 水平像素坐标,范围 [0, W)
v: 垂直像素坐标,范围 [0, H)
H: 图像高度
W: 图像宽度
Returns:
lon: 经度,范围 [-π, π]
lat: 纬度,范围 [-π/2, π/2]
"""
# lon = (u / W) * 2π - π
lon = (u / float(W)) * (2.0 * math.pi) - math.pi
# lat = π/2 - (v / (H-1)) * π
lat = (math.pi / 2.0) - (v / float(H - 1)) * math.pi
return lon, lat
def lonlat_to_erp(
lon: Union[np.ndarray, torch.Tensor],
lat: Union[np.ndarray, torch.Tensor],
H: int,
W: int,
) -> Tuple[Union[np.ndarray, torch.Tensor], Union[np.ndarray, torch.Tensor]]:
"""
经纬度转ERP像素坐标
Args:
lon: 经度,范围 [-π, π]
lat: 纬度,范围 [-π/2, π/2]
H: 图像高度
W: 图像宽度
Returns:
u: 水平像素坐标
v: 垂直像素坐标
"""
# u = (lon + π) / (2π) * W
u = (lon + math.pi) / (2.0 * math.pi) * float(W)
# v = (π/2 - lat) / π * (H-1)
v = (math.pi / 2.0 - lat) / math.pi * float(H - 1)
return u, v
def lonlat_to_direction(
lon: Union[np.ndarray, torch.Tensor],
lat: Union[np.ndarray, torch.Tensor],
) -> Union[np.ndarray, torch.Tensor]:
"""
经纬度转方向向量(单位向量)
坐标系:[X右, Y上, Z前]
Args:
lon: 经度
lat: 纬度
Returns:
dirs: (..., 3) 单位方向向量 [x, y, z]
"""
if isinstance(lon, torch.Tensor):
cos_lat = torch.cos(lat)
x = torch.sin(lon) * cos_lat # 右
y = torch.sin(lat) # 上
z = torch.cos(lon) * cos_lat # 前
dirs = torch.stack([x, y, z], dim=-1)
else:
cos_lat = np.cos(lat)
x = np.sin(lon) * cos_lat
y = np.sin(lat)
z = np.cos(lon) * cos_lat
dirs = np.stack([x, y, z], axis=-1)
return dirs
def direction_to_lonlat(
dirs: Union[np.ndarray, torch.Tensor],
) -> Tuple[Union[np.ndarray, torch.Tensor], Union[np.ndarray, torch.Tensor]]:
"""
方向向量转经纬度
Args:
dirs: (..., 3) 方向向量 [x, y, z]
Returns:
lon: 经度
lat: 纬度
"""
x = dirs[..., 0]
y = dirs[..., 1]
z = dirs[..., 2]
if isinstance(dirs, torch.Tensor):
# 归一化
norm = torch.norm(dirs, dim=-1, keepdim=False)
norm = torch.clamp(norm, min=1e-9)
# lon = atan2(x, z)
lon = torch.atan2(x, z)
# lat = asin(y / norm)
y_normalized = torch.clamp(y / norm, -1.0, 1.0)
lat = torch.asin(y_normalized)
else:
norm = np.linalg.norm(dirs, axis=-1)
norm = np.maximum(norm, 1e-9)
lon = np.arctan2(x, z)
y_normalized = np.clip(y / norm, -1.0, 1.0)
lat = np.arcsin(y_normalized)
return lon, lat
def erp_to_direction(
u: Union[np.ndarray, torch.Tensor],
v: Union[np.ndarray, torch.Tensor],
H: int,
W: int,
) -> Union[np.ndarray, torch.Tensor]:
"""
ERP像素坐标转方向向量
Args:
u: 水平像素坐标
v: 垂直像素坐标
H: 图像高度
W: 图像宽度
Returns:
dirs: (..., 3) 单位方向向量 [x, y, z]
"""
lon, lat = erp_to_lonlat(u, v, H, W)
return lonlat_to_direction(lon, lat)
def direction_to_erp(
dirs: Union[np.ndarray, torch.Tensor],
H: int,
W: int,
) -> Tuple[Union[np.ndarray, torch.Tensor], Union[np.ndarray, torch.Tensor]]:
"""
方向向量转ERP像素坐标
Args:
dirs: (..., 3) 方向向量 [x, y, z]
H: 图像高度
W: 图像宽度
Returns:
u: 水平像素坐标
v: 垂直像素坐标
"""
lon, lat = direction_to_lonlat(dirs)
return lonlat_to_erp(lon, lat, H, W)
def create_erp_grid(
H: int,
W: int,
device: torch.device = None,
) -> Tuple[torch.Tensor, torch.Tensor]:
"""
创建ERP像素网格
Args:
H: 图像高度
W: 图像宽度
device: 计算设备
Returns:
uu: (H, W) 水平坐标网格
vv: (H, W) 垂直坐标网格
"""
if device is None:
device = torch.device("cpu")
us = torch.arange(W, device=device, dtype=torch.float32)
vs = torch.arange(H, device=device, dtype=torch.float32)
vv, uu = torch.meshgrid(vs, us, indexing="ij")
return uu, vv
def create_direction_grid(
H: int,
W: int,
device: torch.device = None,
) -> torch.Tensor:
"""
创建ERP方向向量网格
Args:
H: 图像高度
W: 图像宽度
device: 计算设备
Returns:
dirs: (H, W, 3) 单位方向向量
"""
uu, vv = create_erp_grid(H, W, device)
return erp_to_direction(uu, vv, H, W)
def wrap_u(u: Union[np.ndarray, torch.Tensor], W: int) -> Union[np.ndarray, torch.Tensor]:
"""
水平坐标环绕处理(ERP在水平方向是周期性的)
Args:
u: 水平像素坐标
W: 图像宽度
Returns:
u_wrapped: 环绕后的坐标,范围 [0, W)
"""
if isinstance(u, torch.Tensor):
return torch.remainder(u, float(W))
else:
return np.mod(u, float(W))
def clamp_v(v: Union[np.ndarray, torch.Tensor], H: int) -> Union[np.ndarray, torch.Tensor]:
"""
垂直坐标裁剪处理
Args:
v: 垂直像素坐标
H: 图像高度
Returns:
v_clamped: 裁剪后的坐标,范围 [0, H-1]
"""
if isinstance(v, torch.Tensor):
return torch.clamp(v, 0.0, float(H - 1))
else:
return np.clip(v, 0.0, float(H - 1))
|