title: Comparing two images with OpenCV in Java
body: <p>I am trying to compare two images (determine whether they are similar or not) using the OpenCV library. I configured the java wrapper and found several tutorials (mostly in C/C++) that I am trying to rewrite into Java. I am using the feature detection approach.</p>
  
  <p>The problem is that the algorithm that I currently have does not produce any reasonable results (it claims that two similar images have nothing in common and finds matches between other two images that are completely different). Could someone suggest how should I use the openCV matcher to produce some reasonable results?</p>
  
  <p>This is my code for the image comparison</p>
  
  <pre><code>private static void compareImages(String path1, String path2) {
      System.out.println(path1 + "-" + path2);
  
      FeatureDetector detector = FeatureDetector.create(FeatureDetector.ORB);
      DescriptorExtractor descriptor = DescriptorExtractor.create(DescriptorExtractor.ORB);
  
      DescriptorMatcher matcher = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE_HAMMING);
  
      // first image
      Mat img1 = Imgcodecs.imread(path1, Imgcodecs.CV_LOAD_IMAGE_GRAYSCALE);
      Mat descriptors1 = new Mat();
      MatOfKeyPoint keypoints1 = new MatOfKeyPoint();
  
      detector.detect(img1, keypoints1);
      descriptor.compute(img1, keypoints1, descriptors1);
  
      // second image
      Mat img2 = Imgcodecs.imread(path2, Imgcodecs.CV_LOAD_IMAGE_GRAYSCALE);
      Mat descriptors2 = new Mat();
      MatOfKeyPoint keypoints2 = new MatOfKeyPoint();
  
      detector.detect(img2, keypoints2);
      descriptor.compute(img2, keypoints2, descriptors2);
  
      // match these two keypoints sets
      MatOfDMatch matches = new MatOfDMatch();
      matcher.match(descriptors1, descriptors2, matches);
  
      for (DMatch m : matches.toArray()) {
        // how to use these values to detect the similarity? They seem to be way off
        // all of these values are in range 50-80 which seems wrong to me
        System.out.println(m.distance);
      }
    }
  </code></pre>
  
  <p>Unfortunately, algorithms like SURF and SIFT are not available in the java wrapper so I am using ORB. I have little to no experience with computer vision, I am just trying to get this simple comparison algorithm work to produce some reasonable outcome. I would be glad for any help!</p>
  
  <p>EDIT: My use-case is running this algorithm against images taken from different angles. I updated my code to be better formatted.</p>
  
  <p>Sample images to compare:</p>
  
  <p><a href="https://i.stack.imgur.com/8nztW.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/8nztW.jpg" alt="enter image description here"></a>
  <a href="https://i.stack.imgur.com/xfZM8.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/xfZM8.jpg" alt="enter image description here"></a></p>