Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(geometry): keep the center fixed in get_shear_matrix3d for combined shears (#6315)

Debanjan Sarkar6770e7d9a375
diff --git a/changelog.d/6315.fixed.md b/changelog.d/6315.fixed.md
new file mode 100644
index 000000000..0f047f071
--- /dev/null
+++ b/changelog.d/6315.fixed.md
@@ -0,0 +1 @@
+`kornia.geometry.transform.get_shear_matrix3d` now keeps `center` fixed for every combination of shears. Before, two or more shears that meet in a product term (for example `syx` with `szy`) moved the center by up to several pixels, so `get_affine_matrix3d` and a shear-only `RandomAffine3D` also translated the volume. Single shears and shears about the origin are unchanged.
diff --git a/kornia/geometry/transform/imgwarp.py b/kornia/geometry/transform/imgwarp.py
index 9400f88cc..1ad55355a 100644
--- a/kornia/geometry/transform/imgwarp.py
+++ b/kornia/geometry/transform/imgwarp.py
@@ -1363,9 +1363,9 @@ def get_shear_matrix3d(
.. math::
\begin{bmatrix}
- 1 & o & r & oy + rz \\
- m & p & s & mx + py + sz -y \\
- n & q & t & nx + qy + tz -z \\
+ 1 & o & r & x - (x + oy + rz) \\
+ m & p & s & y - (mx + py + sz) \\
+ n & q & t & z - (nx + qy + tz) \\
0 & 0 & 0 & 1 \\
\end{bmatrix}
Where:
@@ -1378,9 +1378,11 @@ def get_shear_matrix3d(
s = S_{xy}S_{zx} + (S_{xy}S_{yx} + 1)S_{zy}
t = S_{xz}S_{zx} + (S_{xz}S_{yx} + S_{yz})S_{zy} + 1
+ Here :math:`S_{ab} = -\tan(s_{ab})`, and :math:`(x, y, z)` is ``center``.
+
Convention:
- ``center`` is ``(x, y, z)`` in pixels, origin at the top-left of the first depth
- slice (``z = 0``)
+ slice (``z = 0``); it is a fixed point of the returned matrix for every combination of shears
- returns :math:`(B, 4, 4)` affine matrix in pixel coordinates
Args:
@@ -1428,21 +1430,17 @@ def get_shear_matrix3d(
szx_tan = torch.tan(szx)
szy_tan = torch.tan(szy)
- # compute translation matrix
- m00, m10, m20, m01, m11, m21, m02, m12, m22 = _compute_shear_matrix_3d(
- sxy_tan, sxz_tan, syx_tan, syz_tan, szx_tan, szy_tan
- )
-
- m03 = m01 * y + m02 * z
- m13 = m10 * x + m11 * y + m12 * z - y
- m23 = m20 * x + m21 * y + m22 * z - z
-
# shear matrix is implemented with negative values
sxy_tan, sxz_tan, syx_tan, syz_tan, szx_tan, szy_tan = -sxy_tan, -sxz_tan, -syx_tan, -syz_tan, -szx_tan, -szy_tan
m00, m10, m20, m01, m11, m21, m02, m12, m22 = _compute_shear_matrix_3d(
sxy_tan, sxz_tan, syx_tan, syz_tan, szx_tan, szy_tan
)
+ # translation t = c - A c from the returned linear part A, so that the center is a fixed point
+ m03 = x - (m00 * x + m01 * y + m02 * z)
+ m13 = y - (m10 * x + m11 * y + m12 * z)
+ m23 = z - (m20 * x + m21 * y + m22 * z)
+
shear_mat = torch.stack([m00, m01, m02, m03, m10, m11, m12, m13, m20, m21, m22, m23], -1).view(-1, 3, 4)
return convert_affinematrix_to_homography3d(shear_mat)
diff --git a/tests/augmentation/_3d/test_random_affine_3d.py b/tests/augmentation/_3d/test_random_affine_3d.py
index 41e1a42c2..d31e456c0 100644
--- a/tests/augmentation/_3d/test_random_affine_3d.py
+++ b/tests/augmentation/_3d/test_random_affine_3d.py
@@ -50,3 +50,13 @@ class TestRandomAffine3D(BaseTester):
self.assert_close(f(tensor), expected)
self.assert_close(f.transform_matrix, expected_transform)
+
+ def test_shear_only_keeps_center(self, device, dtype):
+ # A shear-only transform must not translate: the matrix maps its own center to itself (#6302).
+ torch.manual_seed(0)
+ aug = RandomAffine3D(degrees=0.0, shears=20.0, p=1.0)
+ aug(torch.rand(8, 1, 9, 17, 33, device=device, dtype=dtype))
+ center = aug._params["center"].to(device=device, dtype=dtype)
+ matrix = aug.transform_matrix
+ moved = (matrix[:, :3, :3] @ center[..., None])[..., 0] + matrix[:, :3, 3]
+ self.assert_close(moved, center)
diff --git a/tests/geometry/transform/test_affine.py b/tests/geometry/transform/test_affine.py
index c6ff1d1ab..c9294a5cc 100644
--- a/tests/geometry/transform/test_affine.py
+++ b/tests/geometry/transform/test_affine.py
@@ -15,6 +15,7 @@
# limitations under the License.
#
+import itertools
import math
import numpy as np
@@ -1356,6 +1357,22 @@ class TestGetShearMatrix(BaseTester):
assert out.device.type == center.device.type, "Output device must match center device"
assert out.dtype == center.dtype, "Output dtype must match center dtype"
+ def test_get_shear_matrix3d_keeps_center_for_every_shear_subset(self, device, dtype):
+ # The translation used to come from the positive-tangent matrix while the returned linear part uses the
+ # negated tangents, so shears that meet in a product term moved the center (#6302). The center is asymmetric
+ # so that no coordinate can stand in for another.
+ names = ("sxy", "sxz", "syx", "syz", "szx", "szy")
+ values = (0.3, -0.25, 0.2, 0.15, -0.35, 0.1)
+ center = torch.tensor([[7.0, 23.0, 41.0]], device=device, dtype=dtype)
+ center_h = torch.tensor([7.0, 23.0, 41.0, 1.0], device=device, dtype=dtype)
+ # bfloat16 rounds near 41 in steps of 0.25, while the smallest displacement the bug caused here is 1.24 px.
+ tol = 0.5 if dtype in (torch.float16, torch.bfloat16) else None
+ for size in range(1, 7):
+ for subset in itertools.combinations(range(6), size):
+ shears = {names[i]: torch.tensor([values[i]], device=device, dtype=dtype) for i in subset}
+ out = kornia.geometry.transform.get_shear_matrix3d(center, **shears)
+ self.assert_close(out[0] @ center_h, center_h, rtol=tol and 0.0, atol=tol)
+
class TestTransformModuleBuffers(BaseTester):
@pytest.mark.parametrize(