qarmin / img_hash

A Rust library for calculating perceptual hash values of images
Apache License 2.0
27 stars 11 forks source link

pHash correctness #12

Open leofidus opened 10 months ago

leofidus commented 10 months ago

I'm trying to use phash across different programming languages. For that purpose I consider the hashes produced by the python libraries https://github.com/thorn-oss/perception and https://github.com/JohannesBuchner/imagehash to be canonically correct.

The documentation of this project suggests that HasherConfig::new().hash_alg(HashAlg::Mean).preproc_dct().to_hasher() would produce a compatible hash, but in practice this is not the case. After some extensive experimentation, there are three changes I've identified to produce nearly the same results (Hamming Distance of ~4 on a 1024bit hash after these three changes):

I'll try to make the necessary PRs to make each of these options possible without changing the existing defaults.

gyk commented 7 months ago

Using median will also improve performance, because mean is susceptible to outliners. I have found that pHash by mean produces worse results (by my subjective evaluation) than aHash or dHash, which contradicts the findings from perception's benchmark where pHash is the winner. I was about to investigate the problem before I saw your post here, and it saved me a lot of time👍🏼.

I also ran a benchmark on a subset of "thorn-perceptual-benchmark-v0". Setting threshold = 3, the result is:

Hashing by Mean seems to produce significantly more false positives (visually dissimilar images reported as similar).