ImageRegistrator's 'similarity' and 'rotation' models rotate in the anisotropic normalized frame, so on a non-square image they cannot register a rotated image (32x48: residual 12x the square case)
Similarity.forward() (kornia/geometry/transform/image_registrator.py) builds scale * R(rot) with R = angle_to_rotation_matrix(rot) directly in ImageRegistrator's normalized [-1, 1] frame. On an H × W image with H ≠ W, one normalized unit along x is W / 2 pixels and along y H / 2 pixels. A rotation of the content in pixels therefore becomes [[c, s·H/W], [−s·W/H, c]] in that frame, which is not a rotation. The 'rotation' and 'similarity' models cannot represent it, so on a non-square image they cannot register a rotated image. They converge to a compromise angle with an order-of-magnitude larger residual. A square image works.
import math, torch
from kornia.geometry.transform import ImageRegistrator
def scene(H, W, angle_deg):
# smooth blobs; the content is rotated by angle_deg (in pixels, y down) about the image centre
ys, xs = torch.meshgrid(torch.arange(H, dtype=torch.float64), torch.arange(W, dtype=torch.float64), indexing="ij")
cx, cy, a = (W - 1) / 2, (H - 1) / 2, math.radians(angle_deg)
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(H, W, 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), (0.2, 0.8, 0.07, 0.6)]:
img = img + amp * torch.exp(-(((xr - bx * W) / (s * W)) ** 2 + ((yr - by * H) / (s * H)) ** 2) / 2)
return img[None, None].float()
for H, W in [(40, 40), (32, 48), (48, 32)]:
src, dst = scene(H, W, 0.0), scene(H, W, 6.0)
ir = ImageRegistrator("rotation", lr=1e-2, num_iterations=400, pyramid_levels=2, tolerance=1e-12)
ir.register(src, dst)
res = (ir.warp_src_into_dst(src) - dst)[..., 4:-4, 4:-4].abs().mean().item()
print(f"{H}x{W}: rot {float(ir.model.rot):+.3f} deg, interior residual {res:.4f}")40x40: rot +6.000 deg, interior residual 0.0020
32x48: rot +6.062 deg, interior residual 0.0242
48x32: rot +5.786 deg, interior residual 0.0236kornia main 78cd06c35, torch 2.14.0, CPU. With 'homography' (same scene and settings) the 32×48 interior residual is 0.0025, close to the square case, because the homography family contains the conjugated matrix. For 6° on 32×48 the dst→src matrix is [[0.9945, 0.0697], [−0.1568, 0.9945]] in normalized coordinates.
Classification
Kornia defect: the 'similarity' / 'rotation' model family is not the similarity group of the image unless H == W, and rot is not the rotation angle in pixels. The shift part is fine: translations stay translations under the anisotropic scaling, so 'translation' is unaffected. The normalized units of the shift were discussed in #2989 (closed); that is a separate matter.
Disposition
Build R in an isotropic frame: conjugate with the aspect scaling, diag(W/H, 1) · R · diag(H/W, 1) in the normalized frame, or equivalently rotate in pixels and normalize afterwards. The model must then know (H, W), e.g. from the warper or from register. This changes the meaning of rot and scale for non-square images, so it needs a breaking changelog fragment. The batch-10 convention docstring for Similarity will name this defect, with a wart pin on the 32×48 case.
Posted on behalf of @ducha-aiki by Claude (Opus 5.5).
Source: kornia/kornia