[PYTHON] Compare two images with image hash

Introduction

Confirm the comparison method by image hash to confirm the identity of the two images.

About image hash

Hash generally refers to a value obtained from a string using a hash function. You can do the same with images and generate summarized values. Therefore, it is possible to narrow down images with similar patterns by using image hash for a large number of images.

Since there are multiple hash functions, it is necessary to narrow down to one in order to compare. reference: https://qiita.com/mamo3gr/items/b93545a0346d8731f03c

Compare two image hashes

To calculate the hash distance, take the exclusive OR (XOR) and count "1".

Source code example (Python)

sample.py


hash_a = 'b69cbd89090b8f8e'
hash_a_value = int(hash_a, 16)
hash_b = 'b69c3d89090b0f8e'
hash_b_value = int(hash_b, 16)

hamming_distance = bin(hash_a_value ^ hash_b_value).count('1')
print(hamming_distance)

** Calculate the Hamming distance of the hash in the above source. ** **

Converting hash_a to binary ** 1011011010011100101111011000100100001001000010111000111110001110 ** Converting hash_b to binary ** 1011011010011100001111011000100100001001000010110000111110001110 ** The exclusive OR is ** 00000000000000100000000000000000000000000000001000000000000000 **. The number of 1s is 2, and the Hamming distance is ** 2 **.

Image similarity by Hamming distance

When the Hamming distance is 10 or less, the images are almost the same, but they cannot be said to be exactly the same. If it is confirmed that they are the same, it needs to be confirmed by another process.

Summary

Image hashes alone are not enough to guarantee image identity. It is necessary to use the features of the image.

If the number of images to be compared is small, it is sufficient to use the features of the images for comparison. If you want to compare tens of millions of images, you may want to narrow down the comparison target by image hash.

Articles such as features https://qiita.com/hmichu/items/f5f1c778a155c7c414fd https://qiita.com/hitomatagi/items/caac014b7ab246faf6b1

Recommended Posts

Compare two images with image hash
Combine two images with Django
Image processing with MyHDL
Center images with python-pptx
Image Processing with PIL
Image download with Flickr API
Image processing with Python (Part 2)
Automatically download images with scraping
Bordering images with python Part 1
Read image coordinates with Python-matplotlib
Image processing with PIL (Pillow)
Image editing with python OpenCV
Save images with web scraping
Image upload & customization with django-ckeditor
Sorting image files with Python (2)
Sorting image files with Python (3)
Easy image classification with TensorFlow
Create Image Viewer with Tkinter
Image processing with Python (Part 1)
Tweet with image in Python
Sorting image files with Python
Image processing with Python (Part 3)
Image caption generation with Chainer
Get image features with OpenCV
Image recognition with Keras + OpenCV
[Python] Image processing with scikit-image
Two ways to display multiple graphs in one image with matplotlib