Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions hloc/localize_inloc.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
def interpolate_scan(scan, kp):
h, w, c = scan.shape
kp = kp / np.array([[w - 1, h - 1]]) * 2 - 1
assert np.all(kp > -1) and np.all(kp < 1)
assert np.all(kp >= -1) and np.all(kp <= 1)
scan = torch.from_numpy(scan).permute(2, 0, 1)[None]
kp = torch.from_numpy(kp)[None, None]
kp = torch.as_tensor(kp, dtype=scan.dtype, device=scan.device)[None, None]
grid_sample = torch.nn.functional.grid_sample

# To maximize the number of points that have depth:
Expand Down
40 changes: 40 additions & 0 deletions tests/test_scan_interpolation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import numpy as np
import pytest

from hloc.localize_inloc import interpolate_scan


@pytest.mark.parametrize("dtype", [np.float32, np.float64])
@pytest.mark.parametrize("border", [False, True])
def test_scan_interpolation_matches_an_affine_surface(dtype, border):
x, y = np.meshgrid(np.arange(5), np.arange(4))
scan = np.stack([x, y, 2 * x - 3 * y + 7], axis=-1).astype(dtype)
keypoints = np.array([[0.25, 0.5], [2.3, 1.2]], dtype=np.float32)
if border:
keypoints = np.array([[0, 0], [4, 3], [0, 2.5], [4, 0.25]], dtype=np.float32)
expected = np.stack(
[
keypoints[:, 0],
keypoints[:, 1],
2 * keypoints[:, 0] - 3 * keypoints[:, 1] + 7,
],
-1,
)
points, valid = interpolate_scan(scan, keypoints)
assert valid.all()
assert points.dtype == dtype
np.testing.assert_allclose(points, expected, atol=2e-6)


def test_scan_nearest_fallback_retains_valid_depth():
scan = np.ones((4, 5, 3), dtype=np.float32)
scan[1, 2] = np.nan
points, valid = interpolate_scan(scan, np.array([[1.1, 1.1]], dtype=np.float64))
assert valid.all()
np.testing.assert_allclose(points, [[1.0, 1.0, 1.0]])


@pytest.mark.parametrize("keypoint", [[-1.0, 0.5], [5.0, 1.0], [1.0, 4.0]])
def test_scan_interpolation_rejects_outside_pixels(keypoint):
with pytest.raises(AssertionError):
interpolate_scan(np.ones((4, 5, 3)), np.array([keypoint]))
Loading