-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatcher.py
More file actions
145 lines (115 loc) · 4.31 KB
/
Copy pathMatcher.py
File metadata and controls
145 lines (115 loc) · 4.31 KB
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
#Cao Khac Le Duy @copyright
# 1351008
# All rights are reserved
import sys
import cv2
import numpy as np
from enum import Enum
import time
class Matcher(object):
def __init__(self):
self.detector = None
self.imgs = []
pass
def match(self,imgs):
self.imgs = imgs
pass
def setDetector(self,detector):
self.detector = detector
detector.callback = self.doMatch
pass
def doMatch(self):
pass
class SiftMatcher(Matcher):
def __init__(self):
super(SiftMatcher,self).__init__()
def match(self,imgs):
Matcher.match(self,imgs)
self.doMatch()
self.detector.addUi()
pass
def doMatch(self):
kpts1 = self.detector.getDetected(self.imgs[0])
kpts2 = self.detector.getDetected(self.imgs[1])
grays = [cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) for img in self.imgs]
sift = cv2.xfeatures2d.SIFT_create()
start = time.time()
desc = []
desc = sift.compute(grays,[kpts1, kpts2],desc)
bf = cv2.BFMatcher()
matches = None
# print (desc[1][0][0])
matches = bf.knnMatch(desc[1][0],desc[1][1], 2)
# Apply ratio test
good = []
for m,n in matches:
if m.distance < 0.75*n.distance:
good.append([m])
end = time.time()
print end - start, " Time consuming "
# cv2.drawMatchesKnn expects list of lists as matches.
img3 = None
img3 = cv2.drawMatchesKnn(self.imgs[0], kpts1, self.imgs[1], kpts2, matches,img3,matchColor = [0,255,0], flags=2)
cv2.imshow(self.detector.windowName, img3)
class LbpMatcher(Matcher):
def __init__(self):
super(LbpMatcher, self).__init__()
def match(self,imgs):
Matcher.match(self,imgs)
self.doMatch()
self.detector.addUi()
pass
def doMatch(self):
kpnts = [self.detector.getDetected(image) for image in self.imgs]
grays = [cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) for img in self.imgs]
start = time.time()
lbp = LbpDescriptorExtractor()
desc = [lbp.extractDescriptors(kpnts[0],grays[0]),
lbp.extractDescriptors(kpnts[1],grays[1])]
bf = cv2.BFMatcher()
matches = None
matches = bf.knnMatch(np.asarray(desc[0],dtype=np.float32),np.asarray(desc[1],dtype=np.float32),2)
# Apply ratio test
good = []
for m,n in matches:
if m.distance < 0.75*n.distance:
good.append([m])
end = time.time()
print end - start, " Time consuming "
# cv2.drawMatchesKnn expects list of lists as matches.
img3 = None
img3 = cv2.drawMatchesKnn(self.imgs[0],kpnts[0],self.imgs[1],kpnts[1],matches,img3,matchColor = [0,255,0], flags=2)
cv2.imshow(self.detector.windowName, img3)
class LbpDescriptorExtractor(object):
"Extract descriptors from keypoints"
def __init__(self):
pass
def extractDescriptors(self, kpnts, img):
descrpt = [self.buildHistoVector(self.getCellLbp(key,img)) for key in kpnts]
return descrpt
def getCellLbp(self, keypoint, img):
x,y = keypoint.pt
gapx = len(img) - int(x)
gapy = len(img[0]) - int(y)
gapx = gapx if gapx < 8 else 8
gapy = gapy if gapy < 8 else 8
cell = img[int(x) - gapx : int(x) + gapx, int(y) - gapy : int(y) + gapy]
return cell
def buildHistoVector(self, lbpCell):
vectorLbp = [0]*256
for i in range(1,len(lbpCell)-1):
for j in range(1,len(lbpCell[0])-1):
index = self.getBinaryNumberAt(i,j,lbpCell)
vectorLbp[index] += 1.0
return vectorLbp
def getBinaryNumberAt(self, x, y, ofCell):
count = 0
count += ((ofCell[x][y] < ofCell[x-1][y-1]) << 7)
count += ((ofCell[x][y] < ofCell[x-1][y]) << 6)
count += ((ofCell[x][y] < ofCell[x-1][y+1]) << 5)
count += ((ofCell[x][y] < ofCell[x][y+1]) << 4)
count += ((ofCell[x][y] < ofCell[x+1][y+1]) << 3)
count += ((ofCell[x][y] < ofCell[x+1][y]) << 2)
count += ((ofCell[x][y] < ofCell[x+1][y-1]) << 1)
count += ((ofCell[x][y] < ofCell[x][y-1]) << 0)
return count