<p>I would like to compare a screenshot of one application (could be a Web page) with a previously taken screenshot to determine whether the application is displaying itself correctly. I don't want an exact match comparison, because the aspect could be slightly different (in the case of a Web app, depending on the browser, some element could be at a slightly different location). It should give a measure of how similar are the screenshots.</p>

<p>Is there a library / tool that already does that? How would you implement it?</p>

[{'answer_id': 25989, 'author': 'Vaibhav', 'author_id': 380, 'author_profile': 'https://Stackoverflow.com/users/380', 'pm_score': 2, 'selected': False, 'text': '<p>Well, not to answer your question directly, but I have seen this happen. Microsoft recently launched a tool called <a href="http://photosynth.net/" rel="nofollow noreferrer">PhotoSynth</a> which does something very similar to determine overlapping areas in a large number of pictures (which could be of different aspect ratios).</p>\n\n<p>I wonder if they have any available libraries or code snippets on their blog.</p>\n'}, {'answer_id': 25991, 'author': 'Konrad Rudolph', 'author_id': 1968, 'author_profile': 'https://Stackoverflow.com/users/1968', 'pm_score': 3, 'selected': False, 'text': '<p>You\'ll need <a href="http://en.wikipedia.org/wiki/Pattern_recognition" rel="noreferrer">pattern recognition</a> for that. To determine small differences between two images, <a href="http://en.wikipedia.org/wiki/Hopfield_net" rel="noreferrer">Hopfield nets</a> work fairly well and are quite easy to implement. I don\'t know any available implementations, though.</p>\n'}, {'answer_id': 25994, 'author': 'Matt Sheppard', 'author_id': 797, 'author_profile': 'https://Stackoverflow.com/users/797', 'pm_score': 2, 'selected': False, 'text': "<p>I wonder (and I'm really just throwing the idea out there to be shot down) if something could be derived by subtracting one image from the other, and then compressing the resulting image as a jpeg of gif, and taking the file size as a measure of similarity.</p>\n\n<p>If you had two identical images, you'd get a white box, which would compress really well. The more the images differed, the more complex it would be to represent, and hence the less compressible.</p>\n\n<p>Probably not an ideal test, and probably much slower than necessary, but it might work as a quick and dirty implementation.</p>\n"}, {'answer_id': 25998, 'author': 'dmckee --- ex-moderator kitten', 'author_id': 2509, 'author_profile': 'https://Stackoverflow.com/users/2509', 'pm_score': 2, 'selected': False, 'text': '<p>You might look at the code for the open source tool <A href="http://www.kudla.org/raindog/perl/" rel="nofollow noreferrer">findimagedupes</A>, though it appears to have been written in perl, so I can\'t say how easy it will be to parse...</p>\n\n<p>Reading the findimagedupes page that I liked, I see that there is a <A href="http://www.kudla.org/raindog/perl/finddupes.cpp" rel="nofollow noreferrer">C++ implementation of the same algorithm</A>. Presumably this will be easier to understand.</p>\n\n<p>And it appears you can also use <A href="http://gqview.sourceforge.net/" rel="nofollow noreferrer">gqview</A>.</p>\n'}, {'answer_id': 26004, 'author': 'hometoast', 'author_id': 2009, 'author_profile': 'https://Stackoverflow.com/users/2009', 'pm_score': 2, 'selected': False, 'text': '<p>to expand on Vaibhav\'s note, <a href="http://hugin.sourceforge.net/" rel="nofollow noreferrer">hugin</a> is an open-source \'autostitcher\' which should have some insight on the problem.</p>\n'}, {'answer_id': 26012, 'author': 'Ross', 'author_id': 2025, 'author_profile': 'https://Stackoverflow.com/users/2025', 'pm_score': -1, 'selected': False, 'text': "<p>Well a really base-level method to use could go through every pixel colour and compare it with the corresponding pixel colour on the second image - but that's a probably a very <strong>very</strong> slow solution.</p>\n"}, {'answer_id': 26061, 'author': 'Lasse V. Karlsen', 'author_id': 267, 'author_profile': 'https://Stackoverflow.com/users/267', 'pm_score': 7, 'selected': True, 'text': "<p>This depends entirely on how smart you want the algorithm to be.</p>\n\n<p>For instance, here are some issues:</p>\n\n<ul>\n<li>cropped images vs. an uncropped image</li>\n<li>images with a text added vs. another without</li>\n<li>mirrored images</li>\n</ul>\n\n<p>The easiest and simplest <em>algorithm</em> I've seen for this is just to do the following steps to each image:</p>\n\n<ol>\n<li>scale to something small, like 64x64 or 32x32, disregard aspect ratio, use a combining scaling algorithm instead of nearest pixel</li>\n<li>scale the color ranges so that the darkest is black and lightest is white</li>\n<li>rotate and flip the image so that the lighest color is top left, and then top-right is next darker, bottom-left is next darker (as far as possible of course)</li>\n</ol>\n\n<p><strong>Edit</strong> A <em>combining scaling algorithm</em> is one that when scaling 10 pixels down to one will do it using a function that takes the color of all those 10 pixels and combines them into one. Can be done with algorithms like averaging, mean-value, or more complex ones like bicubic splines.</p>\n\n<p>Then calculate the mean distance pixel-by-pixel between the two images.</p>\n\n<p>To look up a possible match in a database, store the pixel colors as individual columns in the database, index a bunch of them (but not all, unless you use a very small image), and do a query that uses a range for each pixel value, ie. every image where the pixel in the small image is between -5 and +5 of the image you want to look up.</p>\n\n<p>This is easy to implement, and fairly fast to run, but of course won't handle most advanced differences. For that you need much more advanced algorithms.</p>\n"}, {'answer_id': 26677, 'author': 'Louis Brandy', 'author_id': 2089740, 'author_profile': 'https://Stackoverflow.com/users/2089740', 'pm_score': 5, 'selected': False, 'text': "<p>The 'classic' way of measuring this is to break the image up into some canonical number of sections (say a 10x10 grid) and then computing a histogram of RGB values inside of each cell and compare corresponding histograms. This type of algorithm is preferred because of both its simplicity and it's invariance to scaling and (small!) translation.</p>\n"}, {'answer_id': 30606, 'author': 'Lehane', 'author_id': 142, 'author_profile': 'https://Stackoverflow.com/users/142', 'pm_score': 5, 'selected': False, 'text': '<p>Use a normalised colour histogram. (Read the section on applications <a href="http://en.wikipedia.org/wiki/Color_histogram" rel="noreferrer">here</a>), they are commonly used in image retrieval/matching systems and are a standard way of matching images that is very reliable, relatively fast and very easy to implement.</p>\n\n<p>Essentially a colour histogram will capture the colour distribution of the image. This can then be compared with another image to see if the colour distributions match.</p>\n\n<p>This type of matching is pretty resiliant to scaling (once the histogram is normalised), and rotation/shifting/movement etc.</p>\n\n<p>Avoid pixel-by-pixel comparisons as if the image is rotated/shifted slightly it may lead to a large difference being reported.</p>\n\n<p>Histograms would be straightforward to generate yourself (assuming you can get access to pixel values), but if you don\'t feel like it, the <a href="http://en.wikipedia.org/wiki/OpenCV" rel="noreferrer">OpenCV</a> library is a great resource for doing this kind of stuff. <a href="http://vast.uccs.edu/~tboult/CS330/NOTES/OpenCVTutorial_II.ppt" rel="noreferrer">Here</a> is a powerpoint presentation that shows you how to create a histogram using OpenCV.</p>\n'}, {'answer_id': 76641, 'author': 'Mark B', 'author_id': 13070, 'author_profile': 'https://Stackoverflow.com/users/13070', 'pm_score': 4, 'selected': False, 'text': '<p>Don\'t video encoding algorithms like MPEG compute the difference between each frame of a video so they can just encode the delta?  You might look into how video encoding algorithms compute those frame differences.</p>\n\n<p>Look at this open source image search application <a href="http://www.semanticmetadata.net/lire/" rel="noreferrer">http://www.semanticmetadata.net/lire/</a>.  It describes several image similarity algorighms, three of which are from the MPEG-7 standard: ScalableColor, ColorLayout, EdgeHistogram and Auto Color Correlogram.</p>\n'}, {'answer_id': 205119, 'author': 'Mark Ransom', 'author_id': 5987, 'author_profile': 'https://Stackoverflow.com/users/5987', 'pm_score': 0, 'selected': False, 'text': "<p>If this is something you will be doing on an occasional basis and doesn't need automating, you can do it in an image editor that supports layers, such as Photoshop or Paint Shop Pro (probably GIMP or Paint.Net too, but I'm not sure about those).  Open both screen shots, and put one as a layer on top of the other.  Change the layer blending mode to Difference, and everything that's the same between the two will become black.  You can move the top layer around to minimize any alignment differences.</p>\n"}, {'answer_id': 6801185, 'author': 'Shachar', 'author_id': 859398, 'author_profile': 'https://Stackoverflow.com/users/859398', 'pm_score': 4, 'selected': False, 'text': '<p>You could use a pure mathematical approach of <code>O(n^2)</code>, but it will be useful only if you are certain that there\'s no offset or something like that. (Although that if you have a few objects with homogeneous coloring it will still work pretty well.)</p>\n\n<p>Anyway, the idea is the compute the normalized dot-product of the two matrices.\n<code>C = sum(Pij*Qij)^2/(sum(Pij^2)*sum(Qij^2))</code>.</p>\n\n<p>This formula is actually the "cosine" of the angle between the matrices (wierd). \nThe bigger the similarity (lets say <code>Pij=Qij</code>), C will be 1, and if they\'re completely different, lets say for every <code>i,j Qij = 1</code> (avoiding zero-division), <code>Pij = 255</code>, then for size <code>nxn</code>, the bigger <code>n</code> will be, the closer to zero we\'ll get. (By rough calculation: <code>C=1/n^2</code>).</p>\n'}, {'answer_id': 7521343, 'author': 'edk750', 'author_id': 622139, 'author_profile': 'https://Stackoverflow.com/users/622139', 'pm_score': 3, 'selected': False, 'text': '<p>A ruby solution can be <a href="https://github.com/mperham/phashion" rel="noreferrer">found here</a></p>\n\n<p>From the readme:</p>\n\n<p>Phashion is a Ruby wrapper around the pHash library, "perceptual hash", which detects duplicate and near duplicate multimedia files</p>\n'}, {'answer_id': 16107439, 'author': 'Mathias', 'author_id': 2299663, 'author_profile': 'https://Stackoverflow.com/users/2299663', 'pm_score': 2, 'selected': False, 'text': '<p>There\'s software for content-based image retrieval, which does (partially) what you need. All references and explanations are linked from the project site and there\'s also a short text book (Kindle): <a href="https://code.google.com/p/lire/" rel="nofollow">LIRE</a></p>\n'}, {'answer_id': 35112893, 'author': 'Gregor Simončič', 'author_id': 4007861, 'author_profile': 'https://Stackoverflow.com/users/4007861', 'pm_score': 3, 'selected': False, 'text': '<p>How to measure similarity between two images entirely depends on what you would like to measure, for example: contrast, brightness, modality, noise... and then choose the best suitable similarity measure there is for you. You can choose from <strong>MAD</strong> (mean absolute difference), <strong>MSD</strong> (mean squared difference) which are good for measuring brightness...there is also available <strong>CR</strong> (correlation coefficient) which is good in representing correlation between two images. You could also choose from histogram based similarity measures like <strong>SDH</strong> (standard deviation of difference image histogram) or multimodality similarity measures like <strong>MI</strong> (mutual information) or <strong>NMI</strong> (normalized mutual information). </p>\n\n<p>Because this similarity measures cost much in time, it is advised to scale images down before applying these measures on them. </p>\n'}, {'answer_id': 42481370, 'author': 'cpwah', 'author_id': 7396273, 'author_profile': 'https://Stackoverflow.com/users/7396273', 'pm_score': 1, 'selected': False, 'text': '<p>You can use Siamese Network to see if the two images are similar or dissimilar following this <a href="https://github.com/NVIDIA/DIGITS/tree/master/examples/siamese" rel="nofollow noreferrer">tutorial</a>. This tutorial cluster the similar images whereas you can use <code>L2</code> distance to measure the similarity of two images. </p>\n'}, {'answer_id': 43643455, 'author': 'emallove', 'author_id': 712035, 'author_profile': 'https://Stackoverflow.com/users/712035', 'pm_score': 1, 'selected': False, 'text': '<p><a href="http://www.scootersoftware.com/download.php" rel="nofollow noreferrer">Beyond Compare</a> has pixel-by-pixel comparison for images, e.g.,</p>\n\n<p><a href="https://i.stack.imgur.com/XliXd.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/XliXd.jpg" alt="enter image description here"></a></p>\n'}]
['https://Stackoverflow.com/questions/25977', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2680/']