Datasets:
Upload 7 files
Browse files- assets/1047_0.png +3 -0
- assets/1047_1.png +3 -0
- assets/888_0.png +3 -0
- assets/888_1.png +3 -0
- assets/HongKong_seq2@500@30_60@cloudy.txt +0 -0
- demo.py +167 -0
- requirements.txt +5 -0
assets/1047_0.png
ADDED
|
Git LFS Details
|
assets/1047_1.png
ADDED
|
Git LFS Details
|
assets/888_0.png
ADDED
|
Git LFS Details
|
assets/888_1.png
ADDED
|
Git LFS Details
|
assets/HongKong_seq2@500@30_60@cloudy.txt
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
demo.py
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
单文件:参考深度反投影到 ECEF → 投影到 query 相机 → 并排可视化对应点。
|
| 3 |
+
运行: python demo.py
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
from pathlib import Path
|
| 7 |
+
|
| 8 |
+
import cv2
|
| 9 |
+
import numpy as np
|
| 10 |
+
import pyproj
|
| 11 |
+
from scipy.spatial.transform import Rotation as R
|
| 12 |
+
|
| 13 |
+
# 与 poses 里帧名一致(如 888.jpg)
|
| 14 |
+
REF_STEM = "888"
|
| 15 |
+
QUERY_STEM = "1047"
|
| 16 |
+
NUM_SAMPLES = 100
|
| 17 |
+
# 内参 fx, fy, cx, cy — 需与渲染一致
|
| 18 |
+
FX, FY, CX, CY = 1931.7, 1931.7, 800.0, 600.0
|
| 19 |
+
W, H = 1600, 1200
|
| 20 |
+
|
| 21 |
+
ROOT = Path(__file__).resolve().parent
|
| 22 |
+
ASSETS = ROOT / "assets"
|
| 23 |
+
OUT = ROOT / "outputs"
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def wgs84_to_ecef(lon, lat, h):
|
| 27 |
+
t = pyproj.Transformer.from_crs(
|
| 28 |
+
"EPSG:4326",
|
| 29 |
+
{"proj": "geocent", "ellps": "WGS84", "datum": "WGS84"},
|
| 30 |
+
always_xy=True,
|
| 31 |
+
)
|
| 32 |
+
x, y, z = t.transform(lon, lat, h, radians=False)
|
| 33 |
+
return np.array([x, y, z], np.float64)
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def enu_to_ecef_rot(lon, lat):
|
| 37 |
+
lat_r, lon_r = np.radians(lat), np.radians(lon)
|
| 38 |
+
up = np.array(
|
| 39 |
+
[
|
| 40 |
+
np.cos(lon_r) * np.cos(lat_r),
|
| 41 |
+
np.sin(lon_r) * np.cos(lat_r),
|
| 42 |
+
np.sin(lat_r),
|
| 43 |
+
]
|
| 44 |
+
)
|
| 45 |
+
east = np.array([-np.sin(lon_r), np.cos(lon_r), 0.0])
|
| 46 |
+
north = np.cross(up, east)
|
| 47 |
+
m = np.zeros((3, 3))
|
| 48 |
+
m[:, 0], m[:, 1], m[:, 2] = east, north, up
|
| 49 |
+
return m
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
def pose_to_c2w_ecef(lon, lat, alt, roll, pitch, yaw):
|
| 53 |
+
r_pose = R.from_euler("xyz", [pitch, roll, yaw], degrees=True).as_matrix()
|
| 54 |
+
r_enu = enu_to_ecef_rot(lon, lat)
|
| 55 |
+
r_c2w = r_enu @ r_pose
|
| 56 |
+
t = wgs84_to_ecef(lon, lat, alt)
|
| 57 |
+
T = np.eye(4, dtype=np.float64)
|
| 58 |
+
T[:3, :3] = r_c2w
|
| 59 |
+
T[:3, 3] = t
|
| 60 |
+
T[:3, 1] *= -1
|
| 61 |
+
T[:3, 2] *= -1
|
| 62 |
+
return T
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
def load_poses(path):
|
| 66 |
+
d = {}
|
| 67 |
+
for line in open(path, encoding="utf-8"):
|
| 68 |
+
p = line.split()
|
| 69 |
+
if p:
|
| 70 |
+
d[p[0]] = list(map(float, p[1:]))
|
| 71 |
+
return d
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
def unproject_xy_depth(xy, z, T_c2w, fx, fy, cx, cy):
|
| 75 |
+
"""像素 (x,y) + 深度 z → ECEF。"""
|
| 76 |
+
x, y = xy[:, 0], xy[:, 1]
|
| 77 |
+
z = z.astype(np.float64)
|
| 78 |
+
xc = z * (x - cx) / fx
|
| 79 |
+
yc = z * (y - cy) / fy
|
| 80 |
+
pc = np.stack([xc, yc, z], axis=1)
|
| 81 |
+
Rm, t = T_c2w[:3, :3], T_c2w[:3, 3]
|
| 82 |
+
return (Rm @ pc.T).T + t
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
def project_ecef(pts, T_c2w, fx, fy, cx, cy):
|
| 86 |
+
"""ECEF → 像素 (x,y)。"""
|
| 87 |
+
Rm, t = T_c2w[:3, :3], T_c2w[:3, 3]
|
| 88 |
+
Rinv = Rm.T
|
| 89 |
+
pc = (Rinv @ (pts - t).T).T
|
| 90 |
+
z = pc[:, 2]
|
| 91 |
+
u = fx * pc[:, 0] / z + cx
|
| 92 |
+
v = fy * pc[:, 1] / z + cy
|
| 93 |
+
return np.stack([u, v], axis=1)
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
def vis_side_by_side(img_l, img_r, pl, pr, out_path):
|
| 97 |
+
h1, w1 = img_l.shape[:2]
|
| 98 |
+
h2, w2 = img_r.shape[:2]
|
| 99 |
+
pl = np.asarray(pl, dtype=np.float64)
|
| 100 |
+
pr = np.asarray(pr, dtype=np.float64)
|
| 101 |
+
in_l = (pl[:, 0] >= 0) & (pl[:, 0] < w1) & (pl[:, 1] >= 0) & (pl[:, 1] < h1)
|
| 102 |
+
in_r = (pr[:, 0] >= 0) & (pr[:, 0] < w2) & (pr[:, 1] >= 0) & (pr[:, 1] < h2)
|
| 103 |
+
m = in_l & in_r
|
| 104 |
+
pl, pr = pl[m], pr[m]
|
| 105 |
+
|
| 106 |
+
h = max(h1, h2)
|
| 107 |
+
w = w1 + w2
|
| 108 |
+
vis = np.zeros((h, w, 3), np.uint8)
|
| 109 |
+
vis[:h1, :w1] = img_l
|
| 110 |
+
vis[:h2, w1 : w1 + w2] = img_r
|
| 111 |
+
for i in range(len(pl)):
|
| 112 |
+
c = (int(37 * i % 255), int(91 * i % 255), int(17 * i % 255))
|
| 113 |
+
x1, y1 = int(pl[i, 0]), int(pl[i, 1])
|
| 114 |
+
x2, y2 = int(pr[i, 0]), int(pr[i, 1])
|
| 115 |
+
cv2.circle(vis, (x1, y1), 4, c, -1)
|
| 116 |
+
cv2.circle(vis, (x2 + w1, y2), 4, c, -1)
|
| 117 |
+
cv2.line(vis, (x1, y1), (x2 + w1, y2), c, 2)
|
| 118 |
+
OUT.mkdir(parents=True, exist_ok=True)
|
| 119 |
+
cv2.imwrite(str(out_path), vis)
|
| 120 |
+
print("Wrote", out_path)
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
def load_bgr(rgb_path, depth_path):
|
| 124 |
+
im = cv2.imread(str(rgb_path))
|
| 125 |
+
if im is not None:
|
| 126 |
+
return im
|
| 127 |
+
d = cv2.imread(str(depth_path), cv2.IMREAD_UNCHANGED)
|
| 128 |
+
d = np.flipud(d[:, :, 0] if d.ndim == 3 else d).astype(np.float32)
|
| 129 |
+
v = d[d > 0]
|
| 130 |
+
lo, hi = (np.percentile(v, [2, 98]) if v.size else (0.0, 1.0))
|
| 131 |
+
g = (np.clip((d - lo) / (hi - lo + 1e-6), 0, 1) * 255).astype(np.uint8)
|
| 132 |
+
return cv2.applyColorMap(g, cv2.COLORMAP_VIRIDIS)
|
| 133 |
+
|
| 134 |
+
|
| 135 |
+
def main():
|
| 136 |
+
pose_txt = ASSETS / "HongKong_seq2@500@30_60@cloudy.txt"
|
| 137 |
+
ref_d = ASSETS / f"{REF_STEM}_1.png"
|
| 138 |
+
q_rgb = ASSETS / f"{QUERY_STEM}_0.png"
|
| 139 |
+
q_d = ASSETS / f"{QUERY_STEM}_1.png"
|
| 140 |
+
|
| 141 |
+
poses = load_poses(pose_txt)
|
| 142 |
+
T_ref = pose_to_c2w_ecef(*poses[f"{REF_STEM}.jpg"])
|
| 143 |
+
T_q = pose_to_c2w_ecef(*poses[f"{QUERY_STEM}.jpg"])
|
| 144 |
+
|
| 145 |
+
depth = cv2.imread(str(ref_d), cv2.IMREAD_UNCHANGED)
|
| 146 |
+
depth = np.ascontiguousarray(np.flipud(depth))
|
| 147 |
+
|
| 148 |
+
rng = np.random.default_rng(0)
|
| 149 |
+
ex = rng.integers(0, W, size=NUM_SAMPLES)
|
| 150 |
+
ey = rng.integers(0, H, size=NUM_SAMPLES)
|
| 151 |
+
xy = np.column_stack([ex.astype(np.float64), ey.astype(np.float64)])
|
| 152 |
+
z = depth[ey, ex].astype(np.float64)
|
| 153 |
+
|
| 154 |
+
m = z > 0
|
| 155 |
+
xy, z = xy[m], z[m]
|
| 156 |
+
|
| 157 |
+
pts_ecef = unproject_xy_depth(xy, z, T_ref, FX, FY, CX, CY)
|
| 158 |
+
uv_q = project_ecef(pts_ecef, T_q, FX, FY, CX, CY)
|
| 159 |
+
|
| 160 |
+
ref_img = load_bgr(ASSETS / f"{REF_STEM}_0.png", ref_d)
|
| 161 |
+
q_img = load_bgr(q_rgb, q_d)
|
| 162 |
+
|
| 163 |
+
vis_side_by_side(q_img, ref_img, uv_q, xy, OUT / "reprojection_matches.png")
|
| 164 |
+
|
| 165 |
+
|
| 166 |
+
if __name__ == "__main__":
|
| 167 |
+
main()
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
numpy>=1.20
|
| 2 |
+
scipy>=1.7
|
| 3 |
+
torch>=1.10
|
| 4 |
+
opencv-python>=4.5
|
| 5 |
+
pyproj>=3.3
|