Compare the similarity of two images with opencv?

前端 未结 1 1388
无人共我
无人共我 2021-02-04 17:53

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

相关标签:
1条回答
  • 2021-02-04 18:08

    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.

    0 讨论(0)
提交回复
热议问题