forked from AlisaLC/CVFeatureMatching
-
Notifications
You must be signed in to change notification settings - Fork 0
/
detector.py
189 lines (139 loc) · 5.73 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
from typing import Any
import cv2
import numpy as np
class KeypointDetector:
def __init__(self):
pass
def __call__(self, *args: Any, **kwds: Any):
return self.detect_and_compute(*args, **kwds)
def detect(self, image):
raise NotImplementedError
def compute(self, image, keypoints):
raise NotImplementedError
def detect_and_compute(self, image):
raise NotImplementedError
def filter_points(self, keypoints, min_size=3):
return [kp for kp in keypoints if kp.size > min_size]
class HarrisDetector(KeypointDetector):
def __init__(self):
pass
def __harris_corners(self, grayscaled_image):
image = grayscaled_image.astype(np.float32)
dst = cv2.cornerHarris(image, 2, 3, 0.04)
dst = cv2.dilate(dst, None)
ret, dst = cv2.threshold(dst, 0.01*dst.max(), 255, 0)
dst = np.uint8(dst)
keypoints = [cv2.KeyPoint(x, y, 3) for y in range(dst.shape[0]) for x in range(dst.shape[1]) if dst[y, x] > 0]
return keypoints
def detect(self, image):
return self.__harris_corners(image)
def compute(self, image, keypoints):
return None, None
def detect_and_compute(self, image):
return self.__harris_corners(image), None
class ShiTomasiDetector(KeypointDetector):
def __init__(self):
pass
def detect(self, image):
return self.__shi_tomasi_corners(image)
def compute(self, image, keypoints):
return None, None
def detect_and_compute(self, image):
return self.__shi_tomasi_corners(image), None
def __shi_tomasi_corners(self, grayscaled_image, max_corners=23):
corners = cv2.goodFeaturesToTrack(grayscaled_image, max_corners, 0.01, 10)
corners = corners.reshape(-1, 2)
keypoints = [cv2.KeyPoint(x, y, 3) for x, y in corners]
return keypoints
class SIFTDetector(KeypointDetector):
def __init__(self):
self.detector = cv2.SIFT_create()
def detect(self, image):
return self.detector.detect(image)
def compute(self, image, keypoints):
return self.detector.compute(image, keypoints)
def detect_and_compute(self, image):
return self.detector.detectAndCompute(image, None)
class SURFDetector(KeypointDetector):
def __init__(self):
self.detector = cv2.xfeatures2d.SURF_create()
def detect(self, image):
return self.detector.detect(image)
def compute(self, image, keypoints):
return self.detector.compute(image, keypoints)
def detect_and_compute(self, image):
return self.detector.detectAndCompute(image, None)
class FastDetector(KeypointDetector):
def __init__(self):
self.detector = cv2.FastFeatureDetector_create()
def detect(self, image):
return self.detector.detect(image)
def compute(self, image, keypoints):
return None, None
def detect_and_compute(self, image):
return self.detector.detect(image), None
class BRIEFDetector(KeypointDetector):
def __init__(self):
self.star = cv2.xfeatures2d.StarDetector_create()
self.brief = cv2.xfeatures2d.BriefDescriptorExtractor_create()
def detect(self, image):
return self.star.detect(image)
def compute(self, image, keypoints):
return self.brief.compute(image, keypoints)
def detect_and_compute(self, image):
keypoints = self.star.detect(image)
return keypoints, self.brief.compute(image, keypoints)
class ORBDetector(KeypointDetector):
def __init__(self):
self.detector = cv2.ORB_create()
def detect(self, image):
return self.detector.detect(image)
def compute(self, image, keypoints):
return self.detector.compute(image, keypoints)
def detect_and_compute(self, image):
return self.detector.detectAndCompute(image, None)
class MSERDetector(KeypointDetector):
def __init__(self):
self.mser = cv2.MSER_create()
def __extract_regions(self, image):
regions, _ = self.mser.detectRegions(image)
keypoints = []
for region in regions:
if len(region) > 0:
region = np.array(region)
x, y, w, h = cv2.boundingRect(region.reshape(-1, 1, 2))
keypoints.append(cv2.KeyPoint(x + w / 2, y + h / 2, max(w, h)))
return keypoints
def detect(self, image):
return self.__extract_regions(image)
def compute(self, image, keypoints):
return None, None
def detect_and_compute(self, image):
keypoints = self.__extract_regions(image)
return keypoints, None
class AKAZEDetector(KeypointDetector):
def __init__(self):
super().__init__()
self.akaze = cv2.AKAZE_create()
def detect(self, image):
keypoints = self.akaze.detect(image, None)
return keypoints
def compute(self, image, keypoints):
keypoints, descriptors = self.akaze.compute(image, keypoints)
return keypoints, descriptors
def detect_and_compute(self, image):
keypoints, descriptors = self.akaze.detectAndCompute(image, None)
return keypoints, descriptors
class BRISKDetector(KeypointDetector):
def __init__(self):
super().__init__()
self.brisk = cv2.BRISK_create()
def detect(self, image):
keypoints = self.brisk.detect(image, None)
return keypoints
def compute(self, image, keypoints):
keypoints, descriptors = self.brisk.compute(image, keypoints)
return keypoints, descriptors
def detect_and_compute(self, image):
keypoints, descriptors = self.brisk.detectAndCompute(image, None)
return keypoints, descriptors