Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check similarity of two images that have different pixelization

I am running a python code to check similarity of Quora and Twitter users profiles photos, but i am not getting a positive result when images are the same.

This is the code for comparing the two images :

path_photo_quora= "/home/yousuf/Desktop/quora_photo.jpg" path_photo_twitter="/home/yousuf/Desktop/twitter_photo.jpeg" if open(path_photo_quora,"rb").read() == open(path_photo_twitter,"rb").read():      print('photos profile are identical') 

despite images are the same, the console is not printing "photos profile are identical", what can i do?

like image 281
Youcef Avatar asked Oct 10 '18 08:10

Youcef


People also ask

How can we compare similarity between two images?

The similarity of the two images is detected using the package “imagehash”. If two images are identical or almost identical, the imagehash difference will be 0. Two images are more similar if the imagehash difference is closer to 0.

How can I quantify difference between two images?

You can compare two images using functions from PIL. The diff object is an image in which every pixel is the result of the subtraction of the color values of that pixel in the second image from the first image.


2 Answers

You can use the imagehash library to compare similar images.

from PIL import Image import imagehash hash0 = imagehash.average_hash(Image.open('quora_photo.jpg'))  hash1 = imagehash.average_hash(Image.open('twitter_photo.jpeg'))  cutoff = 5  # maximum bits that could be different between the hashes.   if hash0 - hash1 < cutoff:   print('images are similar') else:   print('images are not similar') 

Since the images are not exactly the same, there will be some differences, so therefore we use a cutoff value with an acceptable maximum difference. That difference between the hash objects is the number of bits that are flipped. But imagehash will work even if the images are resized, compressed, different file formats or with adjusted contrast or colors.

The hash (or fingerprint, really) is derived from a 8x8 monochrome thumbnail of the image. But even with such a reduced sample, the similarity comparisons give quite accurate results. Adjust the cutoff to find a balance between false positives and false negatives that is acceptable.

With 64 bit hashes, a difference of 0 means the hashes are identical. A difference of 32 means that there's no similarity at all. A difference of 64 means that one hash is the exact negative of the other.

like image 177
Håken Lid Avatar answered Sep 24 '22 12:09

Håken Lid


The two images are NOT the same - only the thing imaged. The images obviously are different size, as you note yourself. Thus a comparison must fail.

You'll need to employ some kind of similarity check. The first step is to scale up the smaller image to the one of the larger one. Then you need to employ some mean of detecting and defining similarity. There are different ways and methods for that, and any combination of them might be valid.

For example see Checking images for similarity with OpenCV

like image 30
planetmaker Avatar answered Sep 23 '22 12:09

planetmaker