-
Notifications
You must be signed in to change notification settings - Fork 42
/
dewarp.py
38 lines (27 loc) · 1.07 KB
/
dewarp.py
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
import numpy as np
def equirect_proj(x_proj, y_proj, W, H, fov):
"""Return the equirectangular projection on a unit sphere,
given cartesian coordinates of the de-warped image."""
theta_alt = x_proj * fov / W
phi_alt = y_proj * np.pi / H
x = np.sin(theta_alt) * np.cos(phi_alt)
y = np.sin(phi_alt)
z = np.cos(theta_alt) * np.cos(phi_alt)
return np.arctan2(y, x), np.arctan2(np.sqrt(x**2 + y**2), z)
def buildmap(Ws, Hs, Wd, Hd, fov=180.0):
"""Return a mapping from de-warped images to fisheye images."""
fov = fov * np.pi / 180.0
# cartesian coordinates of the de-warped rectangular image
ys, xs = np.indices((Hs, Ws), np.float32)
y_proj = Hs / 2.0 - ys
x_proj = xs - Ws / 2.0
# spherical coordinates
theta, phi = equirect_proj(x_proj, y_proj, Ws, Hs, fov)
# polar coordinates (of the fisheye image)
p = Hd * phi / fov
# cartesian coordinates of the fisheye image
y_fish = p * np.sin(theta)
x_fish = p * np.cos(theta)
ymap = Hd / 2.0 - y_fish
xmap = Wd / 2.0 + x_fish
return xmap, ymap