Datasets:
File size: 5,092 Bytes
11a632c | 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 | """
单文件:参考深度反投影到 ECEF → 投影到 query 相机 → 并排可视化对应点。
运行: python demo.py
"""
from pathlib import Path
import cv2
import numpy as np
import pyproj
from scipy.spatial.transform import Rotation as R
# 与 poses 里帧名一致(如 888.jpg)
REF_STEM = "888"
QUERY_STEM = "1047"
NUM_SAMPLES = 100
# 内参 fx, fy, cx, cy — 需与渲染一致
FX, FY, CX, CY = 1931.7, 1931.7, 800.0, 600.0
W, H = 1600, 1200
ROOT = Path(__file__).resolve().parent
ASSETS = ROOT / "assets"
OUT = ROOT / "outputs"
def wgs84_to_ecef(lon, lat, h):
t = pyproj.Transformer.from_crs(
"EPSG:4326",
{"proj": "geocent", "ellps": "WGS84", "datum": "WGS84"},
always_xy=True,
)
x, y, z = t.transform(lon, lat, h, radians=False)
return np.array([x, y, z], np.float64)
def enu_to_ecef_rot(lon, lat):
lat_r, lon_r = np.radians(lat), np.radians(lon)
up = np.array(
[
np.cos(lon_r) * np.cos(lat_r),
np.sin(lon_r) * np.cos(lat_r),
np.sin(lat_r),
]
)
east = np.array([-np.sin(lon_r), np.cos(lon_r), 0.0])
north = np.cross(up, east)
m = np.zeros((3, 3))
m[:, 0], m[:, 1], m[:, 2] = east, north, up
return m
def pose_to_c2w_ecef(lon, lat, alt, roll, pitch, yaw):
r_pose = R.from_euler("xyz", [pitch, roll, yaw], degrees=True).as_matrix()
r_enu = enu_to_ecef_rot(lon, lat)
r_c2w = r_enu @ r_pose
t = wgs84_to_ecef(lon, lat, alt)
T = np.eye(4, dtype=np.float64)
T[:3, :3] = r_c2w
T[:3, 3] = t
T[:3, 1] *= -1
T[:3, 2] *= -1
return T
def load_poses(path):
d = {}
for line in open(path, encoding="utf-8"):
p = line.split()
if p:
d[p[0]] = list(map(float, p[1:]))
return d
def unproject_xy_depth(xy, z, T_c2w, fx, fy, cx, cy):
"""像素 (x,y) + 深度 z → ECEF。"""
x, y = xy[:, 0], xy[:, 1]
z = z.astype(np.float64)
xc = z * (x - cx) / fx
yc = z * (y - cy) / fy
pc = np.stack([xc, yc, z], axis=1)
Rm, t = T_c2w[:3, :3], T_c2w[:3, 3]
return (Rm @ pc.T).T + t
def project_ecef(pts, T_c2w, fx, fy, cx, cy):
"""ECEF → 像素 (x,y)。"""
Rm, t = T_c2w[:3, :3], T_c2w[:3, 3]
Rinv = Rm.T
pc = (Rinv @ (pts - t).T).T
z = pc[:, 2]
u = fx * pc[:, 0] / z + cx
v = fy * pc[:, 1] / z + cy
return np.stack([u, v], axis=1)
def vis_side_by_side(img_l, img_r, pl, pr, out_path):
h1, w1 = img_l.shape[:2]
h2, w2 = img_r.shape[:2]
pl = np.asarray(pl, dtype=np.float64)
pr = np.asarray(pr, dtype=np.float64)
in_l = (pl[:, 0] >= 0) & (pl[:, 0] < w1) & (pl[:, 1] >= 0) & (pl[:, 1] < h1)
in_r = (pr[:, 0] >= 0) & (pr[:, 0] < w2) & (pr[:, 1] >= 0) & (pr[:, 1] < h2)
m = in_l & in_r
pl, pr = pl[m], pr[m]
h = max(h1, h2)
w = w1 + w2
vis = np.zeros((h, w, 3), np.uint8)
vis[:h1, :w1] = img_l
vis[:h2, w1 : w1 + w2] = img_r
for i in range(len(pl)):
c = (int(37 * i % 255), int(91 * i % 255), int(17 * i % 255))
x1, y1 = int(pl[i, 0]), int(pl[i, 1])
x2, y2 = int(pr[i, 0]), int(pr[i, 1])
cv2.circle(vis, (x1, y1), 4, c, -1)
cv2.circle(vis, (x2 + w1, y2), 4, c, -1)
cv2.line(vis, (x1, y1), (x2 + w1, y2), c, 2)
OUT.mkdir(parents=True, exist_ok=True)
cv2.imwrite(str(out_path), vis)
print("Wrote", out_path)
def load_bgr(rgb_path, depth_path):
im = cv2.imread(str(rgb_path))
if im is not None:
return im
d = cv2.imread(str(depth_path), cv2.IMREAD_UNCHANGED)
d = np.flipud(d[:, :, 0] if d.ndim == 3 else d).astype(np.float32)
v = d[d > 0]
lo, hi = (np.percentile(v, [2, 98]) if v.size else (0.0, 1.0))
g = (np.clip((d - lo) / (hi - lo + 1e-6), 0, 1) * 255).astype(np.uint8)
return cv2.applyColorMap(g, cv2.COLORMAP_VIRIDIS)
def main():
pose_txt = ASSETS / "HongKong_seq2@500@30_60@cloudy.txt"
ref_d = ASSETS / f"{REF_STEM}_1.png"
q_rgb = ASSETS / f"{QUERY_STEM}_0.png"
q_d = ASSETS / f"{QUERY_STEM}_1.png"
poses = load_poses(pose_txt)
T_ref = pose_to_c2w_ecef(*poses[f"{REF_STEM}.jpg"])
T_q = pose_to_c2w_ecef(*poses[f"{QUERY_STEM}.jpg"])
depth = cv2.imread(str(ref_d), cv2.IMREAD_UNCHANGED)
depth = np.ascontiguousarray(np.flipud(depth))
rng = np.random.default_rng(0)
ex = rng.integers(0, W, size=NUM_SAMPLES)
ey = rng.integers(0, H, size=NUM_SAMPLES)
xy = np.column_stack([ex.astype(np.float64), ey.astype(np.float64)])
z = depth[ey, ex].astype(np.float64)
m = z > 0
xy, z = xy[m], z[m]
pts_ecef = unproject_xy_depth(xy, z, T_ref, FX, FY, CX, CY)
uv_q = project_ecef(pts_ecef, T_q, FX, FY, CX, CY)
ref_img = load_bgr(ASSETS / f"{REF_STEM}_0.png", ref_d)
q_img = load_bgr(q_rgb, q_d)
vis_side_by_side(q_img, ref_img, uv_q, xy, OUT / "reprojection_matches.png")
if __name__ == "__main__":
main()
|