Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

More LAF stuff (#340)

Dmytro Mishkindcc98676b52a
diff --git a/kornia/feature/__init__.py b/kornia/feature/__init__.py
index 963a00005..c29f309fc 100644
--- a/kornia/feature/__init__.py
+++ b/kornia/feature/__init__.py
@@ -14,7 +14,10 @@ from .laf import (extract_patches_from_pyramid,
make_upright,
scale_laf,
get_laf_scale,
+ get_laf_center,
+ get_laf_orientation,
raise_error_if_laf_is_not_valid,
+ laf_from_center_scale_ori,
laf_is_inside_image)
from .siftdesc import SIFTDescriptor
from .scale_space_detector import ScaleSpaceDetector, PassLAF
@@ -38,6 +41,8 @@ __all__ = [
"ellipse_to_laf",
"make_upright",
"get_laf_scale",
+ "get_laf_center",
+ "get_laf_orientation",
"scale_laf",
"SIFTDescriptor",
"PassLAF",
@@ -48,4 +53,5 @@ __all__ = [
"PatchDominantGradientOrientation",
"raise_error_if_laf_is_not_valid",
"laf_is_inside_image",
+ "laf_from_center_scale_ori",
]
diff --git a/kornia/feature/laf.py b/kornia/feature/laf.py
index cd4ec24ff..9a8084c39 100644
--- a/kornia/feature/laf.py
+++ b/kornia/feature/laf.py
@@ -45,6 +45,84 @@ def get_laf_scale(LAF: torch.Tensor) -> torch.Tensor:
return out.abs().sqrt()
+def get_laf_center(LAF: torch.Tensor) -> torch.Tensor:
+ """Returns a center (keypoint) of the LAFs
+
+ Args:
+ LAF: (torch.Tensor): tensor [BxNx2x3].
+
+ Returns:
+ torch.Tensor: tensor BxNx2 .
+
+ Shape:
+ - Input: :math: `(B, N, 2, 3)`
+ - Output: :math: `(B, N, 2)`
+
+ Example:
+ >>> input = torch.ones(1, 5, 2, 3) # BxNx2x3
+ >>> output = kornia.get_laf_center(input) # BxNx2
+ """
+ raise_error_if_laf_is_not_valid(LAF)
+ out: torch.Tensor = LAF[..., 2]
+ return out
+
+
+def get_laf_orientation(LAF: torch.Tensor) -> torch.Tensor:
+ """Returns orientation of the LAFs, in degrees.
+
+ Args:
+ LAF: (torch.Tensor): tensor [BxNx2x3].
+
+ Returns:
+ torch.Tensor: tensor BxNx1 .
+
+ Shape:
+ - Input: :math: `(B, N, 2, 3)`
+ - Output: :math: `(B, N, 1)`
+
+ Example:
+ >>> input = torch.ones(1, 5, 2, 3) # BxNx2x3
+ >>> output = kornia.get_laf_orientation(input) # BxNx1
+ """
+ raise_error_if_laf_is_not_valid(LAF)
+ angle_rad: torch.Tensor = torch.atan2(LAF[..., 0, 1], LAF[..., 0, 0])
+ return kornia.rad2deg(angle_rad).unsqueeze(-1)
+
+
+def laf_from_center_scale_ori(xy: torch.Tensor, scale: torch.Tensor, ori: torch.Tensor) -> torch.Tensor:
+ """Returns orientation of the LAFs, in radians. Useful to create kornia LAFs from OpenCV keypoints
+
+ Args:
+ xy: (torch.Tensor): tensor [BxNx2].
+ scale: (torch.Tensor): tensor [BxNx1x1].
+ ori: (torch.Tensor): tensor [BxNx1].
+
+ Returns:
+ torch.Tensor: tensor BxNx2x3 .
+ """
+ names = ['xy', 'scale', 'ori']
+ for var_name, var, req_shape in zip(names,
+ [xy, scale, ori],
+ [("B", "N", 2), ("B", "N", 1, 1), ("B", "N", 1)]):
+ if not torch.is_tensor(var):
+ raise TypeError("{} type is not a torch.Tensor. Got {}"
+ .format(var_name, type(var)))
+ if len(var.shape) != len(req_shape): # type: ignore # because it does not like len(tensor.shape)
+ raise TypeError(
+ "{} shape should be must be [{}]. "
+ "Got {}".format(var_name, str(req_shape), var.size()))
+ for i, dim in enumerate(req_shape): # type: ignore # because it wants typing for dim
+ if dim is not int:
+ continue
+ if var.size(i) != dim:
+ "{} shape should be must be [{}]. "
+ "Got {}".format(var_name, str(req_shape), var.size())
+ unscaled_laf: torch.Tensor = torch.cat([kornia.angle_to_rotation_matrix(ori.squeeze(-1)),
+ xy.unsqueeze(-1)], dim=-1)
+ laf: torch.Tensor = scale_laf(unscaled_laf, scale)
+ return laf
+
+
def scale_laf(laf: torch.Tensor, scale_coef: Union[float, torch.Tensor]) -> torch.Tensor:
"""
Multiplies region part of LAF ([:, :, :2, :2]) by a scale_coefficient.
diff --git a/test/feature/test_laf.py b/test/feature/test_laf.py
index 4aa295b5a..09ee3d859 100644
--- a/test/feature/test_laf.py
+++ b/test/feature/test_laf.py
@@ -53,6 +53,50 @@ class TestGetLAFScale:
raise_exception=True)
+class TestGetLAFCenter:
+ def test_shape(self):
+ inp = torch.ones(1, 3, 2, 3)
+ xy = kornia.feature.get_laf_center(inp)
+ assert xy.shape == (1, 3, 2)
+
+ def test_center(self):
+ inp = torch.tensor([[5., 1, 2], [1, 1, 3]]).float()
+ inp = inp.view(1, 1, 2, 3)
+ expected = torch.tensor([[[2, 3]]]).float()
+ xy = kornia.feature.get_laf_center(inp)
+ assert_allclose(xy, expected)
+
+ def test_gradcheck(self):
+ batch_size, channels, height, width = 1, 2, 2, 3
+ img = torch.rand(batch_size, channels, height, width)
+ img = utils.tensor_to_gradcheck_var(img) # to var
+ assert gradcheck(kornia.feature.get_laf_center,
+ (img,),
+ raise_exception=True)
+
+
+class TestGetLAFOri:
+ def test_shape(self):
+ inp = torch.ones(1, 3, 2, 3)
+ ori = kornia.feature.get_laf_orientation(inp)
+ assert ori.shape == (1, 3, 1)
+
+ def test_ori(self):
+ inp = torch.tensor([[1, 1, 2], [1, 1, 3]]).float()
+ inp = inp.view(1, 1, 2, 3)
+ expected = torch.tensor([[[45.]]]).float()
+ angle = kornia.feature.get_laf_orientation(inp)
+ assert_allclose(angle, expected)
+
+ def test_gradcheck(self):
+ batch_size, channels, height, width = 1, 2, 2, 3
+ img = torch.rand(batch_size, channels, height, width)
+ img = utils.tensor_to_gradcheck_var(img) # to var
+ assert gradcheck(kornia.feature.get_laf_orientation,
+ (img,),
+ raise_exception=True)
+
+
class TestScaleLAF:
def test_shape_float(self):
inp = torch.ones(7, 3, 2, 3).float()
@@ -305,3 +349,42 @@ class TestLAFIsTouchingBoundary:
[[1, 0, 5], [0, 1, 2]]]]).float()
expected = torch.tensor([[False, True]])
assert torch.all(kornia.feature.laf_is_inside_image(laf, img) == expected).item()
+
+
+class TestGetCreateLAF:
+ def test_shape(self):
+ xy = torch.ones(1, 3, 2)
+ ori = torch.ones(1, 3, 1)
+ scale = torch.ones(1, 3, 1, 1)
+ laf = kornia.feature.laf_from_center_scale_ori(xy, scale, ori)
+ assert laf.shape == (1, 3, 2, 3)
+
+ def test_laf(self):
+ xy = torch.ones(1, 1, 2)
+ ori = torch.zeros(1, 1, 1)
+ scale = 5 * torch.ones(1, 1, 1, 1)
+ expected = torch.tensor([[[[5, 0, 1], [0, 5, 1]]]]).float()
+ laf = kornia.feature.laf_from_center_scale_ori(xy, scale, ori)
+ assert_allclose(laf, expected)
+
+ def test_cross_consistency(self):
+ batch_size, channels = 3, 2
+ xy = torch.rand(batch_size, channels, 2)
+ ori = torch.rand(batch_size, channels, 1)
+ scale = torch.abs(torch.rand(batch_size, channels, 1, 1))
+ laf = kornia.feature.laf_from_center_scale_ori(xy, scale, ori)
+ scale2 = kornia.feature.get_laf_scale(laf)
+ assert_allclose(scale, scale2)
+ xy2 = kornia.feature.get_laf_center(laf)
+ assert_allclose(xy2, xy)
+ ori2 = kornia.feature.get_laf_orientation(laf)
+ assert_allclose(ori2, ori)
+
+ def test_gradcheck(self):
+ batch_size, channels = 3, 2
+ xy = utils.tensor_to_gradcheck_var(torch.rand(batch_size, channels, 2))
+ ori = utils.tensor_to_gradcheck_var(torch.rand(batch_size, channels, 1))
+ scale = utils.tensor_to_gradcheck_var(torch.abs(torch.rand(batch_size, channels, 1, 1)))
+ assert gradcheck(kornia.feature.laf_from_center_scale_ori,
+ (xy, scale, ori,),
+ raise_exception=True)