Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(geometry): correct similarity rotation for anisotropic images (#5096)

NiteeshGowda72daa8a3a594c9
diff --git a/changelog.d/5096.breaking.md b/changelog.d/5096.breaking.md
new file mode 100644
index 000000000..15cbfe383
--- /dev/null
+++ b/changelog.d/5096.breaking.md
@@ -0,0 +1,10 @@
+`ImageRegistrator`'s `'similarity'` and `'rotation'` models now rotate the image in pixels on a non-square image
+(#5063). `Similarity.forward()` used to apply `scale * angle_to_rotation_matrix(rot)` directly in the normalized
+`[-1, 1]` frame, whose x and y units differ by `W / H`, so on an `H != W` image the model was not a rotation of the
+pixels and a rotated image could not be registered exactly: a 6 degree rotation of a 32 x 48 image was recovered with
+a 1.2 px corner error, against 0.001 px on a 40 x 40 image. `register()` now calls the new
+`Similarity.set_image_shape(height, width)` with the size of `dst_img`, and `forward()` then uses
+`scale * diag(H / W, 1) @ angle_to_rotation_matrix(rot) @ diag(W / H, 1)`, so `rot` and `scale` are the rotation and
+scaling of the pixels about the image centre. On a non-square image the matrices returned by `register()` and used by
+`warp_src_into_dst()` and `warp_dst_into_src()` change. Square images, the `'translation'` and `'scale'` models, and a
+`Similarity` whose image shape was never set are unchanged.
diff --git a/kornia/geometry/transform/image_registrator.py b/kornia/geometry/transform/image_registrator.py
index 55628ddf5..1a819e2da 100644
--- a/kornia/geometry/transform/image_registrator.py
+++ b/kornia/geometry/transform/image_registrator.py
@@ -98,6 +98,19 @@ class Homography(BaseModel):
class Similarity(BaseModel):
"""Similarity geometric model to be used with ImageRegistrator module for the optimization-based image registration.
+ Convention:
+ - ``forward()`` is ``[[scale * R, shift], [0, 0, 1]]`` in the normalized :math:`[-1, 1]` coordinates of
+ :class:`ImageRegistrator` (``align_corners=False``), where ``R`` comes from
+ :func:`~kornia.geometry.conversions.angle_to_rotation_matrix` of ``rot`` in degrees.
+ - After :meth:`set_image_shape` with the image height ``H`` and width ``W``, ``R`` is
+ ``diag(H / W, 1) @ angle_to_rotation_matrix(rot) @ diag(W / H, 1)``. The normalized frame scales x by
+ ``2 / W`` and y by ``2 / H``, so in pixels the linear part is then ``scale * angle_to_rotation_matrix(rot)``
+ about the image centre, for any aspect ratio. :meth:`ImageRegistrator.register` sets the shape of
+ ``dst_img``. ``shift`` stays in normalized units.
+ - Without an image shape, ``R`` is ``angle_to_rotation_matrix(rot)``, which rotates the pixels only on a
+ square image. The shape is not part of ``state_dict()``: after ``load_state_dict``, call
+ :meth:`set_image_shape` again before using the model on a non-square image.
+
Args:
rotation: if True, the rotation is optimizable, else constant zero.
scale: if True, the scale is optimizable, else constant zero.
@@ -107,6 +120,8 @@ class Similarity(BaseModel):
def __init__(self, rotation: bool = True, scale: bool = True, shift: bool = True) -> None:
super().__init__()
+ self.height: Optional[int] = None
+ self.width: Optional[int] = None
if rotation:
self.rot = nn.Parameter(torch.zeros(1))
else:
@@ -132,6 +147,17 @@ class Similarity(BaseModel):
torch.nn.init.zeros_(self.shift)
torch.nn.init.ones_(self.scale)
+ def set_image_shape(self, height: int, width: int) -> None:
+ """Set the image size in pixels, which makes ``rot`` and ``scale`` a rotation and scaling of the pixels.
+
+ Args:
+ height: image height ``H``.
+ width: image width ``W``.
+
+ """
+ self.height = height
+ self.width = width
+
def forward(self) -> torch.Tensor:
r"""Single-batch similarity transform".
@@ -140,6 +166,11 @@ class Similarity(BaseModel):
"""
rot = self.scale * angle_to_rotation_matrix(self.rot)
+ if self.height is not None and self.width is not None:
+ # diag(H / W, 1) @ rot @ diag(W / H, 1): the pixel rotation in the normalized frame, whose x unit is W / 2
+ # pixels and y unit H / 2 pixels. Only the off-diagonal entries change, so the diagonal stays exact.
+ aspect = self.height / self.width
+ rot = rot * rot.new_tensor([[1.0, aspect], [1.0 / aspect, 1.0]])
return convert_affinematrix_to_homography(torch.cat([rot, self.shift], dim=2))
def forward_inverse(self) -> torch.Tensor:
@@ -290,6 +321,10 @@ class ImageRegistrator(nn.Module):
aux_models = []
if len(img_dst_pyr) != len(img_src_pyr):
raise ValueError("Cannot register images of different sizes")
+ if isinstance(self.model, Similarity):
+ # every pyramid level spans the same image, so the full-resolution shape is the pixel aspect ratio of each
+ # level; a level's own rounded shape is not
+ self.model.set_image_shape(dst_img.shape[-2], dst_img.shape[-1])
for img_src_level, img_dst_level in zip(img_src_pyr, img_dst_pyr):
# tolerance compares successive losses of one level; a loss from the coarser level is not one of them
prev_loss: Optional[float] = None
diff --git a/tests/geometry/transform/test_image_registrator.py b/tests/geometry/transform/test_image_registrator.py
index 4c7d8f5bf..adb0fa5d4 100644
--- a/tests/geometry/transform/test_image_registrator.py
+++ b/tests/geometry/transform/test_image_registrator.py
@@ -15,6 +15,7 @@
# limitations under the License.
#
+import math
import sys
import warnings
@@ -24,7 +25,7 @@ import torch
import kornia
from kornia.core._compat import torch_version
from kornia.geometry import transform_points
-from kornia.geometry.conversions import denormalize_homography
+from kornia.geometry.conversions import angle_to_rotation_matrix, denormalize_homography, normalize_homography
from kornia.geometry.transform import ImageRegistrator
from testing.base import BaseTester, supports_bilinear_2d_grid_sample_backward, supports_reflect_padding
@@ -58,6 +59,43 @@ class TestSimilarity(BaseTester):
s = kornia.geometry.transform.Similarity(r, sc, sh).to(device, dtype)
assert s is not None
+ @pytest.mark.parametrize(
+ "height, width, expected",
+ [
+ # diag(H / W, 1) @ R @ diag(W / H, 1) = [[c, s * H / W], [-s * W / H, c]], R = angle_to_rotation_matrix(6)
+ (32, 48, [[0.9945219, 0.0696856, 0.0], [-0.1567927, 0.9945219, 0.0], [0.0, 0.0, 1.0]]),
+ (48, 32, [[0.9945219, 0.1567927, 0.0], [-0.0696856, 0.9945219, 0.0], [0.0, 0.0, 1.0]]),
+ (40, 40, [[0.9945219, 0.1045285, 0.0], [-0.1045285, 0.9945219, 0.0], [0.0, 0.0, 1.0]]),
+ # without an image shape, R itself
+ (None, None, [[0.9945219, 0.1045285, 0.0], [-0.1045285, 0.9945219, 0.0], [0.0, 0.0, 1.0]]),
+ ],
+ )
+ def test_anisotropic_rotation(self, device, dtype, height, width, expected):
+ sim = kornia.geometry.transform.Similarity(True, False, False).to(device, dtype)
+ with torch.no_grad():
+ sim.rot.fill_(6.0)
+ if height is not None:
+ sim.set_image_shape(height, width)
+ expected = torch.tensor([expected], device=device, dtype=dtype)
+ self.assert_close(sim(), expected)
+
+ @pytest.mark.parametrize("height, width", [(32, 48), (48, 32), (33, 47), (40, 40)])
+ def test_pixel_rotation_about_centre(self, device, dtype, height, width):
+ # With the image shape set, the model is scale * angle_to_rotation_matrix(rot) about the image centre in
+ # pixels, normalized like ImageRegistrator's warper (align_corners=False), for any aspect ratio (#5063).
+ sim = kornia.geometry.transform.Similarity(True, True, False).to(device, dtype)
+ with torch.no_grad():
+ sim.rot.fill_(30.0)
+ sim.scale.fill_(1.3)
+ sim.set_image_shape(height, width)
+ linear = 1.3 * angle_to_rotation_matrix(torch.tensor([30.0], dtype=torch.float64))[0]
+ centre = torch.tensor([(width - 1) / 2, (height - 1) / 2], dtype=torch.float64)
+ pixel = torch.eye(3, dtype=torch.float64)
+ pixel[:2, :2] = linear
+ pixel[:2, 2] = centre - linear @ centre
+ expected = normalize_homography(pixel[None], (height, width), (height, width), align_corners=False)
+ self.assert_close(sim(), expected.to(dtype).to(device))
+
class TestHomography(BaseTester):
def test_smoke(self, device, dtype):
@@ -190,3 +228,59 @@ class TestImageRegistrator(BaseTester):
).to(device, dtype)
ir.register(image, image)
assert len(steps) == levels
+
+ def test_register_sets_full_resolution_shape_5063(self, device, dtype):
+ # register() gives a Similarity the shape of dst_img before the first level. Every pyramid level spans the whole
+ # image, so that is the pixel aspect ratio of each level, which a level's own rounded shape is not
+ # (33 x 47 -> 16 x 23 -> 8 x 11). A second register() on another size replaces it.
+ if not supports_bilinear_2d_grid_sample_backward(device, dtype):
+ pytest.skip(f"torch has no {dtype} bilinear grid_sample kernel with backward on {device.type}")
+ if not supports_reflect_padding(device, dtype):
+ pytest.skip(f"torch has no {dtype} reflection padding kernel on {device.type}")
+ seen = []
+
+ def l1(a: torch.Tensor, b: torch.Tensor, reduction: str = "none") -> torch.Tensor:
+ seen.append((ir.model.height, ir.model.width))
+ return torch.nn.functional.l1_loss(a, b, reduction=reduction)
+
+ ir = ImageRegistrator("similarity", loss_fn=l1, num_iterations=1, pyramid_levels=3).to(device, dtype)
+ for height, width in ((33, 47), (47, 33)):
+ seen.clear()
+ image = torch.zeros(1, 1, height, width, device=device, dtype=dtype)
+ ir.register(image, image)
+ assert len(seen) == 6 # two losses per level
+ assert set(seen) == {(height, width)}
+
+ def test_register_homography_has_no_image_shape(self, device, dtype):
+ # register() gives only a Similarity the image shape; a Homography has no set_image_shape.
+ image = torch.zeros(1, 1, 8, 12, device=device, dtype=dtype)
+ ir = ImageRegistrator("homography", num_iterations=0, pyramid_levels=1).to(device, dtype)
+ self.assert_close(ir.register(image, image).detach(), torch.eye(3, device=device, dtype=dtype)[None])
+
+ @pytest.mark.parametrize("height, width", [(32, 48), (48, 32)])
+ def test_register_rotation_non_square_5063(self, device, dtype, height, width):
+ # dst is src rotated by angle_to_rotation_matrix(6 deg) about the image centre, in pixels. The model maps dst to
+ # src, so rot converges to -6 on a non-square image as on a square one: measured -6.00 to -6.01 in float32 and
+ # float64, -5.78 to -5.85 in float16 and bfloat16. Rotating in the anisotropic normalized frame converged to
+ # -5.27 and -5.42 in float32, and -4.99 to -5.38 in half precision.
+ if not supports_bilinear_2d_grid_sample_backward(device, dtype):
+ pytest.skip(f"torch has no {dtype} bilinear grid_sample kernel with backward on {device.type}")
+
+ def scene(degrees: float) -> torch.Tensor:
+ # smooth analytic blobs, so the fixture needs no seed; sampled at R^-1 (x - c) + c
+ ys, xs = torch.meshgrid(
+ torch.arange(height, dtype=torch.float64), torch.arange(width, dtype=torch.float64), indexing="ij"
+ )
+ cx, cy, a = (width - 1) / 2, (height - 1) / 2, math.radians(degrees)
+ xr = math.cos(a) * (xs - cx) - math.sin(a) * (ys - cy) + cx
+ yr = math.sin(a) * (xs - cx) + math.cos(a) * (ys - cy) + cy
+ img = torch.zeros(height, width, dtype=torch.float64)
+ for bx, by, s, amp in [(0.3, 0.3, 0.12, 1.0), (0.7, 0.35, 0.1, 0.8), (0.45, 0.72, 0.09, 0.9)]:
+ img = img + amp * torch.exp(
+ -(((xr - bx * width) / (s * width)) ** 2 + ((yr - by * height) / (s * height)) ** 2) / 2
+ )
+ return img[None, None].to(dtype).to(device)
+
+ ir = ImageRegistrator("rotation", lr=0.1, num_iterations=60, pyramid_levels=1, tolerance=0.0).to(device, dtype)
+ ir.register(scene(0.0), scene(6.0))
+ assert abs(ir.model.rot.item() + 6.0) < 0.4