Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

moved angle-to-rot-matrix

Dmytro Mishkinb4d4f5a2c5c3
diff --git a/kornia/feature/__init__.py b/kornia/feature/__init__.py
index b92342691..e82a14d72 100644
--- a/kornia/feature/__init__.py
+++ b/kornia/feature/__init__.py
@@ -8,8 +8,8 @@ from .laf import (extract_patches_from_pyramid,
LAF2pts,
ell2LAF,
make_upright,
- get_laf_scale,
- angle_to_rotation_matrix)
+ get_laf_scale)
+from kornia.geometry.transform.imgwarp import angle_to_rotation_matrix
from .siftdesc import SIFTDescriptor
__all__ = [
"non_maxima_suppression2d",
diff --git a/kornia/feature/laf.py b/kornia/feature/laf.py
index 3cf404305..2f880d50d 100644
--- a/kornia/feature/laf.py
+++ b/kornia/feature/laf.py
@@ -1,13 +1,9 @@
# flake8: noqa E127
# flake8: noqa E128
-from typing import Tuple
import kornia
-import torch
import math
-import numpy as np
import torch
-import torch.nn as nn
import torch.nn.functional as F
import warnings
try:
@@ -16,37 +12,6 @@ try:
except:
warnings.warn("no matplotlib found, visualize_LAF not available ")
MATPLOTLIB = False
-
-
-def angle_to_rotation_matrix(angle: torch.Tensor,
- do_deg2rad: bool = False) -> torch.Tensor:
- """
- Creates a rotation matrix out of angles
- Args:
- angle: (torch.Tensor): tensor of angles, any shape.
- do_deg2rad: (bool): if we should convert to radians first
-
- Returns:
- torch.Tensor: tensor of Nx2x2 rotation matrices.
-
- Shape:
- - Input: :math:`(N)`
- - Output: :math:`(N, 2, 2)`
-
- Example:
- >>> input = torch.rand(1, 3) # Nx3
- >>> output = kornia.angle_to_rotation_matrix(input) # Nx3x2x2
- """
- if do_deg2rad:
- ang = kornia.deg2rad(angle)
- else:
- ang = angle
- n_dims = len(ang.size())
- cos_a = torch.cos(ang).unsqueeze(-1).unsqueeze(-1)
- sin_a = torch.sin(ang).unsqueeze(-1).unsqueeze(-1)
- A1_ang = torch.cat([ cos_a, sin_a], dim=n_dims + 1)
- A2_ang = torch.cat([-sin_a, cos_a], dim=n_dims + 1)
- return torch.cat([A1_ang, A2_ang], dim=n_dims)
def get_laf_scale(A: torch.Tensor) -> torch.Tensor:
diff --git a/kornia/geometry/transform/imgwarp.py b/kornia/geometry/transform/imgwarp.py
index b74213da4..984701a3c 100644
--- a/kornia/geometry/transform/imgwarp.py
+++ b/kornia/geometry/transform/imgwarp.py
@@ -3,6 +3,7 @@ from typing import Tuple, Optional
import torch
import torch.nn.functional as F
+import kornia
from kornia.geometry.warp.homography_warper import homography_warp
# TODO: move to utils or conversions
from kornia.geometry.conversions import (
@@ -268,6 +269,37 @@ def get_perspective_transform(src, dst):
return M.view(-1, 3, 3) # Bx3x3
+def angle_to_rotation_matrix(angle: torch.Tensor,
+ do_deg2rad: bool = False) -> torch.Tensor:
+ """
+ Creates a rotation matrix out of angles
+ Args:
+ angle: (torch.Tensor): tensor of angles, any shape.
+ do_deg2rad: (bool): if we should convert to radians first
+
+ Returns:
+ torch.Tensor: tensor of Nx2x2 rotation matrices.
+
+ Shape:
+ - Input: :math:`(N)`
+ - Output: :math:`(N, 2, 2)`
+
+ Example:
+ >>> input = torch.rand(1, 3) # Nx3
+ >>> output = kornia.angle_to_rotation_matrix(input) # Nx3x2x2
+ """
+ if do_deg2rad:
+ ang = kornia.deg2rad(angle)
+ else:
+ ang = angle
+ n_dims = len(ang.size())
+ cos_a = torch.cos(ang).unsqueeze(-1).unsqueeze(-1)
+ sin_a = torch.sin(ang).unsqueeze(-1).unsqueeze(-1)
+ A1_ang = torch.cat([cos_a, sin_a], dim=n_dims + 1)
+ A2_ang = torch.cat([-sin_a, cos_a], dim=n_dims + 1)
+ return torch.cat([A1_ang, A2_ang], dim=n_dims)
+
+
def get_rotation_matrix2d(
center: torch.Tensor,
angle: torch.Tensor,
@@ -338,8 +370,10 @@ def get_rotation_matrix2d(
.format(center.shape, angle.shape, scale.shape))
# convert angle and apply scale
angle_rad: torch.Tensor = deg2rad(angle)
- alpha: torch.Tensor = torch.cos(angle_rad) * scale
- beta: torch.Tensor = torch.sin(angle_rad) * scale
+
+ scaled_rotation: torch.Tensor = angle_to_rotation_matrix(angle_rad) * scale.view(-1, 1, 1)
+ alpha: torch.Tensor = scaled_rotation[:, 0, 0]
+ beta: torch.Tensor = scaled_rotation[:, 0, 1]
# unpack the center to x, y coordinates
x: torch.Tensor = center[..., 0]
@@ -349,11 +383,8 @@ def get_rotation_matrix2d(
batch_size: int = center.shape[0]
M: torch.Tensor = torch.zeros(
batch_size, 2, 3, device=center.device, dtype=center.dtype)
- M[..., 0, 0] = alpha
- M[..., 0, 1] = beta
+ M[..., 0:2, 0:2] = scaled_rotation
M[..., 0, 2] = (torch.tensor(1.) - alpha) * x - beta * y
- M[..., 1, 0] = -beta
- M[..., 1, 1] = alpha
M[..., 1, 2] = beta * x + (torch.tensor(1.) - alpha) * y
return M
diff --git a/test/feature/test_laf.py b/test/feature/test_laf.py
index 3252e972c..4f9085d7a 100644
--- a/test/feature/test_laf.py
+++ b/test/feature/test_laf.py
@@ -1,6 +1,7 @@
import pytest
import kornia as kornia
+import kornia.geometry.transform.imgwarp
import kornia.testing as utils # test utils
import torch
@@ -12,7 +13,7 @@ import math
class TestAngleToRotationMatrix:
def test_shape(self):
inp = torch.ones(1, 3, 4, 4)
- rotmat = kornia.feature.angle_to_rotation_matrix(inp)
+ rotmat = kornia.geometry.transform.imgwarp.angle_to_rotation_matrix(inp)
assert rotmat.shape == (1, 3, 4, 4, 2, 2)
def test_angles(self):
@@ -20,14 +21,14 @@ class TestAngleToRotationMatrix:
expected = torch.tensor([[[1.0, 0.], [0., 1.0]],
[[0, 1.0], [-1.0, 0]]])
- rotmat = kornia.feature.angle_to_rotation_matrix(inp)
+ rotmat = kornia.geometry.transform.imgwarp.angle_to_rotation_matrix(inp)
assert_allclose(rotmat, expected)
def test_gradcheck(self):
batch_size, channels, height, width = 1, 2, 5, 4
img = torch.rand(batch_size, channels, height, width)
img = utils.tensor_to_gradcheck_var(img) # to var
- assert gradcheck(kornia.feature.angle_to_rotation_matrix,
+ assert gradcheck(kornia.geometry.transform.imgwarp.angle_to_rotation_matrix,
(img,),
raise_exception=True)