Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

:bug: Fix anisotropic scale error (#1340)

Dmytro Mishkin6f9a477a1515
diff --git a/kornia/augmentation/augmentation.py b/kornia/augmentation/augmentation.py
index 7b955f2ce..caeae38ac 100644
--- a/kornia/augmentation/augmentation.py
+++ b/kornia/augmentation/augmentation.py
@@ -892,17 +892,19 @@ class RandomRotation(GeometricAugmentationBase2D):
... [0., 0., 1., 2.]])
>>> aug = RandomRotation(degrees=45.0, return_transform=True, p=1.)
>>> out = aug(input)
- >>> out
- (tensor([[[[0.9824, 0.0088, 0.0000, 1.9649],
+ >>> out[0]
+ tensor([[[[0.9824, 0.0088, 0.0000, 1.9649],
[0.0000, 0.0029, 0.0000, 0.0176],
[0.0029, 1.0000, 1.9883, 0.0000],
- [0.0000, 0.0088, 1.0117, 1.9649]]]]), tensor([[[ 1.0000, -0.0059, 0.0088],
+ [0.0000, 0.0088, 1.0117, 1.9649]]]])
+ >>> out[1]
+ tensor([[[ 1.0000, -0.0059, 0.0088],
[ 0.0059, 1.0000, -0.0088],
- [ 0.0000, 0.0000, 1.0000]]]))
+ [ 0.0000, 0.0000, 1.0000]]])
>>> aug.inverse(out)
- tensor([[[[9.6526e-01, 8.6823e-03, 1.7263e-02, 1.9305e+00],
+ tensor([[[[9.6526e-01, 8.6824e-03, 1.7263e-02, 1.9305e+00],
[8.6398e-03, 2.9485e-03, 5.8971e-03, 1.7365e-02],
- [2.9055e-03, 9.9416e-01, 1.9825e+00, 2.3134e-02],
+ [2.9054e-03, 9.9416e-01, 1.9825e+00, 2.3134e-02],
[2.5777e-05, 1.1640e-02, 9.9992e-01, 1.9392e+00]]]])
"""
diff --git a/kornia/geometry/transform/imgwarp.py b/kornia/geometry/transform/imgwarp.py
index c7f1d9abf..fbf783147 100644
--- a/kornia/geometry/transform/imgwarp.py
+++ b/kornia/geometry/transform/imgwarp.py
@@ -1,4 +1,3 @@
-import warnings
from typing import Optional, Tuple
import torch
@@ -10,6 +9,7 @@ from kornia.geometry.conversions import (
deg2rad,
normalize_pixel_coordinates,
)
+from kornia.geometry.epipolar import eye_like
from kornia.geometry.linalg import transform_points
from kornia.geometry.transform.homography_warper import normalize_homography
from kornia.geometry.transform.projwarp import get_projective_transform
@@ -380,30 +380,21 @@ def get_rotation_matrix2d(center: torch.Tensor, angle: torch.Tensor, scale: torc
)
)
- # convert angle and apply scale
- rotation_matrix: torch.Tensor = angle_to_rotation_matrix(angle)
- scaling_matrix: torch.Tensor = torch.eye(2, device=rotation_matrix.device, dtype=rotation_matrix.dtype).repeat(
- rotation_matrix.size(0), 1, 1
- )
+ shift_m = eye_like(3, center)
+ shift_m[:, :2, 2] = center
- scaling_matrix = scaling_matrix * scale.unsqueeze(dim=2).repeat(1, 1, 2)
- scaled_rotation: torch.Tensor = rotation_matrix @ scaling_matrix
- alpha: torch.Tensor = scaled_rotation[:, 0, 0]
- beta: torch.Tensor = scaled_rotation[:, 0, 1]
+ shift_m_inv = eye_like(3, center)
+ shift_m_inv[:, :2, 2] = -center
- # unpack the center to x, y coordinates
- x: torch.Tensor = center[..., 0]
- y: torch.Tensor = center[..., 1]
+ scale_m = eye_like(3, center)
+ scale_m[:, 0, 0] *= scale[:, 0]
+ scale_m[:, 1, 1] *= scale[:, 1]
- # create output tensor
- batch_size: int = center.shape[0]
- one = torch.tensor(1.0, device=center.device, dtype=center.dtype)
- M: torch.Tensor = torch.zeros(batch_size, 2, 3, device=center.device, dtype=center.dtype)
+ rotat_m = eye_like(3, center)
+ rotat_m[:, :2, :2] = angle_to_rotation_matrix(angle)
- M[..., 0:2, 0:2] = scaled_rotation
- M[..., 0, 2] = (one - alpha) * x - beta * y
- M[..., 1, 2] = beta * x + (one - alpha) * y
- return M
+ affine_m = shift_m @ rotat_m @ scale_m @ shift_m_inv
+ return affine_m[:, :2, :] # Bx2x3
def remap(
diff --git a/test/geometry/transform/test_affine.py b/test/geometry/transform/test_affine.py
index b0032b3bd..b2cd97dbc 100644
--- a/test/geometry/transform/test_affine.py
+++ b/test/geometry/transform/test_affine.py
@@ -698,3 +698,23 @@ class TestAffine2d:
)
matrix_kornia = matrix_kornia.inverse()[0, :2].detach().cpu()
assert_close(matrix_kornia, matrix_expected, atol=1e-4, rtol=1e-4)
+
+
+class TestGetAffineMatrix:
+ def test_smoke(self, device, dtype):
+ H, W = 5, 5
+ translation = torch.tensor([[0., 0.]], device=device, dtype=dtype)
+ # NOTE: ideally the center should be [W * 0.5, H * 0.5]
+ center = torch.tensor([[W // 2, H // 2]], device=device, dtype=dtype)
+ zoom1 = torch.ones([1, 1], device=device, dtype=dtype) * 0.5
+ zoom2 = torch.ones([1, 1], device=device, dtype=dtype) * 1.0
+ zoom = torch.cat([zoom1, zoom2], -1)
+ angle = torch.zeros([1], device=device, dtype=dtype)
+ affine_mat = kornia.geometry.get_affine_matrix2d(translation, center, zoom, angle)
+
+ img = torch.ones(1, 1, H, W, device=device, dtype=dtype)
+ expected = torch.zeros_like(img)
+ expected[..., 1:4] = 1.
+
+ out = kornia.geometry.warp_affine(img, affine_mat[:, :2], (H, W))
+ assert_close(out, expected)