feat: refine boundary mapping to improve alignment of inner and outer source coordinates

This commit is contained in:
2026-09-15 19:40:40 +08:00
parent 4035f5c9e4
commit 94130ff38f
2 changed files with 14 additions and 0 deletions

View File

@@ -51,6 +51,7 @@ def build_boundary_mapping(content_values, threshold, padding, target_shape=None
)
distance = padding * scale
signed = ndimage.distance_transform_edt(mask) - ndimage.distance_transform_edt(~mask)
signed = np.where(signed != 0.0, signed - 0.5 * np.sign(signed), 0.0)
inward_y = ndimage.sobel(signed, axis=0, mode="nearest")
inward_x = ndimage.sobel(signed, axis=1, mode="nearest")
length = np.hypot(inward_y, inward_x)

View File

@@ -36,6 +36,19 @@ def test_mapping_moves_along_one_direction_instead_of_nearest_content():
assert abs(result[1, 8, 0] - 8) < 0.5
def test_boundary_mapping_aligns_inner_and_outer_source_coordinates():
values = np.zeros((9, 9), dtype=np.float32)
values[3:6, 3:6] = 1.0
mapping = build_boundary_mapping(values, 0.9, 2)
np.testing.assert_allclose(
mapping.source_x[4, 2],
mapping.source_x[4, 3],
atol=0.25,
)
def test_thin_structure_stops_at_the_last_point_on_the_same_ray():
values = np.zeros((11, 15), dtype=np.float32)
values[5:7, 2:13] = 1