-
Notifications
You must be signed in to change notification settings - Fork 0
/
image_match.py
240 lines (189 loc) · 6.55 KB
/
image_match.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
#!/bin/env python
#
# Matches the Top and Bottom half of a picture to a database of pictures.
#
import numpy as np
import cv2
import os
import sys
import matplotlib.pyplot as plt
from natsort import natsorted
#import code
from tkinter import *
X = 1
Y = 0
#Class to hold an image match
class ImageMatch:
#Orb
orb = cv2.ORB_create()
#sift = cv2.xfeatures2d.SIFT_create()
# create BFMatcher object
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
def __init__(self, img, name, ref=None, **kwargs):
self.img = img
#Name of image
self.name = name
self.matches = None
self.up_matches = None
self.down_matches = None
#Find keypoints on img
print("Reading %s" % (name))
self.kp, self.des = ImageMatch.orb.detectAndCompute(img,None)
#self.kp, self.des = ImageMatch.sift.detectAndCompute(img,None)
if ref != None:
self.set_reference(ref)
return super().__init__(**kwargs)
#Use another image as reference
def set_reference(self,ref):
self.ref = ref
self.matches = None
self.up_matches = None
self.down_matches = None
def match(self):
if self.matches == None:
print("Matching %s vs %s" % (self.name, self.ref.name))
# Match descriptors.
#self.matches = ImageMatch.bf.knnMatch(self.des,self.ref.des, k=1)
self.matches = ImageMatch.bf.match(self.des,self.ref.des)
# Sort them in the order of their distance.
self.matches = sorted(self.matches, key = lambda x:x.distance)
print("Distance %f, %d matches" % (self.distance(), len(self.matches)))
return self.matches
#Return if a kp is in the upper or lower part of the image
def up_or_down(self, kpidx):
y_max = self.img.shape[Y]
y_half = y_max * ( 0.50)
kp = self.kp[kpidx]
y = kp.pt[1]
if y < y_half:
return "up"
else:
return "down"
#Split matches in upper and lower part of image
def split_matches(self):
up = []
down = []
for m in self.match():
if self.up_or_down(m.queryIdx) == "up" and self.ref.up_or_down(m.trainIdx) == "up":
up.append (m)
elif self.up_or_down(m.queryIdx) == "down" and self.ref.up_or_down(m.trainIdx) == "down":
down.append (m)
else:
#Match is not correlated.
pass
#Store them sorted
self.up_matches = sorted(up, key = lambda x:x.distance)
self.down_matches = sorted(down, key = lambda x:x.distance)
def upper_matches(self):
if self.up_matches == None:
self.split_matches()
return self.up_matches
def lower_matches(self):
if self.down_matches == None:
self.split_matches()
return self.down_matches
def distance(self, updown = None):
N = 10
if updown == "up":
matches = self.upper_matches()[:N]
elif updown == "down":
matches = self.lower_matches()[:N]
else:
matches = self.match()[:N]
if len(matches) == 0:
return 99999
avg = sum(m.distance for m in matches)/len(matches)
avg_scaled = avg*(N/len(matches))
return avg_scaled
def print(self):
print("Image %s: Shape: %dx %dy" % (self.name, self.img.shape[X], self.img.shape[Y]))
print(" Total Distance: %f, %d matches" % (self.distance(), len(self.matches)))
print(" Upper Distance: %f, %d matches" % (self.distance("up"), len(self.up_matches)))
print(" Lower Distance: %f, %d matches" % (self.distance("down"), len(self.down_matches)))
def matchplot(self, updown = None):
N = 20
if updown == "up":
matches = self.upper_matches()[:N]
elif updown == "down":
matches = self.lower_matches()[:N]
else:
matches = self.match()[:N]
fig = plt.figure()
a = fig.add_subplot(1,1,1)
a.set_title("%s distance %0.2f, up %0.2f, down %0.2f" % (self.name, self.distance(), self.distance("up"), self.distance("down")))
img = cv2.drawMatches(self.img ,self.kp,
self.ref.img,self.ref.kp, matches, None, flags=2)
plt.imshow(img)
#Read images
def load_images_from_folder(folder):
images = []
for filename in os.listdir(folder):
img = cv2.imread(os.path.join(folder,filename), 0)
if img is not None:
images.append(ImageMatch(img,filename))
return images
# Test 1 picture
def match(ref):
global images
for image in images:
image.set_reference(ref)
#For all matches.
print("\n\nBest match for Total distance")
best = sorted(images, key = lambda x:x.distance())
for i in best[:10]:
i.print()
#for i in images[:1]:
#i.matchplot()
#i.matchplot("up")
#i.matchplot("down")
for p in ["up", "down"]:
print("\n\nBest match for %s distance" % (p))
best = sorted(images, key = lambda x:x.distance(p))
for i in best[:10]:
i.print()
for i in best[:1]:
i.matchplot(p)
plt.show()
def showit():
print("value is", var2.get())
idx = names_img.index(var2.get())
i = images[idx]
print ("Index %d is %s and %s" % (idx, names_img[idx], images[idx].name))
i.matchplot()
i.matchplot("up")
i.matchplot("down")
plt.show()
# Press the ok..
def ok():
print("value is", var.get())
idx = names.index(var.get())
ref = refs[idx]
match(ref)
def exit():
sys.exit()
if __name__ == '__main__':
#Read in the query images
refs = load_images_from_folder("questions")
images = load_images_from_folder("pictures")
master = Tk()
master.title("Picure matcher")
var = StringVar(master)
#Ask to pick
refs = natsorted(refs, key = lambda x:x.name)
names = [x.name for x in refs]
var.set(names[-1])
option = OptionMenu(master, var, *names)
option.pack()
button = Button(master, text="OK", command=ok)
button.pack()
images = natsorted(images, key = lambda x:x.name)
names_img = [x.name for x in images]
var2 = StringVar(master)
var2.set(names_img[-1])
show_opt = OptionMenu(master, var2, *names_img)
show_opt.pack()
showbtn = Button(master, text="Show", command=showit)
showbtn.pack()
exit = Button(master, text="Exit", command=exit)
exit.pack()
master.mainloop()