title: OpenCV image comparison in Android
body: <p><b>[EDIT]</b>
  I have devised some code for image comparison. The matching part is still a bit flawed and I would love some assitance. The project can be found at - <a href="http://github.com/torcellite/imageComparator" rel="nofollow noreferrer">GitHub</a>.</p>
  
  <p>I have these two images <b>Img1</b> and <b>Img2</b>:</p>
  
  <p><img src="https://i.stack.imgur.com/p7gjt.jpg" alt="enter image description here"> <img src="https://i.stack.imgur.com/FGXrZ.jpg" alt="enter image description here"></p>
  
  <p>When I use the following command in openCV</p>
  
  <pre><code>Mat img1 = Highgui.imread("mnt/sdcard/IMG-20121228.jpg");
  Mat img2 = Highgui.imread("mnt/sdcard/IMG-20121228-1.jpg");
  
  try{
      double l2_norm = Core.norm( img1, img2 );
      tv.setText(l2_norm+"");
  } catch(Exception e) {
      //image is not a duplicate
  }
  </code></pre>
  
  <p>I get a double value for l2_norm. This double value varies for duplicate image pairs. But if the images are different, then an exception is thrown. Is this how I identify duplicate images? Or is there a better method? I've Googled extensively and couldn't find a really convincing answer. I would like the code and explanation as to how I'd compare two images and get a boolean value of <code>true</code> or <code>false</code> depending upon the images.</p>
  
  <p><b>EDIT</b></p>
  
  <pre><code>Scalar blah= Core.sumElems(img2);
      Scalar blah1=Core.sumElems(img1);
  
      if(blah.equals(blah1))
      {
          tv.setText("same image");
      }
      }
  </code></pre>
  
  <p>I've tried this, but the <code>if</code> condition is never satisfied. I'm assuming there are a few differences, but there is no <code>compare</code> function for <code>Scalar</code>. What do I do?</p>
  
  <p><b>EDIT</b></p>
  
  <pre><code>try{
      Scalar blah= Core.sumElems(img2);
      Scalar blah1=Core.sumElems(img1);
      String b=blah.toString();
      String b1=blah1.toString();
      System.out.println(b+" "+b1);
      double comp=b.compareTo(b1);
      tv.setText(""+comp);
      }
  </code></pre>
  
  <p>This method is again flawed. Although it can be used to compare images with a decent accuracy, it fails when images are of different sizes.</p>
  
  <p>When images are of different sizes and I print the scalar values I get this:</p>
  
  <p><code>[9768383.0, 1.0052889E7, 1.0381814E7, 0.0] [1.5897384E7, 1.6322252E7, 1.690251E7, 0.0]</code></p>
  
  <p>The variation between the second and third numbers although not much is quite large compared to when the images of same size are compared. The first number however suffers the most change.</p>
  
  <p>What would be the best fastest way to compare the contents of two images?</p>
  
  <p><b>[EDIT]</b></p>
  
  <p>I'm using the code I found <a href="https://stackoverflow.com/questions/10691521/surf-description-faster-with-fast-detection">here</a>.</p>
  
  <p>What I'm not able to figure out is how to initialize the <code>MatOfKeyPoint</code> variables <code>keypoints</code> and <code>logoKeypoints</code>. Here's my code snippet:</p>
  
  <pre><code>           FeatureDetector detector = FeatureDetector.create(FeatureDetector.SURF);
          //FeatureDetector detector = FeatureDetector.create(FeatureDetector.FAST);
          //Imgproc.cvtColor(img1, img1, Imgproc.COLOR_RGBA2RGB);
          //Imgproc.cvtColor(img2, img2, Imgproc.COLOR_RGBA2RGB);
  
          DescriptorExtractor SurfExtractor = DescriptorExtractor
          .create(DescriptorExtractor.SURF);
  
  
          //extract keypoints
          MatOfKeyPoint keypoints, logoKeypoints;
          long time= System.currentTimeMillis();
          detector.detect(img1, keypoints);
          Log.d("LOG!", "number of query Keypoints= " + keypoints.size());
          detector.detect(img2, logoKeypoints);
          Log.d("LOG!", "number of logo Keypoints= " + logoKeypoints.size());
          Log.d("LOG!", "keypoint calculation time elapsed" + (System.currentTimeMillis() -time));
  
          //Descript keypoints
          long time2 = System.currentTimeMillis();
          Mat descriptors = new Mat();
          Mat logoDescriptors = new Mat();
          Log.d("LOG!", "logo type" + img2.type() + "  intype" + img1.type());
          SurfExtractor.compute(img1, keypoints, descriptors);
          SurfExtractor.compute(img2, logoKeypoints, logoDescriptors);
          Log.d("LOG!", "Description time elapsed" + (System.currentTimeMillis()- time2));
  </code></pre>
  
  <p>I obviously can't initialize the variables <code>keypoints</code> and <code>logoKeypoints</code> to null cuz I'll receive a null pointer exception then. How do I initialize them?</p>