-
Notifications
You must be signed in to change notification settings - Fork 336
/
feature_orbslam2.py
62 lines (50 loc) · 2.44 KB
/
feature_orbslam2.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
"""
* 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 os
import cv2
import numpy as np
from utils_sys import Printer,getchar
from orbslam2_features import ORBextractor
kVerbose = True
# interface for pySLAM
class Orbslam2Feature2D:
def __init__(self,num_features=2000, scale_factor=1.2, num_levels=8):
print('Using Orbslam2Feature2D')
self.orb_extractor = ORBextractor(num_features, scale_factor, num_levels)
# extract keypoints
def detect(self, img, mask=None): #mask is fake: it is not considered by the c++ implementation
# detect and compute
kps_tuples = self.orb_extractor.detect(img)
# convert keypoints
kps = [cv2.KeyPoint(*kp) for kp in kps_tuples]
return kps
def compute(self, img, kps, mask=None):
Printer.orange('WARNING: you are supposed to call detectAndCompute() for ORB2 instead of compute()')
Printer.orange('WARNING: ORB2 is recomputing both kps and des on input frame', img.shape)
return self.detectAndCompute(img)
#return kps, np.array([])
def setMaxFeatures(self, num_features): # use the cv2 method name for extractors (see https://docs.opencv.org/4.x/db/d95/classcv_1_1ORB.html#aca471cb82c03b14d3e824e4dcccf90b7)
self.orb_extractor.SetNumFeatures(num_features) # custom method name for OrbSlam2 python wrapper
# compute both keypoints and descriptors
def detectAndCompute(self, img, mask=None): #mask is fake: it is not considered by the c++ implementation
# detect and compute
kps_tuples, des = self.orb_extractor.detectAndCompute(img)
# convert keypoints
kps = [cv2.KeyPoint(*kp) for kp in kps_tuples]
return kps, des