Skip to content

Commit

Permalink
Changes related to google-research#13: Adds focal, fov
Browse files Browse the repository at this point in the history
  • Loading branch information
Achint08 committed May 30, 2022
1 parent 2addbe6 commit cc73d4a
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 5 deletions.
39 changes: 37 additions & 2 deletions visu3d/dc_arrays/camera_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ class implementation.
repr=False,
init=False,
)
focal_length: Tuple[float, float]

@property
def h(self) -> int:
Expand All @@ -126,6 +127,41 @@ def wh(self) -> tuple[int, int]:
"""`(Width, Height)` in pixel (for usage in `(u, v)` coordinates)."""
return (self.w, self.h)

@property
def fw(self) -> float:
"""Focal length in pixels (for usage in `(u, v)` coordinates)."""
return self.focal_length[0]

@property
def fh(self) -> float:
"""Focal length in pixels (for usage in `(u, v)` coordinates)."""
return self.focal_length[1]

@property
def focal_px_wh(self) -> tuple[float, float]:
"""Focal length in pixel (`(fw, fh)`)."""
return (self.fw, self.fh)

@property
def focal_px(self) -> float:
"""Unique Focal length in pixels (when fw == fh)."""

def _err_msg():
return (
'Cannot get `CameraSpec.focal_px` when fw and fh are '
f'different: {self.focal_px_wh}'
)

if self.fw != self.fh:
raise ValueError(_err_msg())

return self.fw

@property
def fov(self) -> float:
"""Field of view in radians."""
raise NotImplementedError

# @abc.abstractmethod
@property
def px_from_cam(self) -> transformation.TransformBase:
Expand Down Expand Up @@ -300,8 +336,7 @@ def from_focal(
[0, 0, 1],
])
return cls(
K=K,
resolution=resolution,
K=K, resolution=resolution, focal_length=(focal_in_px, focal_in_px)
)

@property
Expand Down
8 changes: 7 additions & 1 deletion visu3d/dc_arrays/camera_spec_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

H, W = 640, 480

focal_in_px = 35.


def make_camera_spec(
*,
Expand All @@ -34,7 +36,7 @@ def make_camera_spec(
) -> v3d.PinholeCamera:
spec = v3d.PinholeCamera.from_focal(
resolution=(H, W),
focal_in_px=35.,
focal_in_px=focal_in_px,
xnp=xnp,
)
spec = spec.broadcast_to(shape)
Expand All @@ -58,6 +60,10 @@ def test_camera_spec_init(
assert spec.hw == (H, W)
assert spec.shape == spec_shape
assert spec.K.shape == spec_shape + (3, 3)
assert spec.fw == focal_in_px
assert spec.fh == focal_in_px
assert spec.focal_px_wh == (focal_in_px, focal_in_px)
assert spec.focal_px == focal_in_px

if xnp is None:
xnp = np
Expand Down
10 changes: 8 additions & 2 deletions visu3d/dc_arrays/camera_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
# Activate the fixture
set_tnp = enp.testing.set_tnp


H, W = 64, 128
focal_in_px = 34.


def _make_cam(
Expand All @@ -34,7 +34,9 @@ def _make_cam(
shape: v3d.typing.Shape,
) -> v3d.Camera:
"""Create a camera at (0, 4, 0) looking at the center."""
spec = v3d.PinholeCamera.from_focal(resolution=(H, W), focal_in_px=34.)
spec = v3d.PinholeCamera.from_focal(
resolution=(H, W), focal_in_px=focal_in_px
)
cam = v3d.Camera.from_look_at(
spec=spec.as_xnp(xnp),
pos=[0, 4, 0], # Camera on the `y` axis
Expand All @@ -59,6 +61,10 @@ def test_camera_properties(xnp: enp.NpModule, shape: v3d.typing.Shape):
assert cam.w == W
assert cam.wh == (W, H)
assert cam.hw == (H, W)
assert cam.spec.fw == focal_in_px
assert cam.spec.fh == focal_in_px
assert cam.spec.focal_px_wh == (focal_in_px, focal_in_px)
assert cam.spec.focal_px == focal_in_px


@enp.testing.parametrize_xnp()
Expand Down

0 comments on commit cc73d4a

Please sign in to comment.