From 94130ff38f29b50ec67a15f2a23a6e179eed79b8 Mon Sep 17 00:00:00 2001 From: nan Date: Tue, 15 Sep 2026 19:40:40 +0800 Subject: [PATCH] feat: refine boundary mapping to improve alignment of inner and outer source coordinates --- .../operators/cutout_tool/boundary_padding.py | 1 + .../operators/test_cutout_boundary_padding.py | 13 +++++++++++++ 2 files changed, 14 insertions(+) diff --git a/src/anyimage/operators/cutout_tool/boundary_padding.py b/src/anyimage/operators/cutout_tool/boundary_padding.py index c5d6ff8..f974e89 100644 --- a/src/anyimage/operators/cutout_tool/boundary_padding.py +++ b/src/anyimage/operators/cutout_tool/boundary_padding.py @@ -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) diff --git a/tests/anyimage/operators/test_cutout_boundary_padding.py b/tests/anyimage/operators/test_cutout_boundary_padding.py index ed81ce2..c6e19ef 100644 --- a/tests/anyimage/operators/test_cutout_boundary_padding.py +++ b/tests/anyimage/operators/test_cutout_boundary_padding.py @@ -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