Perceptual Hash

Questions and postings pertaining to the development of ImageMagick, feature enhancements, and ImageMagick internals. ImageMagick source code and algorithms are discussed here. Usage questions which are too arcane for the normal user list should also be posted here.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Perceptual Hash

Post by snibgo »

Yes, I see the sum-squared-difference gives a measure of how close two images are. (Just as RMSE gives a measure of how close two pixels values are.)

The idea can be extended so that, not only can we compare one image with another, but we can (in principle) cluster them into groups.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Perceptual Hash

Post by fmw42 »

snibgo wrote:Yes, I see the sum-squared-difference gives a measure of how close two images are. (Just as RMSE gives a measure of how close two pixels values are.)

The idea can be extended so that, not only can we compare one image with another, but we can (in principle) cluster them into groups.
I am not sure the perceptual hash is intended to be used for clustering similar but different images. It is mostly a security check to see if an image has been tampered with other processing.

Also in principle, though probably very simplistic images, it can be used for subimage search to find rotated versions of the same small image in the larger image. See my last example on the PHASH page.
blue-j
Posts: 68
Joined: 2007-06-12T14:03:18-07:00

Re: Perceptual Hash

Post by blue-j »

this is very exciting and valuable work! i mean only to support you with my questions and suggestions. i know also this is very fresh.

i think for CBIR, we are talking about sorting by visual similarity, and with comparison, it's about comparing two images. they are separate use cases. for use in CBIR, the hash must be a lexicographic sortable string, as short as it can be while still being effective, and hopefully computed fairly quickly and before a query begins. i don't understand hash functions well enough to suggest a solution at this time, but my studies are motivated right now, so maybe i'll have something to share...
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Perceptual Hash

Post by fmw42 »

I have a test script that reads two images, gets the 42 floats for each, converts each to a 168 decimal digit hash and then unhashes to 42 floats and does the sum squared differences to get the similarity metric. It is just a test, but could be separated into two functions; one to hash and another to compare two hashes. It is in test phase right now.
Alix
Posts: 2
Joined: 2014-04-19T01:17:43-07:00
Authentication code: 6789

Re: Perceptual Hash

Post by Alix »

Thank you for your work. It's great to see this feature in imagemagick!

Any concrete solution yet, to get a unique hash per file ? It would improve this feature dramatically :)

thanks!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Perceptual Hash

Post by fmw42 »

I have a script that I have yet to publish to create a 168 numerical digit hash string from any image for the current perceptual hash. Then another script to take two hashes and do the comparisons. The first script converts the 42 floats to the 168 digits. The second script converts the 168 digits back to the 42 floats and then does the same comparison as in compare -metric phash. I will try to get them finished this next week, if not sooner.

I am also working on a histogram based hash that creates a string of binary digits directly that can be compared using the hamming distance.
Alix
Posts: 2
Joined: 2014-04-19T01:17:43-07:00
Authentication code: 6789

Re: Perceptual Hash

Post by Alix »

Great news fmw42. I'm really excited to try your scripts and build a db of hash from my 50k+ pics :D
ptsteadman
Posts: 1
Joined: 2014-08-01T14:39:00-07:00
Authentication code: 6789

Re: Perceptual Hash

Post by ptsteadman »

Hi Fred,
Thanks for the work on this, it looks great.
I have a question with regards to the 42 float values exposed in identify -moments. Do these values have "significant digits"? I am considering truncating some of the least-significant digits and then making a cryptographic hash of the array in order to create a "fuzzy key/hash" that would correspond to that image and other images very similar to it, for de-duplication. This would allow you to quickly check if another similar exists without comparing the image to every other image in the set of images. Do you anticipate any pitfalls to this approach? I'm going to read up on the method used to compute 42 phash values.
Thanks again.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Perceptual Hash

Post by fmw42 »

Do these values have "significant digits"?
What do you mean by that?

I have already created two scripts.

1) to convert the 42 floats to a string of 168 digits and
2) to compare two strings of 168 digits.

see my scripts, phashconvert and phashcompare. In the script, I use only the highest 4 digits. I tried with 5, which made the string longer, but only resulted in 0.1 difference when converted back to the 42 floats, well within the margin of error/tolerance.
blue-j
Posts: 68
Joined: 2007-06-12T14:03:18-07:00

Re: Perceptual Hash

Post by blue-j »

Fred, this is exciting and wonderful work! One question: how to interpret the phashcompare metric? I don't see anywhere where it's meaning is conveyed.

Thanks so much!
J
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Perceptual Hash

Post by fmw42 »

see the links at viewtopic.php?f=4&t=24906.

The results of the new script to convert the 42 floats to the string of digits and the script to do the compare have almost identical values to directly using

compare -metric phash ...
blue-j
Posts: 68
Joined: 2007-06-12T14:03:18-07:00

Re: Perceptual Hash

Post by blue-j »

In your example, you return a value of 118.937.

http://www.fmwconcepts.com/imagemagick/ ... /index.php

Does that mean it's more similar than a return of 100? I have no way to interpret the value that I can find. I read the link you provided, but apparently I don't understand this at your level and am missing something. I apologize for my thick head!

J
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Perceptual Hash

Post by fmw42 »

The smaller the value, the more similar the images.
blue-j
Posts: 68
Joined: 2007-06-12T14:03:18-07:00

Re: Perceptual Hash

Post by blue-j »

I would suppose this cannot be used yet for CBIR, since one might have to calculate the compare metric for too many images too swiftly. I am puzzled how this has been solved by others. I suspect the solution is somehow to output a sortable string where clusters are more similar to each other, and perhaps there is a way to convert your 42 digit identifier to a sortable string rather than have to calculate the compare metric...?

Libpuzzle is an open source similarity comparison tool, and I know there are some interesting discussions on how to solve this problem:

http://stackoverflow.com/questions/9703 ... f-pictures

http://download.pureftpd.org/libpuzzle/doc/README

I get lost easily here!! Fred, have you pursued this line of inquiry at all?

Best,
J
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Perceptual Hash

Post by fmw42 »

I do not think this is applicable to the sum square difference type of metric that has to be computed for 42 floats in phash.

I have not found a better perceptual hash method that is conducive to using hamming distance, which is very fast on a string of binary digits. See http://en.wikipedia.org/wiki/Hamming_distance

I have scripted and tested a method found at http://www.joics.com/publishedpapers/20 ... 7_4404.pdf for color images only that can be compared with the hamming distance. It uses a hash of 276 binary digits. But it does not seem to work quite as well as the current moments based phash, at least on the one image I have tested. I have have not had time to test on all 8 images in my test set. It has no grayscale counterpart.
Post Reply