Tuesday, February 16, 2016

Are these Images Similar? Simple Machine Vision with a Perceptual Hash

From https://evelyngarone.wordpress.com/2011/11/17/cute-and-funny-cats/


At my job I had an actual requirement to tell if a given image was similar to a previous image (to detect a bad video input).  That was it, no background or suggestions on how to go about it.  This sounded a lot like Machine Vision to me, which is somewhere under the Artificial Intelligence (AI) umbrella.

I promptly started to freak-out (see image at right).


I quickly came across the concept of a Perceptual Hash during my first Google searches.  This led to finding first a C (++?) open-source library called pHash.  This prompted a further search for a Java open source library, which led to a Stack-Overflow question and a small Java class to take care of the heavy lifting.  The docs of this further linked to another source of inspiration on Hacker Factor for that author.


This class, ImagePHash by Elliot Shepherd, will work as-is without me having to delve into the gory details too much (the Hacker Factor link provided an excellent overview).


It worked really well!  I just put in some tweaks to Springify it (see Java Papers tutorial on Spring annotations for details) and use the logging framework the project uses instead of System.out calls and I was getting back the "distance" between two perceptual hashes in no time.

I still had to interpret these results because my code base needed a yes-no answer.  So I downloaded about 5 images from the Internet that were similar to my original image, and cropped the original image as well.  I found that a distance of 8 was a good cut-off for sameness, e.g. for a distance of 8 or below I would consider the images the same.  This would count the cropped image as the same, but not similar but looks different to me images.

I would be more specific but it was done on company time so I can't go into details too much (see also: Non-disclosure Agreements).


The whole loop took about a day and wasn't too scary once I go into it.  I'm glad that I didn't try to re-invent the wheel and that the class worked!

No comments:

Post a Comment