-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageSearch.py
More file actions
431 lines (343 loc) · 14.1 KB
/
ImageSearch.py
File metadata and controls
431 lines (343 loc) · 14.1 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
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
#!/usr/bin/python
import Image, ImageFilter, ImageChops
import sys
import os
import math
from datetime import datetime
# a class to represent the ImageSearch application
class ImageSearch:
# constructor, takes the pattern image location string and source image location string
# if either image/directory is not found, terminate program and alert user
def __init__(self, pattern_array, source_array):
self.pattern_array = pattern_array
self.source_array = source_array
self.current_confidence = 0
self.matches = []
# function for matching two directories of images
def match_images(self):
for pattern in self.pattern_array:
for source in self.source_array:
try:
self.patternImage = Image.open(pattern)
self.patternName = pattern.split('/')[-1]
except (IOError, IndexError):
print >>sys.stderr, 'Pattern image not found or not of the correct image format.'
sys.exit(1)
try:
self.sourceImage = Image.open(source)
self.sourceName = source.split('/')[-1]
except (IOError, IndexError):
print >>sys.stderr, 'Source image not found or not of the correct image format.'
sys.exit(1)
self.patternFormat = self.patternImage.format
self.sourceFormat = self.sourceImage.format
if self.patternImage.mode != "RGB":
self.patternImage = self.patternImage.convert("RGB")
if self.sourceImage.mode != "RGB":
self.sourceImage = self.sourceImage.convert("RGB")
# try to match the images
self.patternPixels = self.patternImage.load()
self.patSize = self.patternImage.size
self.patPixelArray = []
for x in range(0, self.patSize[0]):
for y in range(0, self.patSize[1]):
self.patPixelArray.append((self.patternPixels[x,y], x, y))
self.sourcePixels = self.sourceImage.load()
self.sourceSize = self.sourceImage.size
self.sourcePixelArray = []
for x in range(0, self.sourceSize[0]):
for y in range(0, self.sourceSize[1]):
self.sourcePixelArray.append((self.sourcePixels[x,y], x, y))
if (self.patSize[0]*self.patSize[1]) > 300:
self.key_point_match()
else:
self.SAD()
# print all matches
for x in self.matches:
# print the match in the spec's format
# removed confidence level printing ----> + " with confidence " + str(x[5]) + "%"
print x[0] + " matches " + x[1] + " at "+ str(x[2][0]) + "x" + str(x[2][1]) + "+" + str(x[3]) + "+" + str(x[4])+ " (with "+ str(x[5]) + "% confidence)"
#########################################################
### SAD ALGORITHM, ONLY USE ON SMALL IMAGES VERY SLOW ###
#########################################################
def SAD(self):
xOffset = 0
yOffset = 0
for x in range(0, self.sourceSize[0]):
if x+self.patSize[0] <= self.sourceSize[0]:
for y in range(0, self.sourceSize[1]):
if y+self.patSize[1] <= self.sourceSize[1]:
diff = self.get_SAD_diff(x, y)
if diff < 100:
self.new_or_better_match((self.patternName, self.sourceName, self.patSize, x, y, 100))
def get_SAD_diff(self, xoffset, yoffset):
total_diff = 0
for x in range(0, self.patSize[0]):
for y in range(0, self.patSize[1]):
sourcePixel = self.sourcePixels[x+xoffset, y+yoffset]
patternPixel = self.patternPixels[x, y]
total_diff += (abs(sourcePixel[0]-patternPixel[0])+abs(sourcePixel[1]-patternPixel[1])+abs(sourcePixel[2]-patternPixel[2]))
return total_diff
#########################
### END SAD ALGORITHM ###
#########################
# try to match these two images based on important pixels
def key_point_match(self):
self.patPixelArray.sort(key=lambda x: x[0]) # sorts the list of pattern "RGB" pixel data
uniques = imageSearch.find_unique_pixels(self.patPixelArray) # list of the unique pixels in the pattern image
foundIndex = -1
# checks to see any unique pixels are in the source image
for x in range(0, len(uniques)):
if self.is_pixel_in_source(uniques[x], self.sourcePixelArray):
foundIndex = x
break
if foundIndex != -1:
# if a unique pixel is in the source image, finds the pixel in the source
# and calculates the percentage of the match. If the percentage is above 50%,
# prints out the match message
source_coordinates = self.find_pixels_in_source(uniques[foundIndex], self.sourcePixelArray)
for i in range(0, len(source_coordinates)):
patternXC = uniques[foundIndex][1]
patternYC = uniques[foundIndex][2]
sourceXC = source_coordinates[i][0]
sourceYC = source_coordinates[i][1]
xOffset = sourceXC - patternXC
yOffset = sourceYC - patternYC
if xOffset >= 0 and yOffset >= 0:
self.current_confidence = 0;
percentage = self.percentage_of_unique_matches(uniques, xOffset, yOffset)
if(percentage >= .5):
# this also sets the confidence level self.current_confidence
confidence = self.check_exact_match(xOffset, yOffset)
if confidence > .7:
# inverse the confidence to get the real value, then change to percent, trim decimals
self.current_confidence = 100 * confidence
confd = int(self.current_confidence)
# decide whether to add the match to the total array of matches, replace a match, or do not add
self.new_or_better_match((self.patternName, self.sourceName, self.patSize, xOffset, yOffset, confd))
# Takes a sorted list of pattern pixels to begin with
# It then takes the 100 least value RGB pixels and
# 100 of the largest ones. If the pattern picture has less than 200 pixels total, the whole picture will
# be returned and compared
def find_unique_pixels(self, pattern):
uniques = []
length = len(self.patPixelArray)
if(length > 201):
for x in range(0, 100):
uniques.append(self.patPixelArray[x])
for x in range((length-101), length-1):
uniques.append(self.patPixelArray[x])
else:
for x in range(0, length-1):
uniques.append(self.patPixelArray[x])
return uniques
# checks if this is a duplicate match/over-lapping match
def new_or_better_match(self, image_info):
if len(self.matches) > 0:
for i in range(0, len(self.matches)):
# if the pattern and source names are the same we should check
# if the matched area are over lapping too much (50 percent)
if self.matches[i][0] == image_info[0] and self.matches[i][1] == image_info[1]:
xOffsetDiff = abs(self.matches[i][3] - image_info[3])
yOffsetDiff = abs(self.matches[i][4] - image_info[4])
xC = abs(self.matches[i][2][0] - xOffsetDiff)
yC = abs(self.matches[i][2][1] - yOffsetDiff)
overlap_area = (xC*yC)
image_area = (self.matches[i][2][0]*self.matches[i][2][1])
percentage_overlap = (overlap_area+0.0)/(image_area+0.0)
if(percentage_overlap >= .5):
if not self.matches[i][5] > image_info[5]:
self.matches[i] = image_info
return 0
else:
return 0
else:
self.matches.append(image_info)
return 0
else:
self.matches.append(image_info)
return 0
else:
self.matches.append(image_info)
return 0
# determines if the pixel is in the picture
def is_pixel_in_source(self, pixel, array):
for x in range(0, len(array)):
if self.check_if_two_pixels_are_equivelant(array[x][0][0:3], pixel[0][0:3]):
return True
return False
# returns the coordinates to the pixel in the picture
def find_pixels_in_source(self, pixel, array):
matches = []
for x in range(0, len(array)):
if self.check_if_two_pixels_are_equivelant(array[x][0][0:3], pixel[0][0:3]):
matches.append((array[x][1], array[x][2]))
return matches
# returns the percentage of pixel to pixel matches in the unique pixel array and the source picture
def percentage_of_unique_matches(self, uniques, xOffset, yOffset):
matches = 0.00 # initial value for the match percentage
for x in range(0, len(uniques), 10):
patternXC = uniques[x][1]+xOffset
patternYC = uniques[x][2]+yOffset
if(patternXC >= 0 and patternYC >= 0 and patternXC <= (self.sourceSize[0]-1) and patternYC <= (self.sourceSize[1]-1)):
source_pixel = self.sourcePixels[patternXC, patternYC]
if self.check_if_two_pixels_are_equivelant(source_pixel[0:3], uniques[x][0][0:3]):
matches += 1.00
return matches/((len(uniques)/10.0)+0.00)
# checks if the current pattern and source image exactly match in the
# partial match area. This also sets the confidence level of the match
def check_exact_match(self, xOffset, yOffset):
total_pixels = len(self.patPixelArray)
matched = 0
for y in range(0, self.patSize[1]):
for x in range(0, self.patSize[0]):
if x + xOffset < self.sourceSize[0] and y + yOffset < self.sourceSize[1]:
patPixel = self.patternPixels[x, y]
sourcePixel = self.sourcePixels[x + xOffset, y + yOffset]
if self.check_if_two_pixels_are_equivelant(patPixel, sourcePixel) == True:
matched += 1
else:
return 0
return (matched+0.0)/(total_pixels+0.0)
# checks pixel equivalency rather than equality, since changing image format will alter pixels
def check_if_two_pixels_are_equivelant(self, pixel1, pixel2):
tolerableDiff = 5
# if both PNG, little room for error
# if one is JPG, tolerable error = 60
# if one is GIF, tolerable error = 150
if self.patternFormat == "JPEG" or self.sourceFormat == "JPEG":
tolerableDiff = 30
if self.patternFormat == "GIF" or self.sourceFormat == "GIF":
tolerableDiff = 75
Rdiff = math.fabs(pixel1[0] - pixel2[0])
Gdiff = math.fabs(pixel1[1] - pixel2[1])
Bdiff = math.fabs(pixel1[2] - pixel2[2])
totalDiff = Rdiff + Gdiff + Bdiff
if (totalDiff < tolerableDiff):
self.current_confidence += totalDiff/tolerableDiff
return True
else:
return False
######### Input Checks ###############
# used to check if an image format is supported by the program
# Arguments: fileLoc is the file location and imgtype is the type of input (pattern, source, etc.)
def checkFormat(fileLoc, imgtype):
try:
frm = Image.open(fileLoc).format
except (IndexError, IOError):
print >>sys.stderr, 'Corrupted Image/File Found'
sys.exit(1)
if frm != 'JPG' and frm != 'JPEG' and frm != 'PNG' and frm != 'GIF':
print >>sys.stderr, 'Unsupported file format: ' + '.' + frm + " in " + imgtype
sys.exit(1)
# used to check if a directory has any subdirectories. Exits with exit code 1 if true
# Arguments: fileLoc is the file location and direc is the type of directory: pattern or source
def checkSubDir(fileLoc, direc, p):
try:
path = p.split(fileLoc)[0] + '/' # gets the path that leads to 'fileLoc'
except (IndexError):
print >>sys.stderr, 'Problem with directory path'
sys.exit(1)
if os.path.isdir(os.path.join(path, fileLoc)):
print >>sys.stderr, 'Subdirectory found in ' + direc
sys.exit(1)
# used to check if a file exists. Exits with exit code 1 if false
def checkExistence(fileLoc, filetype):
if not os.path.exists(fileLoc):
print >>sys.stderr, filetype + ' <' + fileLoc + '> does not exist'
sys.exit(1)
#**************************************************#
#**********# BEGIN EXECUTION OF PROGRAM #**********#
#**************************************************#
startTime = datetime.now()
# parse command line arguments as the assignment requires
pattern = "NONE"
source = "NONE"
pattern_dir = "NONE"
source_dir = "NONE"
for x in range(0, len(sys.argv)):
if pattern == "NONE":
if(str(sys.argv[x]) == '-p'):
try:
pattern = str(sys.argv[x+1])
except(IndexError):
print >>sys.stderr, 'There was a problem parsing the command line arguments'
sys.exit(1)
# check if file exists
checkExistence(pattern, 'pattern image')
#check for unsupported file formats
checkFormat(pattern, 'pattern image')
if source == "NONE":
if(str(sys.argv[x]) == '-s'):
try:
source = str(sys.argv[x+1])
except(IndexError):
print >>sys.stderr, 'There was a problem parsing the command line arguments'
sys.exit(1)
# check if file exists
checkExistence(source, 'source image')
# check for unsupported file formats
checkFormat(source, 'source image')
if source_dir == "NONE":
if(str(sys.argv[x]) == '-sdir'):
try:
source_dir = str(sys.argv[x+1])
except(IndexError):
print >>sys.stderr, 'There was a problem parsing the command line arguments'
sys.exit(1)
# check if directory exists
checkExistence(source_dir, 'source directory')
# check for subdirectories and unsupported file formats
try:
for f in os.listdir(source_dir):
checkSubDir(f, 'source directory', source_dir)
checkFormat(source_dir+"/"+f, 'source directory')
except (OSError, WindowsError):
print >>sys.stderr, 'The source directory name is invalid'
sys.exit(1)
if pattern_dir == "NONE":
if(str(sys.argv[x]) == '-pdir'):
try:
pattern_dir = str(sys.argv[x+1])
except(IndexError):
print >>sys.stderr, 'There was a problem parsing the command line arguments'
sys.exit(1)
# check if directory exists
checkExistence(pattern_dir, 'pattern directory')
# check for subdirectories and unsupported file formats
try:
for f in os.listdir(pattern_dir):
checkSubDir(f, 'pattern directory', pattern_dir)
checkFormat(pattern_dir+"/"+f, 'pattern directory')
except (OSError, WindowsError):
print >>sys.stderr, 'The pattern directory name is invalid'
sys.exit(1)
# if the command line arguments were set then run the program, otherwise alert the user they did something wrong
if (pattern != "NONE" or pattern_dir != "NONE") and (source != "NONE" or source_dir != "NONE"):
# get pattern images into an array
if pattern != "NONE":
pattern_array = [pattern]
else:
pattern_array = []
for root, subFolders, files in os.walk(pattern_dir):
# add all image paths to the array
for file in files:
pattern_array.append(os.path.join(root,file))
# get source images into an array
if source != "NONE":
source_array = [source]
else:
source_array = []
for root, subFolders, files in os.walk(source_dir):
# add all image paths to the array
for file in files:
source_array.append(os.path.join(root,file))
imageSearch = ImageSearch(pattern_array, source_array)
# match images, print out results
imageSearch.match_images()
else:
print >>sys.stderr, 'There was a problem parsing the command line arguments'
sys.exit(1)
# uncomment to see benchmark
# print(datetime.now()-startTime)
sys.exit(0)