-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpicture_similarity.py
108 lines (87 loc) · 2.93 KB
/
picture_similarity.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
"""
简单数值运算
ahash
dhash
复杂图像比变换
phash
whash
pipline
灰度
resize->8*8
二值:hash
fingerprint
比较距离/相似性
pip install imagehash
"""
from PIL import Image
import numpy as np
import imagehash
def image_to_dhash(image_path, hash_size=8):
image = Image.open(image_path)
# grey, resize: 9*8
image = image.convert('L').resize((hash_size+1, hash_size), Image.ANTIALIAS)
pixels = np.asarray(image)
diff = pixels[:, 1:] > pixels[:, :-1]
# dhash = sum(2**(i%8) for i, v in enumerate(diff.flatten()) if v)
return diff
# def image_to_phash():
def binary_to_hex(arr):
bit_str = ''.join(str(b) for b in 1*arr.flatten())
print(bit_str)
width = int(np.ceil(len(bit_str)/4))
return '{:0>{width}x}'.format(int(bit_str, 2), width=width)
def hamming_distance(dhash1, dhash2, f=64):
if isinstance(dhash1, str):
dhash1 = int(dhash1, base=16)
if isinstance(dhash2, str):
dhash2 = int(dhash2, base=16)
x = (dhash1 ^ dhash2) & ((1 << f) - 1)
# 数1的个数
ans = 0
while x:
ans += 1
x &= x-1
return ans
def hamming_dist(dhash1, dhash2):
difference = (int(dhash1, 16)) ^ (int(dhash2, 16))
return bin(difference).count("1")
def hash_functions_eval(hash_function=imagehash.dhash):
lena_1 = hash_function(Image.open('./imgs/lena/origin-lena.png'))
lena_2 = hash_function(Image.open('./imgs/lena/blur-lena.png'))
lena_3 = hash_function(Image.open('./imgs/lena/resize-lena.png'))
lena_4 = hash_function(Image.open('./imgs/lena/shift-lena.png'))
forest = hash_function(Image.open('./imgs/forest-high.jpg'))
hashs = [lena_1, lena_2, lena_3, lena_4, forest]
for i in range(len(hashs)):
for j in range(i + 1, len(hashs)):
print(hash_function.__name__, i, j, hashs[i] - hashs[j])
def rotate_eval(img_path='./imgs/lena/origin-lena.png', hash_function=imagehash.dhash):
origin_image = Image.open(img_path)
origin_hash = hash_function(origin_image)
for r in range(1, 180, 10):
rotate_hash = hash_function(origin_image.rotate(r))
print(hash_function.__name__, r, origin_hash - rotate_hash)
if __name__ == '__main__':
# hash_functions_eval(imagehash.average_hash)
# print('----------------------')
#
# hash_functions_eval(imagehash.phash)
# print('----------------------')
#
# hash_functions_eval(imagehash.dhash)
# print('----------------------')
#
hash_functions_eval(imagehash.whash)
rotate_eval(hash_function=imagehash.phash)
class Solution:
def str_to_hex(self, s):
return ''.join([hex(ord(c)).replace('0x', '') for c in s])
def toHexspeak(self, num: str) -> str:
h = self.str_to_hex(num)
h = h.replace('1', 'I').replace('0', 'O')
s = '23456789'
for c in s:
if c in h:
return "ERROR"
return h
Solution().toHexspeak("257")