-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdetector.py
200 lines (146 loc) · 5.23 KB
/
detector.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import cv2
import numpy as np
class DetectorDescriptorInterface:
def __init__(self, des_extractor, as_extractor):
self.detector = None
self.des_extractor = des_extractor
self.as_extractor = as_extractor
self.des = None
def get_keypoints(self, frame):
"""
Takes in a frame and detects key points
:param frame: frame from which to detect key points from
:return: frame location of detected points, shape: (numberOfKeypoints, 2)
"""
pass
def get_descriptors(self, frame, kp):
"""
If applicable for this detector, outputs the descriptors for each detected key point.
Otherwise, return None
:param frame: frame from which to detect key points from
:param kp: key points to compute descritors frome
:return: descriptors of detected points, shape: (numberOfKeypoints, descriptorDimension)
"""
pass
def set_extractor(self, extractor):
self.des_extractor = extractor
class ShiTomasiDetector(DetectorDescriptorInterface):
def __init__(self, des_extractor=None, **params):
super().__init__(des_extractor, as_extractor=False)
self.detector = cv2.GFTTDetector_create(**params)
def get_keypoints(self, frame):
kp = self.detector.detect(frame)
return kp
def get_descriptors(self, frame, kp):
if self.des_extractor is None:
raise NotImplementedError("No descriptor extractor specified.")
return self.des_extractor.get_descriptors(frame, kp)
def __str__(self):
return "Shi-Tomasi"
class FAST_Detector(DetectorDescriptorInterface):
def __init__(self, des_extractor=None, **params):
super().__init__(des_extractor, as_extractor=False)
self.detector = cv2.FastFeatureDetector_create(**params)
def get_keypoints(self, frame):
kp = self.detector.detect(frame)
return kp
def get_descriptors(self, frame, kp):
if self.des_extractor is None:
raise NotImplementedError("No descriptor extractor specified.")
return self.des_extractor.get_descriptors(frame, kp)
def __str__(self):
return "FAST"
class CenSurE_Detector(DetectorDescriptorInterface):
def __init__(self, des_extractor=None, **params):
super().__init__(des_extractor, as_extractor=False)
self.detector = cv2.xfeatures2d.StarDetector_create(**params)
def get_keypoints(self, frame):
kp = self.detector.detect(frame, None)
return kp
def get_descriptors(self, frame, kp):
if self.des_extractor is None:
raise NotImplementedError("No descriptor extractor specified.")
return self.des_extractor.get_descriptors(frame, kp)
def __str__(self):
return "CenSurE"
class SIFT(DetectorDescriptorInterface):
def __init__(self, des_extractor=None, as_extractor=False, **params):
super().__init__(des_extractor, as_extractor)
self.detector = cv2.SIFT_create(**params)
def get_keypoints(self, frame):
kp, des = self.detector.detectAndCompute(frame, None)
self.des = des
return kp
def get_descriptors(self, frame, kp):
if self.des_extractor is None:
if self.as_extractor:
kp, des = self.detector.compute(frame, kp)
return kp, des
else:
return kp, self.des
return self.des_extractor.get_descriptors(frame, kp)
def __str__(self):
return "SIFT"
class SURF(DetectorDescriptorInterface):
def __init__(self, des_extractor=None, as_extractor=False, **params):
super().__init__(des_extractor, as_extractor)
self.detector = cv2.xfeatures2d.SURF_create(**params)
def get_keypoints(self, frame):
kp, des = self.detector.detectAndCompute(frame, None)
self.des = des
return kp
def get_descriptors(self, frame, kp):
if self.des_extractor is None:
if self.as_extractor:
kp, des = self.detector.compute(frame, kp)
return kp, des
else:
return kp, self.des
return self.des_extractor.get_descriptors(frame, kp)
def __str__(self):
return "SURF"
class ORB(DetectorDescriptorInterface):
def __init__(self, des_extractor=None, as_extractor=False, **params):
super().__init__(des_extractor, as_extractor)
self.detector = cv2.ORB_create(**params)
def get_keypoints(self, frame):
kp, des = self.detector.detectAndCompute(frame, None)
self.des = des
return kp
def get_descriptors(self, frame, kp):
if self.des_extractor is None:
if self.as_extractor:
kp, des = self.detector.compute(frame, kp)
return kp, des
else:
return kp, self.des
return self.des_extractor.get_descriptors(frame, kp)
def __str__(self):
return "ORB"
class AKAZE(DetectorDescriptorInterface):
def __init__(self, des_extractor=None, as_extractor=False, **params):
super().__init__(des_extractor, as_extractor)
self.detector = cv2.AKAZE_create(**params)
def get_keypoints(self, frame):
kp, des = self.detector.detectAndCompute(frame, None)
self.des = des
return kp
def get_descriptors(self, frame, kp):
if self.des_extractor is None:
if self.as_extractor:
kp, des = self.detector.compute(frame, kp)
return kp, des
else:
return kp, self.des
return self.des_extractor.get_descriptors(frame, kp)
def __str__(self):
return "AKAZE"
class BRIEF_Extractor(DetectorDescriptorInterface):
def __init__(self, **params):
super().__init__(des_extractor=None, as_extractor=True)
self.des_extractor = cv2.xfeatures2d.BriefDescriptorExtractor_create(**params)
def get_descriptors(self, frame, kp):
kp, des = self.des_extractor.compute(frame, kp)
return kp, des
def __str__(self):
return "BRIEF"