-
Notifications
You must be signed in to change notification settings - Fork 0
/
Detector.py
295 lines (236 loc) · 9.01 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#Cao Khac Le Duy @copyright
# 1351008
# All rights are reserved
import cv2
"""
OpenCV
"""
import numpy as np
"""
Matrix operation library
"""
import time
class Detector(object):
"Dectector Parent Class"
def __init__(self):
self.windowName = ""
self.img = None
self.cloneImg = None
self.matcher = None
self.callback = self.displayDetected
pass
def addUi(self):
pass
def detect(self, img):
if self.img != None:
self.callback = None
self.img = img
self.displayDetected()
return
self.img = img
"Detect"
def getDetected(self,img):
pass
def displayDetected(self):
kpnts = self.getDetected(self.img)
if kpnts != None:
self.cloneImg = cv2.drawKeypoints(self.img, kpnts, np.array([]), (0,0,255), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
cv2.imshow(self.windowName, self.cloneImg)
def setMatcher(self, matcher):
self.matcher = matcher
if self.matcher != None:
self.matcher.setDetector(self)
def setWindowName(self, wdName):
self.windowName = wdName
###################################
#####------------ Blob ------#
###################################
class BlobDetector(Detector):
"Blob Method Detector"
def __init__(self):
"constructor"
super(BlobDetector, self).__init__()
self.params = cv2.SimpleBlobDetector_Params()
self.params.minDistBetweenBlobs = 0.0
self.params.minThreshold = 10
self.params.maxThreshold = 10
self.params.filterByArea = True
self.params.minArea = 0
self.params.filterByCircularity = True
self.params.minCircularity = 0.0
self.params.filterByConvexity = True
self.params.minConvexity = 0.0
self.params.filterByInertia = True
self.params.minInertiaRatio = 0.0
self.callbacks = [self.setMinDistBlob,
self.setAreaMin,
self.setCirMin,
self.setConvexmin,
self.setInertia,
self.setMinThresold,
self.setMaxThresold]
def addUi(self):
cv2.createTrackbar("Min Dist between blobs",self.windowName, 0, 14,self.callbacks[0])
cv2.createTrackbar("Min Area",self.windowName, 0, 15, self.callbacks[1])
cv2.createTrackbar("Min Circularity",self.windowName, 0, 150, self.callbacks[2])
cv2.createTrackbar("Min Convexity",self.windowName, 0, 150, self.callbacks[3])
cv2.createTrackbar("Min Inertia Ratio",self.windowName, 0, 150, self.callbacks[4])
cv2.createTrackbar("Min Thresold",self.windowName, 0, 15, self.callbacks[5])
cv2.createTrackbar("Max Thresold",self.windowName, 0, 15, self.callbacks[6])
def detect(self, img):
Detector.detect(self, img)
self.displayDetected()
self.addUi()
def getDetected(self,img):
detector = cv2.SimpleBlobDetector_create(self.params)
start = time.time()
kpnts = detector.detect(img)
end = time.time()
print end - start, " Time consuming "
return kpnts
def setMinDistBlob(self,val):
self.params.minDistBetweenBlobs = val
if self.callback != None:
self.callback()
# def setFilterByColor(self,val):
# pass
# def setBlobColor(self,val):
# pass
def setAreaMin(self,val):
self.params.minArea = val*100
if self.callback != None:
self.callback()
def setCirMin(self,val):
self.params.minCircularity = val/100
if self.callback != None:
self.callback()
pass
def setConvexmin(self, val):
self.params.minConvexity = val/10
if self.callback != None:
self.callback()
def setInertia(self, val):
self.params.minInertiaRatio = val/1000
if self.callback != None:
self.callback()
def setMinThresold(self,val):
self.params.minThreshold = val * 10
if self.callback != None:
self.callback()
def setMaxThresold(self, val):
self.params.maxThreshold = val * 10
if self.callback != None:
self.callback()
def displayDetected(self):
Detector.displayDetected(self)
pass
###################################
#####------------ Harris ------#
###################################
class HarrisDetector(Detector):
"Harris Method Detector"
def __init__(self):
super(HarrisDetector, self).__init__()
self.minBlocksize = 2
self.minkdsize = 1
self.kdsize = self.minkdsize
self.blocksize = self.minBlocksize
self.kfreeval = 0
self.callbacks = [self.setKDSize, self.setKFreeVal, self.setBlockSize]
def addUi(self):
cv2.createTrackbar("Derivative K Size",self.windowName, 0, 14, self.callbacks[0])
cv2.createTrackbar("Harris K Value",self.windowName, 0, 30, self.callbacks[1])
cv2.createTrackbar("Block size",self.windowName, 0, 15, self.callbacks[2])
def detect(self, img):
Detector.detect(self, img)
self.displayDetected()
self.addUi()
pass
def getDetected(self, img):
dest = None
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
kpnts = None
self.cloneImg = np.ones((img.shape[0],img.shape[1],img.shape[2]), dtype = "uint8")*img
if self.blocksize > 0:
start = time.time()
dest = cv2.cornerHarris(gray, self.blocksize, self.kdsize*2 + 1, self.kfreeval, cv2.BORDER_REFLECT101)
dest = cv2.dilate(dest,None)
kpntss = zip(*np.where(dest > 0.01 * dest.max()))
kpnts = [cv2.KeyPoint(y,x,10) for x,y in kpntss]
end = time.time()
print end - start, " Time consuming "
else:
self.cloneImg = img
return kpnts
def displayDetected(self):
Detector.displayDetected(self)
pass
def setBlockSize(self, val):
self.blocksize = val + self.minBlocksize
if self.callback != None :
self.callback()
def setKDSize(self, val):
self.kdsize = val + self.minBlocksize
if self.callback != None :
self.callback()
def setKFreeVal(self, val):
self.kfreeval = val / 100.0
if self.callback != None :
self.callback()
###################################
#####------------ DoG ------#
###################################
class DoGDetector(Detector):
"DoG Method detecting"
def __init__(self):
super(DoGDetector, self).__init__()
self.thresold = 0
self.callbacks = [self.setThresold]
def addUi(self):
cv2.createTrackbar("Thresold",self.windowName, 0, 20, self.callbacks[0])
def detect(self, img):
Detector.detect(self,img)
self.displayDetected()
self.addUi()
def setThresold(self,vale):
self.thresold = vale/50.0
if self.callback != None :
self.callback()
def getDetected(self, img):
kpntss = []
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
start = time.time()
listlayers = self.getListOfLayers(gray,8)
differencesLayers = self.getDifferenceofLayers(listlayers)
conditionalMat = np.zeros(gray.shape,dtype=bool)
for i in range(1,len(differencesLayers)-1):
for m in range(1,gray.shape[0]-1):
for n in range(1,gray.shape[1]-1):
if conditionalMat[m][n]:
conditionalMat[m][n] = false
else:
conditionalMat[m][n] = (self.isLargerThanLocals(m,n,gray)) and (differencesLayers[i][m][n] > differencesLayers[i+1][m][n] + self.thresold) and (differencesLayers[i][m][n] > differencesLayers[i-1][m][n] + self.thresold)
kpntss += zip(*np.where(conditionalMat == True))
kpnts = [cv2.KeyPoint(y,x,10) for x,y in kpntss]
end = time.time()
print end - start, " Time consuming "
return kpnts
def isLargerThanLocals(self, x,y,grayimg):
comp = grayimg[x-1:x+2,y-1:y+2]
a = np.ones((3,3), dtype = np.float32)*grayimg[x,y]
# print a, " AND ", comp, "AND", np.all(a-comp >= 0)
return np.all((a-comp) >= 0)
def getDifferenceofLayers(self,listlayers):
result = [listlayers[i+1]-listlayers[i] for i in range(len(listlayers)-1)]
return result
def getListOfLayers(self, grayImg, numLayers):
listlayer = [self.gaussianDerivative(i,grayImg) for i in range(3,numLayers+3,2)]
return listlayer
def gaussianDerivative(self, kernelSize, grayImg):
kernel = cv2.getGaussianKernel(kernelSize,1)
grayImg = np.asanyarray(grayImg,dtype= np.float32)
dest = cv2.filter2D(grayImg, -1, kernel)
return dest
def displayDetected(self):
Detector.displayDetected(self)
pass