Skip to content

Commit

Permalink
Add more interpolation modes.
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianPugh committed Dec 16, 2024
1 parent 0a0dd50 commit d305ce7
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 19 deletions.
8 changes: 2 additions & 6 deletions py360convert/c2e.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
cube_list2h,
equirect_facetype,
equirect_uvgrid,
mode_to_order,
sample_cubefaces,
)

Expand Down Expand Up @@ -72,12 +73,7 @@ def c2e(
np.ndarray
Equirectangular image.
"""
if mode == "bilinear":
order = 1
elif mode == "nearest":
order = 0
else:
raise ValueError(f'Unknown mode "{mode}".')
order = mode_to_order(mode)

if cube_format == "horizon":
if not isinstance(cubemap, np.ndarray):
Expand Down
8 changes: 2 additions & 6 deletions py360convert/e2c.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
cube_h2dice,
cube_h2dict,
cube_h2list,
mode_to_order,
sample_equirec,
uv2coor,
xyz2uv,
Expand Down Expand Up @@ -77,12 +78,7 @@ def e2c(
squeeze = False

h, w = e_img.shape[:2]
if mode == "bilinear":
order = 1
elif mode == "nearest":
order = 0
else:
raise ValueError(f'Unknown mode: "{mode}".')
order = mode_to_order(mode)

xyz = xyzcube(face_w)
uv = xyz2uv(xyz)
Expand Down
8 changes: 2 additions & 6 deletions py360convert/e2p.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from .utils import (
DType,
InterpolationMode,
mode_to_order,
sample_equirec,
uv2coor,
xyz2uv,
Expand Down Expand Up @@ -64,12 +65,7 @@ def e2p(

in_rot = np.deg2rad(in_rot_deg)

if mode == "bilinear":
order = 1
elif mode == "nearest":
order = 0
else:
raise ValueError(f'Unknown mode: "{mode}".')
order = mode_to_order(mode)

u = -u_deg * np.pi / 180
v = v_deg * np.pi / 180
Expand Down
48 changes: 47 additions & 1 deletion py360convert/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,57 @@
from numpy.typing import NDArray
from scipy.ndimage import map_coordinates

_mode_to_order = {
"nearest": 0,
"linear": 1,
"bilinear": 1,
"biquadratic": 2,
"quadratic": 2,
"quad": 2,
"bicubic": 3,
"cubic": 3,
"biquartic": 4,
"quartic": 4,
"biquintic": 5,
"quintic": 5,
}

CubeFormat = Literal["horizon", "list", "dict", "dice"]
InterpolationMode = Literal["bilinear", "nearest"]
InterpolationMode = Literal[
"nearest",
"linear",
"bilinear",
"biquadratic",
"quadratic",
"quad",
"bicubic",
"cubic",
"biquartic",
"quartic",
"biquintic",
"quintic",
]
DType = TypeVar("DType", bound=np.generic, covariant=True)


def mode_to_order(mode: InterpolationMode) -> int:
"""Convert a human-friendly interpolation string to integer equivalent.
Parameters
----------
mode: str
Human-friendly interpolation string.
Returns
-------
The order of the spline interpolation
"""
try:
return _mode_to_order[mode.lower()]
except KeyError:
raise ValueError(f'Unknown mode "{mode}".') from None


def xyzcube(face_w: int) -> NDArray[np.float32]:
"""
Return the xyz coordinates of the unit cube in [F R B L U D] format.
Expand Down

0 comments on commit d305ce7

Please sign in to comment.