Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix even kernel and add test (#1183)

Dmytro Mishkin9518ed02ba77
diff --git a/kornia/geometry/transform/affwarp.py b/kornia/geometry/transform/affwarp.py
index ad696dca9..06325b4e6 100644
--- a/kornia/geometry/transform/affwarp.py
+++ b/kornia/geometry/transform/affwarp.py
@@ -585,7 +585,15 @@ def resize(
# Now kernel size. Good results are for 3 sigma, but that is kind of slow. Pillow uses 1 sigma
# https://github.com/python-pillow/Pillow/blob/master/src/libImaging/Resample.c#L206
# But they do it in the 2 passes, which gives better results. Let's try 2 sigmas for now
- ks = int(2.0 * 2 * sigmas[0] + 1), int(2.0 * 2 * sigmas[1] + 1)
+ ks = int(2.0 * 2 * sigmas[0]), int(2.0 * 2 * sigmas[1])
+
+ # Make sure it is odd
+ if (ks[0] % 2) == 0:
+ ks = ks[0] + 1, ks[1]
+
+ if (ks[1] % 2) == 0:
+ ks = ks[0], ks[1] + 1
+
input = kornia.filters.gaussian_blur2d(input, ks, sigmas)
output = torch.nn.functional.interpolate(input, size=size, mode=interpolation, align_corners=align_corners)
diff --git a/test/geometry/transform/test_affine.py b/test/geometry/transform/test_affine.py
index 87cb0307a..0c8d3da87 100644
--- a/test/geometry/transform/test_affine.py
+++ b/test/geometry/transform/test_affine.py
@@ -73,6 +73,10 @@ class TestResize:
out = kornia.resize(inp, (5, 3), antialias=True)
assert out.shape == (1, 3, 5, 3)
+ inp = torch.rand(1, 1, 20, 10, device=device, dtype=dtype)
+ out = kornia.resize(inp, (15, 8), antialias=True)
+ assert out.shape == (1, 1, 15, 8)
+
# 2D
inp = torch.rand(10, 8, device=device, dtype=dtype)
out = kornia.resize(inp, (5, 3), antialias=True)