Skip to content

Commit

Permalink
e2c and c2e tests; fix flipped image issue mentioned in #11
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianPugh committed Dec 15, 2024
1 parent 01eee66 commit a47eb50
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 17 deletions.
Binary file modified assets/demo_equirec.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/demo_horizon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 6 additions & 13 deletions py360convert/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,19 @@ def xyzcube(face_w: int) -> NDArray[np.float32]:

# Right face (x = 0.5)
out[:, 1 * face_w : 2 * face_w, [2, 1]] = grid
out[:, 1 * face_w : 2 * face_w, [2, 1]] = np.flip(grid, axis=1)
out[:, 1 * face_w : 2 * face_w, 0] = 0.5

# Back face (z = -0.5)
out[:, 2 * face_w : 3 * face_w, [0, 1]] = grid
out[:, 2 * face_w : 3 * face_w, [0, 1]] = np.flip(grid, axis=1)
out[:, 2 * face_w : 3 * face_w, 2] = -0.5

# Left face (x = -0.5)
out[:, 3 * face_w : 4 * face_w, [2, 1]] = grid
out[:, 3 * face_w : 4 * face_w, 0] = -0.5

# Up face (y = 0.5)
out[:, 4 * face_w : 5 * face_w, [0, 2]] = grid
out[:, 4 * face_w : 5 * face_w, [0, 2]] = np.flip(grid, axis=0)
out[:, 4 * face_w : 5 * face_w, 1] = 0.5

# Down face (y = -0.5)
Expand Down Expand Up @@ -213,9 +214,9 @@ def sample_cubefaces(
cube_faces: NDArray[DType], tp: NDArray, coor_y: NDArray, coor_x: NDArray, order: int
) -> NDArray[DType]:
cube_faces = cube_faces.copy()
cube_faces[1] = np.flip(cube_faces[1], 1)
cube_faces[2] = np.flip(cube_faces[2], 1)
cube_faces[4] = np.flip(cube_faces[4], 0)
# cube_faces[1] = np.flip(cube_faces[1], 1)
# cube_faces[2] = np.flip(cube_faces[2], 1)
# cube_faces[4] = np.flip(cube_faces[4], 0)

# Pad up down
pad_ud = np.zeros((6, 2, cube_faces.shape[2]), dtype=cube_faces.dtype)
Expand Down Expand Up @@ -297,10 +298,6 @@ def cube_h2dice(cube_h: NDArray[DType]) -> NDArray[DType]:
sxy = [(1, 1), (2, 1), (3, 1), (0, 1), (1, 0), (1, 2)]
for i, (sx, sy) in enumerate(sxy):
face = cube_list[i]
if i in [1, 2]:
face = np.flip(face, axis=1)
if i == 4:
face = np.flip(face, axis=0)
cube_dice[sy * w : (sy + 1) * w, sx * w : (sx + 1) * w] = face
return cube_dice

Expand All @@ -316,10 +313,6 @@ def cube_dice2h(cube_dice: NDArray[DType]) -> NDArray[DType]:
sxy = [(1, 1), (2, 1), (3, 1), (0, 1), (1, 0), (1, 2)]
for i, (sx, sy) in enumerate(sxy):
face = cube_dice[sy * w : (sy + 1) * w, sx * w : (sx + 1) * w]
if i in [1, 2]:
face = np.flip(face, axis=1)
if i == 4:
face = np.flip(face, axis=0)
cube_h[:, i * w : (i + 1) * w] = face
return cube_h

Expand Down
34 changes: 34 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,37 @@
@pytest.fixture
def dice_image():
return np.array(Image.open("assets/demo_cube.png"))


@pytest.fixture
def equirec_image():
return np.array(Image.open("assets/demo_equirec.png"))


@pytest.fixture
def horizon_image():
return np.array(Image.open("assets/demo_horizon.png"))


@pytest.fixture
def list_image(horizon_image):
return [
horizon_image[:, 0:256],
horizon_image[:, 256:512],
horizon_image[:, 512:768],
horizon_image[:, 768:1024],
horizon_image[:, 1024:1280],
horizon_image[:, 1280:1536],
]


@pytest.fixture
def dict_image(list_image):
return {
"F": list_image[0],
"R": list_image[1],
"B": list_image[2],
"L": list_image[3],
"U": list_image[4],
"D": list_image[5],
}
2 changes: 0 additions & 2 deletions tests/test_c2e.py

This file was deleted.

2 changes: 0 additions & 2 deletions tests/test_e2c.py

This file was deleted.

70 changes: 70 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import numpy as np
import pytest

import py360convert

AVG_DIFF_THRESH = 1.2


def diff(x, y):
return np.abs(x.astype(float) - y.astype(float))


def test_c2e_dice(equirec_image, dice_image):
equirec_actual = py360convert.c2e(dice_image, 512, 1024)
equirec_diff = diff(equirec_image, equirec_actual)
assert equirec_diff.mean() < AVG_DIFF_THRESH


def test_c2e_horizon(equirec_image, horizon_image):
equirec_actual = py360convert.c2e(horizon_image, 512, 1024, cube_format="horizon")
equirec_diff = diff(equirec_image, equirec_actual)
assert equirec_diff.mean() < AVG_DIFF_THRESH


def test_c2e_dict(equirec_image, dict_image):
equirec_actual = py360convert.c2e(dict_image, 512, 1024, cube_format="dict")
equirec_diff = diff(equirec_image, equirec_actual)
assert equirec_diff.mean() < AVG_DIFF_THRESH


def test_c2e_list(equirec_image, list_image):
equirec_actual = py360convert.c2e(list_image, 512, 1024, cube_format="list")
equirec_diff = diff(equirec_image, equirec_actual)
assert equirec_diff.mean() < AVG_DIFF_THRESH


def test_e2c_dice(equirec_image, dice_image):
dice_actual = py360convert.e2c(equirec_image, cube_format="dice")
dice_diff = diff(dice_image, dice_actual)
assert dice_diff.mean() < AVG_DIFF_THRESH


def test_e2c_horizon(equirec_image, horizon_image):
horizon_actual = py360convert.e2c(equirec_image, 256, cube_format="horizon")
horizon_diff = diff(horizon_image, horizon_actual)
assert horizon_diff.mean() < AVG_DIFF_THRESH


def test_e2c_dict(equirec_image, dict_image):
dict_actual = py360convert.e2c(equirec_image, 256, cube_format="dict")
dict_diff = {k: diff(dict_image[k], dict_actual[k]) for k in "FRBLUD"}

assert dict_diff["F"].mean() < AVG_DIFF_THRESH
assert dict_diff["R"].mean() < AVG_DIFF_THRESH
assert dict_diff["B"].mean() < AVG_DIFF_THRESH
assert dict_diff["L"].mean() < AVG_DIFF_THRESH
assert dict_diff["U"].mean() < AVG_DIFF_THRESH
assert dict_diff["D"].mean() < AVG_DIFF_THRESH


def test_e2c_list(equirec_image, list_image):
list_actual = py360convert.e2c(equirec_image, 256, cube_format="list")
list_diff = [diff(list_image[i], list_actual[i]) for i in range(6)]

assert list_diff[0].mean() < AVG_DIFF_THRESH
assert list_diff[1].mean() < AVG_DIFF_THRESH
assert list_diff[2].mean() < AVG_DIFF_THRESH
assert list_diff[3].mean() < AVG_DIFF_THRESH
assert list_diff[4].mean() < AVG_DIFF_THRESH
assert list_diff[5].mean() < AVG_DIFF_THRESH

0 comments on commit a47eb50

Please sign in to comment.