forked from luigifreda/pyslam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
feature_root_sift.py
73 lines (61 loc) · 2.53 KB
/
feature_root_sift.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
"""
* This file is part of PYSLAM
*
* Copyright (C) 2016-present Luigi Freda <luigi dot freda at gmail dot com>
*
* PYSLAM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PYSLAM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PYSLAM. If not, see <http://www.gnu.org/licenses/>.
"""
import sys
import math
from enum import Enum
import numpy as np
import cv2
# https://www.robots.ox.ac.uk/~vgg/publications/2012/Arandjelovic12/arandjelovic12.pdf
# adapated from https://www.pyimagesearch.com/2015/04/13/implementing-rootsift-in-python-and-opencv/
class RootSIFTFeature2D:
def __init__(self, feature):
# initialize the SIFT feature detector
self.feature = feature
def detect(self, frame, mask=None):
return self.feature.detect(frame, mask)
def transform_descriptors(self, des, eps=1e-7):
# apply the Hellinger kernel by first L1-normalizing and
# taking the square-root
des /= (des.sum(axis=1, keepdims=True) + eps)
des = np.sqrt(des)
return des
def compute(self, frame, kps, eps=1e-7):
# compute SIFT descriptors
(kps, des) = self.feature.compute(frame, kps)
# if there are no keypoints or descriptors, return an empty tuple
if len(kps) == 0:
return ([], None)
# apply the Hellinger kernel by first L1-normalizing and
# taking the square-root
des = self.transform_descriptors(des)
# return a tuple of the keypoints and descriptors
return (kps, des)
# detect keypoints and their descriptors
# out: kps, des
def detectAndCompute(self, frame, mask=None):
# compute SIFT keypoints and descriptors
(kps, des) = self.feature.detectAndCompute(frame, mask)
# if there are no keypoints or descriptors, return an empty tuple
if len(kps) == 0:
return ([], None)
# apply the Hellinger kernel by first L1-normalizing and
# taking the square-root
des = self.transform_descriptors(des)
# return a tuple of the keypoints and descriptors
return (kps, des)