Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(geometry): respect homography warp mode and align_corners (#5032)

Puttala Jeevan Kumarc4404c832c6d
diff --git a/changelog.d/5032.breaking.md b/changelog.d/5032.breaking.md
new file mode 100644
index 000000000..cd98d14ef
--- /dev/null
+++ b/changelog.d/5032.breaking.md
@@ -0,0 +1 @@
+`homography_warp` with `normalized_homography=False` now passes `mode` and `align_corners` on to `warp_perspective`. Previously that path always sampled with `mode="bilinear"` and `align_corners=True`, whatever the caller passed. A call with the default arguments now uses the signature's `align_corners=False`: with `padding_mode="zeros"` or `"border"` the output moves only by float rounding, but with `padding_mode="reflection"` the out-of-bounds samples change. Pass `align_corners=True` to get the previous output. Callers who passed `mode="nearest"` or `mode="bicubic"` now get that interpolation instead of bilinear.
diff --git a/kornia/geometry/transform/imgwarp.py b/kornia/geometry/transform/imgwarp.py
index 0ea578b35..f5af23383 100644
--- a/kornia/geometry/transform/imgwarp.py
+++ b/kornia/geometry/transform/imgwarp.py
@@ -1703,8 +1703,6 @@ def homography_warp(
:func:`warp_perspective`
- ``dsize`` is ``(h, w)``
- align_corners: ``False`` by default (differs from :func:`warp_perspective`)
- - with ``normalized_homography=False``, ``mode`` and ``align_corners`` are ignored and
- ``'bilinear'``/``True`` are used (`#4772 <https://github.com/kornia/kornia/issues/4772>`_)
- negative output dimensions raise ``ValueError``
Args:
@@ -1759,7 +1757,7 @@ def homography_warp(
return F.grid_sample(patch_src, warped_grid, mode=mode, padding_mode=padding_mode, align_corners=align_corners)
return warp_perspective(
- patch_src, src_homo_dst, dsize, mode="bilinear", padding_mode=padding_mode, align_corners=True
+ patch_src, src_homo_dst, dsize, mode=mode, padding_mode=padding_mode, align_corners=align_corners
)
diff --git a/testing/half_precision_xfails/cpu_bfloat16.txt b/testing/half_precision_xfails/cpu_bfloat16.txt
index 9ab4f0990..cd0bf6095 100644
--- a/testing/half_precision_xfails/cpu_bfloat16.txt
+++ b/testing/half_precision_xfails/cpu_bfloat16.txt
@@ -428,7 +428,6 @@ call builtins.AssertionError tests/geometry/transform/test_imgwarp.py::TestWarpP
call builtins.AssertionError tests/geometry/transform/test_imgwarp.py::TestWarpPerspective::test_rotation_inverse[cpu-bfloat16]
call builtins.AssertionError tests/geometry/transform/test_imgwarp.py::TestWarpPerspective::test_translation[cpu-bfloat16]
call builtins.AssertionError tests/geometry/transform/test_imgwarp.py::TestWarpPerspective::test_translation_align_corners[cpu-bfloat16-True]
-call builtins.AssertionError tests/geometry/transform/test_imgwarp.py::TestWarpPerspective::test_translation_normalized[cpu-bfloat16]
call builtins.AssertionError tests/geometry/transform/test_imgwarp3d.py::TestGetRotationMatrix3d::test_rot90x[cpu-bfloat16]
call builtins.AssertionError tests/geometry/transform/test_imgwarp3d.py::TestGetRotationMatrix3d::test_rot90y[cpu-bfloat16]
call builtins.AssertionError tests/geometry/transform/test_imgwarp3d.py::TestGetRotationMatrix3d::test_rot90z[cpu-bfloat16]
diff --git a/tests/geometry/transform/test_imgwarp.py b/tests/geometry/transform/test_imgwarp.py
index e59fb0228..5c22cd8de 100644
--- a/tests/geometry/transform/test_imgwarp.py
+++ b/tests/geometry/transform/test_imgwarp.py
@@ -753,22 +753,25 @@ class TestWarpPerspective(BaseTester):
hw = kornia.geometry.transform.homography_warp(x, _torch_inverse_cast(Hn), (3, 5), align_corners=True)
self.assert_close(hw, expected, atol=1e-4, rtol=1e-4)
- def test_wart_homography_warp_pixel_path_ignores_mode_and_align_corners_4772(self, device, dtype):
- # With normalized_homography=False, homography_warp calls warp_perspective with mode="bilinear"
- # and align_corners=True whatever it was given (#4772). Reflection padding makes align_corners
- # visible; a 1.5-pixel shift makes nearest differ from bilinear. Flips once both are forwarded.
+ @pytest.mark.parametrize("mode", ["bilinear", "nearest"])
+ @pytest.mark.parametrize("align_corners", [True, False])
+ def test_homography_warp_pixel_path_respects_mode_and_align_corners_4772(self, device, dtype, mode, align_corners):
+ # Reflection padding exposes align_corners; a 1.5-pixel shift distinguishes nearest from bilinear.
img = torch.arange(16.0, device=device, dtype=dtype).view(1, 1, 4, 4)
H = torch.tensor([[[1.0, 0.0, 1.5], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]], device=device, dtype=dtype)
- warp = kornia.geometry.transform.warp_perspective
- hw = kornia.geometry.transform.homography_warp(
- img, H, (4, 4), padding_mode="reflection", align_corners=False, normalized_homography=False
+ actual = kornia.geometry.transform.homography_warp(
+ img,
+ H,
+ (4, 4),
+ mode=mode,
+ padding_mode="reflection",
+ align_corners=align_corners,
+ normalized_homography=False,
)
- self.assert_close(hw, warp(img, H, (4, 4), padding_mode="reflection", align_corners=True))
- assert not torch.allclose(hw, warp(img, H, (4, 4), padding_mode="reflection", align_corners=False), atol=0.1)
-
- hn = kornia.geometry.transform.homography_warp(img, H, (4, 4), mode="nearest", normalized_homography=False)
- self.assert_close(hn, warp(img, H, (4, 4), mode="bilinear"))
- assert not torch.allclose(hn, warp(img, H, (4, 4), mode="nearest"), atol=0.1)
+ expected = kornia.geometry.transform.warp_perspective(
+ img, H, (4, 4), mode=mode, padding_mode="reflection", align_corners=align_corners
+ )
+ self.assert_close(actual, expected)
@pytest.mark.parametrize("align_corners", [True, False])
def test_convention_identity_agrees_with_warp_affine(self, align_corners, device, dtype):