Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Added boundary check function to local features detector (#254)

Dmytro Mishkinc84955d786fa
diff --git a/kornia/feature/__init__.py b/kornia/feature/__init__.py
index fbee8e688..963a00005 100644
--- a/kornia/feature/__init__.py
+++ b/kornia/feature/__init__.py
@@ -14,7 +14,8 @@ from .laf import (extract_patches_from_pyramid,
make_upright,
scale_laf,
get_laf_scale,
- raise_error_if_laf_is_not_valid)
+ raise_error_if_laf_is_not_valid,
+ laf_is_inside_image)
from .siftdesc import SIFTDescriptor
from .scale_space_detector import ScaleSpaceDetector, PassLAF
from .affine_shape import LAFAffineShapeEstimator, PatchAffineShapeEstimator
@@ -45,5 +46,6 @@ __all__ = [
"PatchAffineShapeEstimator",
"LAFOrienter",
"PatchDominantGradientOrientation",
- "raise_error_if_laf_is_not_valid"
+ "raise_error_if_laf_is_not_valid",
+ "laf_is_inside_image",
]
diff --git a/kornia/feature/laf.py b/kornia/feature/laf.py
index d6a7b5a79..cd4ec24ff 100644
--- a/kornia/feature/laf.py
+++ b/kornia/feature/laf.py
@@ -381,3 +381,22 @@ def extract_patches_from_pyramid(img: torch.Tensor,
cur_img = kornia.pyrdown(cur_img)
cur_pyr_level += 1
return out
+
+
+def laf_is_inside_image(laf: torch.Tensor, images: torch.Tensor) -> torch.Tensor:
+ """Checks if the LAF is touching or partly outside the image boundary. Returns the mask
+ of LAFs, which are fully inside the image, i.e. valid.
+
+ Args:
+ laf (torch.Tensor): :math:`(B, N, 2, 3)`
+ images (torch.Tensor): images, lafs are detected in :math:`(B, CH, H, W)`
+
+ Returns:
+ mask (torch.Tensor): :math:`(B, N)`
+ """
+ raise_error_if_laf_is_not_valid(laf)
+ n, ch, h, w = images.size()
+ pts: torch.Tensor = laf_to_boundary_points(laf, 12)
+ good_lafs_mask: torch.Tensor = (pts[..., 0] >= 0) * (pts[..., 0] <= w) * (pts[..., 1] >= 0) * (pts[..., 1] <= h)
+ good_lafs_mask = good_lafs_mask.min(dim=2)[0]
+ return good_lafs_mask
diff --git a/kornia/feature/scale_space_detector.py b/kornia/feature/scale_space_detector.py
index c304d1b59..6473f7aab 100644
--- a/kornia/feature/scale_space_detector.py
+++ b/kornia/feature/scale_space_detector.py
@@ -10,7 +10,7 @@ from kornia.geometry import ConvSoftArgmax3d
from kornia.feature.orientation import PassLAF
from kornia.feature.laf import (
denormalize_laf,
- normalize_laf)
+ normalize_laf, laf_is_inside_image)
from kornia.geometry.transform import ScalePyramid
@@ -143,6 +143,11 @@ class ScaleSpaceDetector(nn.Module):
rotmat = angle_to_rotation_matrix(torch.zeros(B, N).to(max_coords_best.device).to(max_coords_best.dtype))
current_lafs = torch.cat([self.mr_size * max_coords_best[:, :, 0].view(B, N, 1, 1) * rotmat,
max_coords_best[:, :, 1:3].view(B, N, 2, 1)], dim=3)
+
+ # Zero response lafs, which touch the boundary
+ good_mask = laf_is_inside_image(current_lafs, octave[:, 0])
+ resp_flat_best = resp_flat_best * good_mask.to(resp_flat_best.device).to(resp_flat_best.dtype)
+
# Normalize LAFs
current_lafs = normalize_laf(current_lafs, octave[:, 0]) # We don`t need # of scale levels, only shape
diff --git a/test/feature/test_laf.py b/test/feature/test_laf.py
index 6a8a952ca..4aa295b5a 100644
--- a/test/feature/test_laf.py
+++ b/test/feature/test_laf.py
@@ -290,3 +290,18 @@ class TestExtractPatchesPyr:
assert gradcheck(kornia.feature.extract_patches_from_pyramid,
(img, nlaf, PS, False),
raise_exception=True)
+
+
+class TestLAFIsTouchingBoundary:
+ def test_shape(self):
+ inp = torch.rand(5, 3, 2, 3)
+ img = torch.rand(5, 3, 10, 10)
+ assert (5, 3) == kornia.feature.laf_is_inside_image(inp, img).shape
+
+ def test_touch(self):
+ w, h = 10, 5
+ img = torch.rand(1, 3, h, w)
+ laf = torch.tensor([[[[10, 0, 3], [0, 10, 3]],
+ [[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()