Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

add sub-bin precision to improve local feature orientation (#2310)

Dmytro Mishkin5d02fee4aa40
diff --git a/kornia/feature/orientation.py b/kornia/feature/orientation.py
index a4b0f5e56..ad0ac6f5b 100644
--- a/kornia/feature/orientation.py
+++ b/kornia/feature/orientation.py
@@ -1,4 +1,3 @@
-import math
from typing import Dict, Optional
import torch
@@ -7,7 +6,7 @@ import torch.nn.functional as F
from kornia.constants import pi
from kornia.core.check import KORNIA_CHECK_LAF, KORNIA_CHECK_SHAPE
-from kornia.filters import SpatialGradient, get_gaussian_kernel2d
+from kornia.filters import SpatialGradient, get_gaussian_discrete_kernel1d, get_gaussian_kernel2d
from kornia.geometry import rad2deg
from kornia.utils.helpers import map_location_to_cpu
@@ -49,10 +48,10 @@ class PatchDominantGradientOrientation(nn.Module):
self.num_ang_bins = num_angular_bins
self.gradient = SpatialGradient('sobel', 1)
self.eps = eps
- self.angular_smooth = nn.Conv1d(1, 1, kernel_size=3, padding=1, bias=False, padding_mode="circular")
+ self.angular_smooth = nn.Conv1d(1, 1, kernel_size=5, padding=2, bias=False, padding_mode="circular")
with torch.no_grad():
- self.angular_smooth.weight[:] = torch.tensor([[[0.33, 0.34, 0.33]]])
- sigma: float = float(self.patch_size) / math.sqrt(2.0)
+ self.angular_smooth.weight[:] = get_gaussian_discrete_kernel1d(5, 1.6)
+ sigma: float = float(self.patch_size) / 6.0
self.weighting = get_gaussian_kernel2d((self.patch_size, self.patch_size), (sigma, sigma), True)
def __repr__(self):
@@ -85,7 +84,7 @@ class PatchDominantGradientOrientation(nn.Module):
gx: torch.Tensor = grads[:, :, 0]
gy: torch.Tensor = grads[:, :, 1]
- mag: torch.Tensor = torch.sqrt(gx * gx + gy * gy + self.eps)
+ mag: torch.Tensor = torch.sqrt(gx * gx + gy * gy + self.eps) * self.weighting
ori: torch.Tensor = torch.atan2(gy, gx + self.eps) + 2.0 * pi
o_big = float(self.num_ang_bins) * (ori + 1.0 * pi) / (2.0 * pi)
@@ -102,9 +101,15 @@ class PatchDominantGradientOrientation(nn.Module):
)
ang_bins_list.append(ang_bins_i)
ang_bins = torch.cat(ang_bins_list, 1).view(-1, 1, self.num_ang_bins)
- ang_bins = self.angular_smooth(ang_bins)
- values, indices = ang_bins.view(-1, self.num_ang_bins).max(1)
- angle = -((2.0 * pi * indices.to(patch.dtype) / float(self.num_ang_bins)) - pi)
+ ang_bins = self.angular_smooth(ang_bins).view(-1, self.num_ang_bins)
+ values, indices = ang_bins.max(1)
+ indices_left = (self.num_ang_bins + indices - 1) % self.num_ang_bins
+ indices_right = (indices + 1) % self.num_ang_bins
+ left = torch.gather(ang_bins, 1, indices_left.reshape(-1, 1)).reshape(-1)
+ center = values
+ right = torch.gather(ang_bins, 1, indices_right.reshape(-1, 1)).reshape(-1)
+ c_subpix = 0.5 * (left - right) / (left + right - 2.0 * center)
+ angle = -((2.0 * pi * (indices.to(patch.dtype) + c_subpix) / float(self.num_ang_bins)) - pi)
return angle
diff --git a/test/feature/test_local_features_orientation.py b/test/feature/test_local_features_orientation.py
index f63bc2998..f57ff73db 100644
--- a/test/feature/test_local_features_orientation.py
+++ b/test/feature/test_local_features_orientation.py
@@ -154,7 +154,7 @@ class TestLAFOrienter:
def test_toy(self, device):
ori = LAFOrienter(32).to(device)
inp = torch.zeros(1, 1, 19, 19, device=device)
- inp[:, :, 5:, :10] = 1
+ inp[:, :, :, :10] = 1
laf = torch.tensor([[[[5.0, 0.0, 8.0], [0.0, 5.0, 8.0]]]], device=device)
new_laf = ori(laf, inp)
expected = torch.tensor([[[[-5.0, 0.0, 8.0], [0.0, -5.0, 8.0]]]], device=device)