feat: unify reference depth calculation by using median-based range factor and baseline

This commit is contained in:
2026-09-16 01:04:59 +08:00
parent 474fc10f0f
commit cf512dc013
6 changed files with 45 additions and 36 deletions

View File

@@ -9,7 +9,7 @@ import pytest
from anyimage.common.coordinate import canonical_direction_to_symmetry_legacy
from anyimage.common.depth import (
FLAT_DEPTH_DIRECTION,
REFERENCE_DEPTH_LIMIT,
REFERENCE_DEPTH_BASELINE,
depth_uniform_scale,
fit_depth_direction,
fit_symmetry_depth_direction,
@@ -70,7 +70,7 @@ def test_base_plane_fit_is_stable_for_a_slender_diagonal_domain():
def test_reference_depth_uses_the_upper_depth_percentile():
depth = np.linspace(1.0, 2.0, 400, dtype=np.float32).reshape(20, 20)
depth = np.linspace(1.0, 1.2, 400, dtype=np.float32).reshape(20, 20)
reference = reference_depth(_camera_depth_image(depth))
@@ -80,7 +80,7 @@ def test_reference_depth_uses_the_upper_depth_percentile():
def test_reference_depth_ignores_transparent_invalid_and_non_positive_pixels():
depth = np.asarray(
((1.0, 2.0, 3.0, 4.0), (5.0, 0.0, np.nan, 6.0)),
((1.0, 1.05, 1.1, 9.0), (1.15, 0.0, np.nan, 1.2)),
dtype=np.float32,
)
alpha = np.asarray(
@@ -90,12 +90,12 @@ def test_reference_depth_ignores_transparent_invalid_and_non_positive_pixels():
reference = reference_depth(_camera_depth_image(depth, alpha))
assert reference == pytest.approx(
float(np.percentile(np.asarray((1.0, 2.0, 3.0, 5.0, 6.0)), 95.0))
float(np.percentile(np.asarray((1.0, 1.05, 1.1, 1.15, 1.2)), 95.0))
)
def test_reference_depth_narrows_its_domain_to_the_mask():
depth = np.asarray(((4.0, 5.0, 1.0, 2.0),), dtype=np.float32)
depth = np.asarray(((1.0, 1.05, 1.1, 1.15),), dtype=np.float32)
image = _camera_depth_image(depth)
mask = np.asarray(((False, False, True, True),))
@@ -103,40 +103,46 @@ def test_reference_depth_narrows_its_domain_to_the_mask():
float(np.percentile(depth, 95.0))
)
assert reference_depth(image, mask) == pytest.approx(
float(np.percentile(np.asarray((1.0, 2.0)), 95.0))
float(np.percentile(np.asarray((1.1, 1.15)), 95.0))
)
def test_reference_depth_ignores_soft_depth_alpha():
depth = np.asarray(((1.0, 2.0, 3.0, 4.0),), dtype=np.float32)
depth = np.asarray(((1.0, 1.05, 1.1, 1.15),), dtype=np.float32)
image = _camera_depth_image(depth, ((1.0, 1.0, 0.9, 0.6),))
assert reference_depth(image) == pytest.approx(
float(np.percentile(np.asarray((1.0, 2.0)), 95.0))
float(np.percentile(np.asarray((1.0, 1.05)), 95.0))
)
def test_reference_depth_ignores_samples_beyond_the_metric_limit():
def test_reference_depth_ignores_a_distant_tail():
depth = np.asarray(((2.0, 4.0, 6.0, 1000.0),), dtype=np.float32)
reference = reference_depth(_camera_depth_image(depth))
assert reference == pytest.approx(
float(np.percentile(np.asarray((2.0, 4.0, 6.0)), 95.0))
float(np.percentile(np.asarray((2.0, 4.0)), 95.0))
)
def test_reference_depth_falls_back_to_the_limit_without_a_selection():
def test_reference_depth_follows_the_content_scale_when_everything_is_far():
depth = np.asarray(((9.4, 10.6, 11.6, 13.0),), dtype=np.float32)
reference = reference_depth(_camera_depth_image(depth))
assert reference == pytest.approx(
float(np.percentile(np.asarray((9.4, 10.6, 11.6)), 95.0))
)
def test_reference_depth_falls_back_to_the_baseline_without_a_selection():
transparent = _camera_depth_image(
np.zeros((2, 2), dtype=np.float32),
np.zeros((2, 2), dtype=np.float32),
)
far = _camera_depth_image(
np.asarray(((1000.0, 2000.0), (4000.0, 8000.0)), dtype=np.float32),
)
assert reference_depth(transparent) == pytest.approx(REFERENCE_DEPTH_LIMIT)
assert reference_depth(far) == pytest.approx(REFERENCE_DEPTH_LIMIT)
assert reference_depth(transparent) == pytest.approx(REFERENCE_DEPTH_BASELINE)
def test_symmetry_depth_direction_fits_a_sloped_base_plane():