# python︱imagehash中的四种图像哈希方式（phash/ahash/dhash/小波hash）

pip install imagehash

# 1 perception hashing

• 两个参数，一起决定了图片resize的大小，最适合的才最好，按照公式： img_size = hash_size * highfreq_factor
• hash_size代表最终返回hash数值长度
• highfreq_factor，代表resize的尺度

highfreq_factor = 1
hash_size = 8
img_size = hash_size * highfreq_factor

hash1 = imagehash.phash(Image.open('1_1.jpg'),hash_size=hash_size,highfreq_factor=highfreq_factor)
print(hash1)

hash2 = imagehash.phash(Image.open('5_1.jpg'),hash_size=hash_size,highfreq_factor=highfreq_factor)
print(hash2)
# > 5b7724c8bb364551

1 - (hash1 - hash2)/len(hash1.hash)**2 # 相似性

# 2 average hashing

  average_hash(image, hash_size=8)

hash_size = 6
hash1 = imagehash.average_hash(Image.open('1_1.jpg'),hash_size=hash_size)
print(hash1)

hash2 = imagehash.average_hash(Image.open('5_1.jpg'),hash_size=hash_size)
print(hash2)
# > 5b7724c8bb364551

1 - (hash1 - hash2)/len(hash1.hash)**2 # 相似性

# 3 difference hashing

def dhash(image, hash_size=8)

hash_size = 10
hash1 = imagehash.dhash(Image.open('5_1.jpg'),hash_size=hash_size)
print(hash1)

hash2 = imagehash.dhash(Image.open('1_1.jpg'),hash_size=hash_size)
print(hash2)
# > 5b7724c8bb364551

1 - (hash1 - hash2)/len(hash1.hash)**2 # 相似性

# 4 wavelet hashing

def whash(image, hash_size = 8, image_scale = None, mode = 'haar', remove_max_haar_ll = True)

• mode: ‘haar’ - Haar wavelets, by default ‘db4’ - Daubechies wavelets
• remove_max_haar_ll:是否去掉低频段位，low level (LL) frequency
• image_scale:图像重新resize成多大，一定是2的倍数

hash_size = 8
mode = 'db4'
image_scale = 64
hash1 = imagehash.whash(Image.open('1_1.jpg'),image_scale=image_scale,hash_size=hash_size,mode = mode)
print(hash1)

hash2 = imagehash.whash(Image.open('5_1.jpg'),image_scale=image_scale,hash_size=hash_size,mode = mode)
print(hash2)
# > 5b7724c8bb364551

1 - (hash1 - hash2)/len(hash1.hash)**2 # 相似性

0 条评论

## 相关文章

1974

3315

915

25710

1102

1163

3885

3184

### 用R语言实现深度学习情感分析

18岁虽然没有成为TF-boys，但是2018新的一年可以成为TF（Tensorflow-boys）啊~~ word embeddings介绍 之前建立的情感分...

1978

40011