-
Notifications
You must be signed in to change notification settings - Fork 0
/
Matcher.py
246 lines (185 loc) · 8.08 KB
/
Matcher.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
'''
General-Purpose Image Matching
==============================
Class that can match an image against a dataset with
options for parameters such as image size and the type
of image matching, e.g. color-based, SIFT, SURF, ORB,
etc.
Usage:
------
python Matcher.py -q [<query image>] -d [<directory>] -a [<algorithm>]
Viable algorithms are ORB, SIFT, and SURF.
'''
import cv2
import numpy as np
import argparse
import glob
import time
from matplotlib import pyplot as plt
from search import Searcher
from pano import Panorama
class Matcher(object):
def __init__(self, queryPath, directory, algorithm, width=800, height=600):
self.image = cv2.imread(queryPath)
self.w = width
self.h = height
self.image = cv2.resize(self.image, (self.w, self.h))
self.data = directory
self.alg = algorithm
def createIndex(self):
'''
Creates dictionary with keys as image names and histograms as values.
'''
def createHistogram(image, bins=[8, 8, 8]):
'''
Creates a flattened 3D histogram.
'''
hist = cv2.calcHist([image], [0, 1, 2], None, bins, [0, 256, 0, 256, 0, 256])
hist = cv2.normalize(hist, hist, 0, 255, cv2.NORM_MINMAX)
return hist.flatten()
print("Indexing: " + self.data + "...")
index = {}
for imagePath in glob.glob(self.data + "/*.jpg"):
filename = imagePath[imagePath.rfind("/") + 1:]
image = cv2.imread(imagePath)
print('\t%s' % imagePath)
features = createHistogram(image)
index[filename] = features
return index
def colorSearch(self, max_matches=5):
'''
Searches query image against index and returns the specified number of matches.
Results are in the format (chi-squared distance, image name).
'''
self.index = self.createIndex()
image = cv2.imread(self.image)
print("Querying: " + self.image + " ...")
searcher = Searcher(self.index)
queryFeatures = self.createHistogram(image)
results = searcher.search(queryFeatures)[:max_matches]
print("Matches found:")
for j in range(len(results)):
(score, imageName) = results[j]
print("\t%d. %s : %.3f" % (j+1, imageName, score))
return results
#################################
### Image Matching Algorithms ###
#################################
def ORBMatch(self, imagePath, display_results=False):
'''
Matches query against specified image using the Oriented FAST and Rotated BRIEF algorithm.
Matching is done through Brute-Force.
'''
training = cv2.imread(imagePath)
training = cv2.resize(training, (self.w, self.h))
orb = cv2.ORB_create()
kp1, des1 = orb.detectAndCompute(self.image, None)
kp2, des2 = orb.detectAndCompute(training, None)
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
matches = bf.match(des1, des2)
matches = sorted(matches, key=lambda x: x.distance)
if display_results:
draw_params = dict(matchColor=(0,255,0),
singlePointColor=None,
flags=2)
image = cv2.drawMatches(self.image, kp1, training, kp2, matches, None, **draw_params)
plt.imshow(image), plt.show()
return len(matches)
def SURFMatch(self, imagePath, display_results=False):
'''
Performs a match using Speeded-Up Robust Features algorithm.
Matching is done with Fast Library for Approximate Nearest Neighbors.
Lowe's ratio test is applied.
'''
training = cv2.imread(imagePath)
training = cv2.resize(training, (self.w, self.h))
surf = cv2.xfeatures2d.SURF_create()
kp1, des1 = surf.detectAndCompute(self.image, None)
kp2, des2 = surf.detectAndCompute(training, None)
FLANN_INDEX_KDTREE = 0
index_params = dict(algorithm=FLANN_INDEX_KDTREE, trees=5)
search_params = dict(checks=25)
flann = cv2.FlannBasedMatcher(index_params, search_params)
matches = flann.knnMatch(des1, des2, k=2)
filtered = list(filter(lambda x:x[0].distance < 0.7*x[1].distance, matches))
good = list(map(lambda x: x[0], filtered))
if display_results:
draw_params = dict(matchColor=(0,255,0),
singlePointColor=None,
flags=2)
result = cv2.drawMatches(self.image, kp1, training, kp2, good, None, **draw_params)
plt.imshow(result), plt.show()
return len(good)
def SIFTMatch(self, imagePath, display_results=False):
'''
Performs a match using Scale-Invariant Feature Transform algorithm.
Matching is done with Fast Library for Approximate Nearest Neighbors.
Lowe's ratio test is applied.
'''
training = cv2.imread(imagePath)
training = cv2.resize(training, (self.w, self.h))
sift = cv2.xfeatures2d.SIFT_create()
kp1, des1 = sift.detectAndCompute(self.image, None)
kp2, des2 = sift.detectAndCompute(training, None)
FLANN_INDEX_KDTREE = 0
index_params = dict(algorithm=FLANN_INDEX_KDTREE, trees=5)
search_params = dict(checks=50)
flann = cv2.FlannBasedMatcher(index_params, search_params)
matches = flann.knnMatch(des1, des2, k=2)
filtered = list(filter(lambda x:x[0].distance < 0.7*x[1].distance, matches))
good = list(map(lambda x: x[0], filtered))
if display_results:
draw_params = dict(matchColor=(0,255,0),
singlePointColor=None,
flags=2)
result = cv2.drawMatches(self.image, kp1, training, kp2, good, None, **draw_params)
plt.imshow(result), plt.show()
return len(good)
def write(self, filename, mode):
file = open(filename, mode)
totalMatches, results, bestMatch = self.run()
file.write(str(totalMatches) + '\n')
file.write('[')
for prob in results[:len(results)-1]:
file.write(str(prob) + ', ')
file.write(str(results[-1]) + ']\n')
angle = int(bestMatch[0].replace(self.data,'').replace('/angle','').replace('.jpg',''))
Panorama(self.data, 100, 100, angle).write(self.data + '_panorama.jpg')
def run(self):
start = time.time()
print('%s matching...' % self.alg)
matches = []
for i in range(0, 375, 15):
imagePath = self.data + '/angle' + str(i).zfill(3) + '.jpg'
print('\tMatching %s ...' % imagePath)
if self.alg == 'SIFT':
numMatches = self.SIFTMatch(imagePath)
elif self.alg == 'SURF':
numMatches = self.SURFMatch(imagePath)
else:
numMatches = self.ORBMatch(imagePath)
print("\tFound %s matches" % numMatches)
matches.append((imagePath, numMatches))
sorted_matches = sorted(matches, key=lambda x: x[1])
totalMatches = sum(list(map(lambda x: x[1], matches)))
for j in range(1,6):
(imageName, score) = sorted_matches[-j]
print("%d. %s : %0.3f" % (j, imageName, 1.0*score / totalMatches))
print("Found %d total matches" % totalMatches)
end = time.time()
print('Time elapsed: %0.1f s' % (end-start))
results = [1.0*match[1]/totalMatches for match in matches]
bestMatch = sorted_matches[-1]
# bestMatch = int(sorted_matches[-1][0].replace(self.data,'').replace('/angle','').replace('.jpg',''))
return (totalMatches, results, bestMatch)
if __name__ == '__main__':
ap = argparse.ArgumentParser()
ap.add_argument('-q', '--query', required=True,
help='Path to query image')
ap.add_argument('-d', '--dataset', required=True,
help='Path to directory of training images')
ap.add_argument('-a', '--algorithm', required=True,
help='Algorithm to use for matching')
args = vars(ap.parse_args())
print(__doc__)
Matcher(args['query'], args['dataset'], args['algorithm']).run()