8

I want to check two images are similar or different with opencv.

if they are same printf("same");

if they are not same printf("not same");

is there any method or way for that in opencv?

3

1 Answer 1

14

It is not so easy task, and it is impossible to do with one if. What I recommend is to match image's interest points. Basically you can use opencv library to identify interest points on images and perform the match of them. If the percentage of the match is high enough, you can conclude that images are the same. This percentage in most cases depends of kind of images which you want to match. It means that you need to adjust the value of the acceptance percentage.

To perform fingerprint matching you can use ORB,FREAK,BRISK,SURF algorithms. But I recommend you to use ORB. You can read more about this here.

Here is some tips how you can do it with OpenCV for Java:

//Load images to compare Mat img1 = Highgui.imread(filename1, Highgui.CV_LOAD_IMAGE_GRAYSCALE); Mat img2 = Highgui.imread(filename1, Highgui.CV_LOAD_IMAGE_GRAYSCALE); MatOfKeyPoint keypoints1 = new MatOfKeyPoint(); MatOfKeyPoint keypoints2 = new MatOfKeyPoint(); Mat descriptors1 = new Mat(); Mat descriptors2 = new Mat(); //Definition of ORB keypoint detector and descriptor extractors FeatureDetector detector = FeatureDetector.create(FeatureDetector.ORB); DescriptorExtractor extractor = DescriptorExtractor.create(DescriptorExtractor.ORB); //Detect keypoints detector.detect(img1, keypoints1); detector.detect(img2, keypoints2); //Extract descriptors extractor.compute(img1, keypoints1, descriptors1); extractor.compute(img2, keypoints2, descriptors2); //Definition of descriptor matcher DescriptorMatcher matcher = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE_HAMMING); //Match points of two images MatOfDMatch matches = new MatOfDMatch(); matcher.match(descriptors1,descriptors2 ,matches); 

Note that it is a quite basic image matcher. How to make it better you should investigate it according to images that you want to match. Also take a look to Good Matches method, which you can find here.

Sign up to request clarification or add additional context in comments.

4 Comments

thank you for your answer. Just wondering, what to do next, after we've got the matches. There are always matches, that means, we need filter some of them or validate them.
oh, for so long I didn't work on this topic. I can't give a code example right now, but the idea is to calculate which amount of points matched and then verify it with defined threshold. For example if 80% of points matched correctly, you have a match. But now, the threshold depends of your needs...can be less or more. You can draw them as well: check this link.
thank you for your answer. I tried thie hier stackoverflow.com/questions/17898480/… but somehow it works not quite right. Too many false positives. And how to decide if points are matched correctly?!
It may work "not quite right" because of several factors. Filters you apply on image before matching, matching algorithm method with which you decide when it is a match. About filters and algorithm you can check my old post and for good matches you can check this one. But I suggest to do more research, as these answers are old and you my find something better. Good luck! I'd also wish to work on it right now!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.