JohannesBuchner / imagehash

A Python Perceptual Image Hashing Module
BSD 2-Clause "Simplified" License
3.12k stars 328 forks source link

phash vs. phash_simple Hamming Distances #211

Open Aliiiqbp opened 3 weeks ago

Aliiiqbp commented 3 weeks ago

I am curious about why the Hamming distance between hash values computed from arbitrary images using the "phash" method is always an even number, while this is not the case with the "phash_simple" function. The only difference between these methods is that phash uses the "median", while phash_simple uses the "mean".

JohannesBuchner commented 3 weeks ago

dctlowfreq is also different

JohannesBuchner commented 3 weeks ago

and dct

Aliiiqbp commented 3 weeks ago

Thank you for your response. The key question is whether we can reduce the hash size by half using the pHash function while maintaining the same semantics, given that the hash values are all at a 2k-bit distance.

I plotted the Hamming distance distribution for a subset of the DIV2K dataset, and there are no odd distances between any pair of images, which does not make sense while this is not the case in phash_simple or other methods like aHash or dHash.

phash: pHash

phash_simple: Figure_2

ahash: Figure_3

dhash: Figure_4

JohannesBuchner commented 3 weeks ago

See also https://github.com/JohannesBuchner/imagehash/issues/13 https://github.com/JohannesBuchner/imagehash/issues/17 https://github.com/JohannesBuchner/imagehash/issues/97 Maybe from these you can get an understanding why the distances are even.

I am also not sure the hash size and the hamming distance are related such that even distances imply that the hash size can be reduced. I am also hesitant to break backwards compatibility regarding the way the hash size is stored.