How to calculate an image's PHASH signature and a distance between images?

Perceptual hashing allows you to generate a unique signature of an image that correlates to its visual appearance and allows you to calculate the "distance" between images using some bitwise arithmetic operations.

While Cloudinary provides server side support for retrieving the PHASH signature of your uploaded images, sometimes you may find yourself in need of pre-calculating this on your side. For that purpose you may find some useful open-source libraries, such as (for PHP): https://github.com/jenssegers/imagehash

Then to determine the "distance" between two signatures of two different images, you can use something like the following (demonstrated using PHP and gmp library):

var p1 = gmp_init(phash1, 16);
var p2 = gmp_init(phash2, 16);
var diff = gmp_popcount(gmp_xor(p1,p2));
Have more questions? Submit a request

Comments

Powered by Zendesk