title: Simple and fast method to compare images for similarity
body: <p>I need a simple and fast way to compare two images for similarity. I.e. I want to get a high value if they contain exactly the same thing but may have some slightly different background and may be moved / resized by a few pixel.</p>
  
  <p>(More concrete, if that matters: The one picture is an icon and the other picture is a subarea of a screenshot and I want to know if that subarea is exactly the icon or not.)</p>
  
  <p>I have <strong>OpenCV</strong> at hand but I am still not that used to it.</p>
  
  <p>One possibility I thought about so far: Divide both pictures into 10x10 cells and for each of those 100 cells, compare the color histogram. Then I can set some made up threshold value and if the value I get is above that threshold, I assume that they are similar.</p>
  
  <p>I haven't tried it yet how well that works but I guess it would be good enough. The images are already pretty much similar (in my use case), so I can use a pretty high threshold value.</p>
  
  <p>I guess there are dozens of other possible solutions for this which would work more or less (as the task itself is quite simple as I only want to detect similarity if they are really very similar). What would you suggest?</p>
  
  <hr>
  
  <p>There are a few very related / similar questions about obtaining a signature/fingerprint/hash from an image:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/2146542/opencv-surf-how-to-generate-a-image-hash-fingerprint-signature-out-of-the">OpenCV / SURF How to generate a image hash / fingerprint / signature out of the descriptors?</a></li>
  <li><a href="https://stackoverflow.com/questions/596262/image-fingerprint-to-compare-similarity-of-many-images">Image fingerprint to compare similarity of many images</a></li>
  <li><a href="https://stackoverflow.com/questions/1034900/near-duplicate-image-detection/">Near-Duplicate Image Detection</a></li>
  <li><a href="https://stackoverflow.com/questions/7205489/opencv-fingerprint-image-and-compare-against-database">OpenCV: Fingerprint Image and Compare Against Database</a>.</li>
  <li><a href="https://stackoverflow.com/questions/843972/image-comparison-fast-algorithm">more</a>, <a href="https://stackoverflow.com/questions/23931/algorithm-to-compare-two-images">more</a>, <a href="https://stackoverflow.com/questions/189943/how-can-i-quantify-difference-between-two-images">more</a>, <a href="https://stackoverflow.com/questions/25977/how-can-i-measure-the-similarity-between-two-images">more</a>, <a href="https://stackoverflow.com/questions/75891/algorithm-for-finding-similar-images">more</a>, <a href="https://stackoverflow.com/questions/5730631/image-similarity-comparison">more</a>, <a href="https://stackoverflow.com/questions/25105599/algorithm-for-finding-similar-images-using-an-index">more</a></li>
  </ul>
  
  <p>Also, I stumbled upon these implementations which have such functions to obtain a fingerprint:</p>
  
  <ul>
  <li><a href="http://phash.org/" rel="noreferrer">pHash</a></li>
  <li><a href="http://www.imgseek.net/" rel="noreferrer">imgSeek</a> (<a href="https://github.com/ricardocabral/iskdaemon" rel="noreferrer">GitHub repo</a>) (GPL) based on the paper <a href="http://grail.cs.washington.edu/projects/query/" rel="noreferrer">Fast Multiresolution Image Querying</a></li>
  <li><a href="https://github.com/ascribe/image-match" rel="noreferrer">image-match</a>. Very similar to what I was searching for. Similar to pHash, based on <a href="http://www.cs.cmu.edu/~hcwong/Pdfs/icip02.ps" rel="noreferrer">An image signature for any kind of image, Goldberg et al</a>. Uses Python and Elasticsearch.</li>
  <li><a href="http://iqdb.org/code/" rel="noreferrer">iqdb</a></li>
  <li><a href="https://github.com/JohannesBuchner/imagehash" rel="noreferrer">ImageHash</a>. supports pHash.</li>
  <li><a href="https://idealo.github.io/imagededup/" rel="noreferrer">Image Deduplicator (imagededup)</a>. Supports CNN, PHash, DHash, WHash, AHash.</li>
  </ul>
  
  <p>Some discussions about perceptual image hashes: <a href="https://news.ycombinator.com/item?id=2614797" rel="noreferrer">here</a></p>
  
  <hr>
  
  <p>A bit offtopic: There exists many methods to create audio fingerprints. <a href="http://musicbrainz.org" rel="noreferrer">MusicBrainz</a>, a web-service which provides fingerprint-based lookup for songs, has a <a href="http://wiki.musicbrainz.org/Fingerprinting" rel="noreferrer">good overview in their wiki</a>. They are using <a href="http://acoustid.org/" rel="noreferrer">AcoustID</a> now. This is for finding exact (or mostly exact) matches. For finding similar matches (or if you only have some snippets or high noise), take a look at <a href="http://echoprint.me/" rel="noreferrer">Echoprint</a>. A related SO question is <a href="https://stackoverflow.com/questions/2068286/music-recognition-and-signal-processing">here</a>. So it seems like this is solved for audio. All these solutions work quite good.</p>
  
  <p>A somewhat more generic question about fuzzy search in general is <a href="https://stackoverflow.com/questions/13514575/how-to-do-fuzzy-search-in-big-data">here</a>. E.g. there is <a href="http://en.wikipedia.org/wiki/Locality-sensitive_hashing" rel="noreferrer">locality-sensitive hashing</a> and <a href="http://en.wikipedia.org/wiki/Nearest_neighbor_search" rel="noreferrer">nearest neighbor search</a>.</p>