gear360-stitcher 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,11 @@
1
+ from .camera import DoubleSphere, ParamsDoubleSphere
2
+ from .stitching import FisheyeCamera, Gear360Stitcher, StitchConfig, VideoStitchConfig
3
+
4
+ __all__ = [
5
+ "DoubleSphere",
6
+ "ParamsDoubleSphere",
7
+ "FisheyeCamera",
8
+ "Gear360Stitcher",
9
+ "StitchConfig",
10
+ "VideoStitchConfig",
11
+ ]
@@ -0,0 +1,74 @@
1
+ """Minimal Double Sphere camera model (Usenko et al., "The Double Sphere
2
+ Camera Model") -- just the projection the stitcher needs, with no torch or
3
+ autocalib dependency.
4
+ """
5
+
6
+ from __future__ import annotations
7
+
8
+ import json
9
+ from pathlib import Path
10
+ from typing import NamedTuple
11
+
12
+ import numpy as np
13
+
14
+
15
+ class ParamsDoubleSphere(NamedTuple):
16
+ fx: float
17
+ fy: float
18
+ cx: float
19
+ cy: float
20
+ alpha: float
21
+ Xi: float
22
+
23
+
24
+ class DoubleSphere:
25
+ def __init__(self, params: ParamsDoubleSphere):
26
+ self.params = params
27
+
28
+ def world2cam(self, XYZ):
29
+ """Project
30
+
31
+ Args:
32
+ XYZ: shape [height, width, 3]
33
+ Returns:
34
+ uv: float32 [height, width, 2] pixel coordinates
35
+ valid: bool [1, 1, height, width]
36
+ """
37
+ p = self.params
38
+ h, w = XYZ.shape[0:2]
39
+ x, y, z = XYZ.reshape(-1, 3).T
40
+ d1 = np.sqrt(x**2 + y**2 + z**2)
41
+ d2 = np.sqrt(x**2 + y**2 + (p.Xi * d1 + z) ** 2)
42
+ z_fe = p.alpha * d2 + (1 - p.alpha) * (p.Xi * d1 + z)
43
+
44
+ u = p.fx * x / z_fe + p.cx
45
+ v = p.fy * y / z_fe + p.cy
46
+ uv = np.stack((u, v), axis=1).reshape((h, w, 2))
47
+
48
+ if p.alpha > 0.5:
49
+ w1 = (1 - p.alpha) / p.alpha
50
+ else:
51
+ w1 = p.alpha / (1 - p.alpha)
52
+ w2 = (w1 + p.Xi) / np.sqrt(2 * w1 * p.Xi + p.Xi**2 + 1)
53
+ valid = (z > -w2 * d1).reshape((1, 1, h, w))
54
+
55
+ return uv.astype("float32"), valid
56
+
57
+
58
+ def load_calibration(path: Path) -> tuple[ParamsDoubleSphere, tuple[int, int]]:
59
+ """Read a lens calibration.json.
60
+
61
+ Format::
62
+
63
+ {"resolution": [width, height],
64
+ "params_ds": {"fx": ..., "fy": ..., "cx": ..., "cy": ...,
65
+ "alpha": ..., "Xi": ...}}
66
+
67
+ Returns the Double Sphere parameters and the (width, height) the
68
+ calibration was fit at.
69
+ """
70
+ with open(path) as f:
71
+ calib = json.load(f)
72
+ params = ParamsDoubleSphere(**{k: float(calib["params_ds"][k]) for k in ParamsDoubleSphere._fields})
73
+ width, height = calib["resolution"]
74
+ return params, (int(width), int(height))