-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFindSimilitudes.py
220 lines (175 loc) · 5.61 KB
/
FindSimilitudes.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
import os
import sys
import numpy as np
import cv2
import sys
resize_w = 8
resize_h = 8
#Source: http://www.hackerfactor.com/blog/?/archives/432-Looks-Like-It.html
# We define all redeable files
accepted = ['jpg','jpeg', 'png', 'tif','tiff', 'bmp']
def average2D(table2D):
n = 0
s = 0
for i in table2D:
s += len(i)
for j in i:
n += j
return (n*1.0/s)
def hashTableA(table2D, averageHash):
for i in xrange(0, len(table2D)):
for j in xrange(0, len(table2D[i])):
if (table2D[i][j] > averageHash):
table2D[i][j] = '1'
else:
table2D[i][j] = '0'
return (table2D)
def hashTableD(table2D):
for i in xrange(0, len(table2D)):
for j in xrange(0, len(table2D[i])-1):
if (table2D[i][j] > table2D[i][j+1]):
table2D[i][j] = 1
else:
table2D[i][j] = 0
table2D = np.delete(table2D,np.s_[-1:],1)
return (table2D)
def concatenation(table2D):
table2D = [ y for x in table2D for y in x]
table2D = ''.join(str(int(i)) for i in table2D)
table2D = int(table2D, 2)
table2D = hex(table2D)
return table2D[2:-1]
def match(hash1, hash2):
if hash1 == '':
hash1 = '0'
if hash2 == '':
hash2 = '0'
hex1 = int(hash1, 16)
hex2 = int(hash2, 16)
size = len(hash2) if hash1 <= hash2 else len(hash1)
size *= 4
similitude = (hex1 ^ hex2)
similitude = bin(similitude)[2:]
similitude = similitude.count('0') + size-len(similitude)
percentage = similitude*100.0/size
return percentage
def checkImage(img1, img2, path1, path2):
if ((img1 is None) or (img2 is None)):
if (img1 is None):
print ("Error: " + path1 + " is not an image")
if (img2 is None):
print ("Error: " + path2 + " is not an image")
sys.exit()
def aHash(path1,path2):
# Import images
img1 = cv2.imread(path1)
img2 = cv2.imread(path2)
checkImage(img1, img2, path1, path2)
# Resize them to 8x8
img1resize = cv2.resize(img1, (resize_w, resize_h))
img2resize = cv2.resize(img2, (resize_w, resize_h))
# Change color to black and white
img1resize = cv2.cvtColor( img1resize, cv2.COLOR_BGR2GRAY );
img2resize = cv2.cvtColor( img2resize, cv2.COLOR_BGR2GRAY );
# Calculate average color value
img1average = average2D(img1resize)
img2average = average2D(img2resize)
# Hash the image with the average color value
img1hashed = hashTableA(img1resize, img1average)
img2hashed = hashTableA(img2resize, img2average)
# Generage the hash Value
img1value = concatenation(img1hashed)
img2value = concatenation(img2hashed)
# Calculate the match between the two hash (in % )
matching = match(img1value, img2value)
print("Method A: " + str(round(matching, 2)) + "% match")
def pHash(path1,path2):
# Import images
img1 = cv2.imread(path1)
img2 = cv2.imread(path2)
checkImage(img1, img2, path1, path2)
# Resize them to 8x8
img1resize = cv2.resize(img1, (resize_w, resize_h))
img2resize = cv2.resize(img2, (resize_w, resize_h))
# Change color to black and white
img1resize = cv2.cvtColor( img1resize, cv2.COLOR_BGR2GRAY );
img2resize = cv2.cvtColor( img2resize, cv2.COLOR_BGR2GRAY );
# Calculate the DCT
img1dct = cv2.dct(np.float32(img1resize)/255.0)
img2dct = cv2.dct(np.float32(img2resize)/255.0)
#Calculate average DCT
img1avdct = average2D(img1dct)
img2avdct = average2D(img1dct)
#Hash the DCT
img1dcthash = hashTableA(img1dct, img1avdct)
img2dcthash = hashTableA(img2dct, img2avdct)
# Generage the hash Value
img1value = concatenation(img1dcthash)
img2value = concatenation(img2dcthash)
# Calculate the match between the two hash (in % )
matching = match(img1value, img2value)
print("Method P: " + str(round(matching, 2)) + "% match")
def dHash(path1,path2):
# Import images
error = False
img1 = cv2.imread(path1)
img2 = cv2.imread(path2)
checkImage(img1, img2, path1, path2)
# Resize them to 9x8
img1resize = cv2.resize(img1, (resize_w+1, resize_h))
img2resize = cv2.resize(img2, (resize_w+1, resize_h))
# Change color to black and white
img1resize = cv2.cvtColor( img1resize, cv2.COLOR_BGR2GRAY );
img2resize = cv2.cvtColor( img2resize, cv2.COLOR_BGR2GRAY );
# Hash the image with the average next color value (8x8)
img1hashed = hashTableD(img1resize)
img2hashed = hashTableD(img2resize)
# Generage the hash Value
img1value = concatenation(img1hashed)
img2value = concatenation(img2hashed)
# Calculate the match between the two hash (in % )
matching = match(img1value, img2value)
print("Method D: " + str(round(matching, 2)) + "% match")
def errorInput():
print("Pleaser enter arguments as follow : image1, image2, algorithm")
print('For the algorithm, you may use:')
print("# 'a', Using the Average Hash algorithm (ahash)")
print("# 'p' Using the Average Hash Perceptive (phash)")
print("# 'd' Using the Difference Hash algorithm (dhash)")
print("# 'all' for using all of the above")
print("The images format accepted are 'jpg','jpeg', 'png', 'tif','tiff', 'bmp' ")
def main():
if len(sys.argv) < 3:
errorInput()
sys.exit()
elif len(sys.argv) == 3:
arg3 = 'all' # hash algorithm
elif len(sys.argv) == 4:
arg3 = sys.argv[3] # hash algorithm
else:
errorInput()
sys.exit()
arg1 = sys.argv[1] # Picture 1
arg2 = sys.argv[2] # Picture 2
if (os.path.isfile(arg1) * os.path.isfile(arg2) == False):
if (os.path.isfile(arg1) * os.path.isfile(arg2) == False):
print("Image " + arg1 +" not found")
if (os.path.isfile(arg1) * os.path.isfile(arg2) == False):
print("Image " + arg2 +" not found")
sys.exit()
if arg3 == 'a':
aHash(arg1, arg2)
elif arg3 == 'p':
pHash(arg1, arg2)
elif arg3 == 'd':
dHash(arg1, arg2)
elif arg3 == 'all':
aHash(arg1, arg2)
pHash(arg1, arg2)
dHash(arg1, arg2)
else:
print ("Error in input algorithm argument !")
errorInput()
sys.exit()
print ("Done !")
main()