Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare the similarity of two images with opencv?

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?

like image 258
Frkn Avatar asked Mar 22 '13 14:03

Frkn


People also ask

How do I compare two images in OpenCV?

Python - OpenCV & PyQT5 together To compare two images, we use the Mean Square Error (MSE) of the pixel values of the two images. Similar images will have less mean square error value. Using this method, we can compare two images having the same height, width and number of channels.

How do you compare two images for similarity?

The similarity of the two images is detected using the package “imagehash”. If two images are identical or almost identical, the imagehash difference will be 0. Two images are more similar if the imagehash difference is closer to 0.

How does Python compare images in OpenCV?

Use the norm() Function of OpenCV to Compare Images If the two images that we want to compare have the same size and orientation, we can use the norm() function of OpenCV. This function finds errors present in identical pixels of the two images.


1 Answers

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.

like image 142
andriy Avatar answered Nov 04 '22 04:11

andriy