Does OpenCV support the comparison of two images, returning some value (maybe a percentage) that indicates how similar these images are? E.g. 100% would be returned if the same image was passed twice, 0% would be returned if the images were totally different.
I already read a lot of similar topics here on StackOverflow. I also did quite some Googling. Sadly I couldn't come up with a satisfieng answer.
This is a really huge topic, with answers from 3 lines of code to entire research magazines.
I will outline the most common such techniques and their results.
Comparing histograms
One of the simplest & fastest methods. Proposed decades ago as a means to find picture simmilarities. The idea is that a forest will have a lot of green, and a human face a lot of pink, or whatever. So, if you compare two pictures with forests, you'll get some simmilarity between histograms, because you have a lot of green in both.
Template matching
A good example here matchTemplate finding good match. It convolves the search image with the one being search into. It is usually used to find smaller image parts in a bigger one.
Feature matching
Considered one of the most efficient ways to do image search. A number of features are extracted from an image, in a way that guarantees the same features will be recognized again even it is rotated/scaled/skewed. The features extracted this way can be matched against other image feature sets. Another image that has a high proportion of the features in the first one is most probably depicting the same object/scene. It can be used to find the relative difference in shooting angle between pics, or the amount of overlapping.
Sam's solution should be sufficient. I've used combination of both histogram difference and template matching because not one method was working for me 100% of the times. I've given less importance to histogram method though. Here's how I've implemented in simple python script.
A little bit off topic but useful is the pythonic
numpy
approach. Its robust and fast but just does compare pixels and not the objects or data the picture contains (and it requires images of same size and shape):A very simple and fast approach to do this without openCV and any library for computer vision is to norm the picture arrays by
After defining both normed pictures (or matrices) you can just sum over the multiplication of the pictures you like to compare:
1) If you compare similar pictures the sum will return 1:
2) If they aren't similar, you'll get a value between 0 and 1 (a percentage if you multiply by 100):
Please notice that if you have colored pictures you have to do this in all 3 dimensions or just compare a greyscaled version. I often have to compare huge amounts of pictures with arbitrary content and that's a really fast way to do so.
If for matching identical images ( same size/orientation )
Source