Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

allow warp_affine fill num_channels other than three (#2568)

Edgar Riba85d94df3c800
diff --git a/kornia/geometry/transform/imgwarp.py b/kornia/geometry/transform/imgwarp.py
index b8cc1218f..e38d125e1 100644
--- a/kornia/geometry/transform/imgwarp.py
+++ b/kornia/geometry/transform/imgwarp.py
@@ -184,11 +184,6 @@ def warp_affine(
if not (len(M.shape) == 3 or M.shape[-2:] == (2, 3)):
raise ValueError(f"Input M must be a Bx2x3 tensor. Got {M.shape}")
- # fill padding is only supported for 3 channels because we can't set fill_value default
- # to None as this gives jit issues.
- if padding_mode == "fill" and fill_value.shape != torch.Size([3]):
- raise ValueError(f"Padding_tensor only supported for 3 channels. Got {fill_value.shape}")
-
B, C, H, W = src.size()
# we generate a 3x3 transformation matrix from 2x3 affine
@@ -202,6 +197,7 @@ def warp_affine(
if padding_mode == "fill":
return _fill_and_warp(src, grid, align_corners=align_corners, mode=mode, fill_value=fill_value)
+
return F.grid_sample(src, grid, align_corners=align_corners, mode=mode, padding_mode=padding_mode)
diff --git a/test/geometry/transform/test_imgwarp.py b/test/geometry/transform/test_imgwarp.py
index 17876a4fc..36485ef6b 100644
--- a/test/geometry/transform/test_imgwarp.py
+++ b/test/geometry/transform/test_imgwarp.py
@@ -253,6 +253,21 @@ class TestWarpAffine:
assert_close(top_row_mean, fill_value)
assert_close(first_col_mean, fill_value)
+ @pytest.mark.parametrize("num_channels", [1, 3, 5])
+ def test_fill_padding_channels(self, device, dtype, num_channels):
+ offset = 1.0
+ h, w = 3, 4
+ aff_ab = torch.eye(2, 3, device=device, dtype=dtype)[None]
+ aff_ab[..., -1] += offset
+
+ img_b = torch.arange(float(num_channels * h * w), device=device, dtype=dtype).view(1, num_channels, h, w)
+
+ fill_value = torch.zeros(num_channels, device=device, dtype=dtype)
+
+ img_a = kornia.geometry.warp_affine(img_b, aff_ab, (h, w), padding_mode="fill", fill_value=fill_value)
+
+ assert_close(img_a[:, :, :1, :1].squeeze(), fill_value.squeeze())
+
class TestWarpPerspective:
def test_smoke(self, device, dtype):