title: Android OpenCV FeatureDetection -> runFeatureHomography giving messed up result image after matching
body: <p>I'm pretty new to openCV, and at the moment im tryin to get an object detection running on an android device. What i basically do, is displaying a a camera preview in my app, and when i click on it it captures a picture. Then this picture is given to the runFeatureHomography - method, which first grabs the second image to which the taken image has to be compared. Then the method finds the keypoints in both pictures, computes them and matches them into one Mat called img_matches. 
  As basic as it can be i guess.</p>
  
  <p>The object im trying to detect here at the moment is some kind of card, just like the format of a credit card. The card is blue and has lots of white and yellow text on it. I can only post one link, thats why i cant show pictures of them.</p>
  
  <p>I dont know why, but when i display the result in the end / or save the result as a bitmap to my phone, it always looks kinda like this:</p>
  
  <p><a href="http://oi44.tinypic.com/oaqel0.jpg" rel="nofollow">http://oi44.tinypic.com/oaqel0.jpg</a> &lt;-- result image after everything is done.</p>
  
  <p>This shows me that the object i wanted to detect was indeed detected, but i dont know why there is a black background and not the pictures of the cards. Why doesn't it show my two images the way the are, just with all the lines on them?</p>
  
  <p>In my code im using those three:</p>
  
  <pre><code>FeatureDetector detector = FeatureDetector.create(FeatureDetector.ORB);
  DescriptorExtractor extractor = DescriptorExtractor.create(DescriptorExtractor.ORB);
  DescriptorMatchermatcher = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE);
  </code></pre>
  
  <p>Here is some of my code:</p>
  
  <pre><code>private void runFeatureHomography(Bitmap image)
  {
  
      Mat img_object = getObjectImage();
      Mat img_scene = newEmptyMat();
  
      Bitmap myimg = image.copy(Bitmap.Config.ARGB_8888, true);
  
      Utils.bitmapToMat(myimg, img_scene);
  
      MatOfKeyPoint keyPoints_object = detectObjectKeyPoints();
      MatOfKeyPoint keyPoints_scene = new MatOfKeyPoint();
  
      this.detector.detect(img_scene, keyPoints_scene);
  
      Mat descriptors_object = calculateObjectDescriptor();
      Mat descriptors_scene = newEmptyMat();
  
      this.extractor.compute(img_scene, keyPoints_scene, descriptors_scene);
  
      MatOfDMatch matches = new MatOfDMatch();
  
      matcher.match(descriptors_object, descriptors_scene, matches);
  
      double min_dist = Double.MAX_VALUE;
      for (int i = -1; ++i &lt; descriptors_object.rows();)
      {
          double dist = matches.toArray()[i].distance;
          if (dist &lt; min_dist)
          {
              min_dist = dist;
          }
      }
  
  
      List&lt;DMatch&gt; good_matches = new ArrayList&lt;DMatch&gt;();
      for (int i = -1; ++i &lt; descriptors_object.rows();)
      {
          if (matches.toArray()[i].distance &lt;= 3 * min_dist)
          {
              good_matches.add(matches.toArray()[i]);
          }
      }
  
      System.out.println("4");
      Mat img_matches = newEmptyMat();
      Features2d.drawMatches(
              img_object,
              keyPoints_object,
              img_scene,
              keyPoints_scene,
              new MatOfDMatch(good_matches.toArray(new DMatch[good_matches
                      .size()])), img_matches, Scalar.all(-1),
              Scalar.all(-1), new MatOfByte(),
              Features2d.NOT_DRAW_SINGLE_POINTS);
  
      List&lt;Point&gt; object = new ArrayList&lt;Point&gt;();
      List&lt;Point&gt; scene = new ArrayList&lt;Point&gt;();
      for (int i = -1; ++i &lt; good_matches.size();)
      {
          object.add(keyPoints_object.toArray()[good_matches.get(i).queryIdx].pt);
          scene.add(keyPoints_scene.toArray()[good_matches.get(i).trainIdx].pt);
      }
  
      Mat H = Calib3d.findHomography(
              new MatOfPoint2f(object.toArray(new Point[object.size()])),
              new MatOfPoint2f(scene.toArray(new Point[scene.size()])),
              Calib3d.RANSAC, 3);
  
      Point[] object_corners = new Point[4];
      object_corners[0] = new Point(0, 0);
      object_corners[1] = new Point(img_object.cols(), 0);
      object_corners[2] = new Point(img_object.cols(), img_object.rows());
      object_corners[3] = new Point(0, img_object.rows());
  
      MatOfPoint2f scene_corners2f = new MatOfPoint2f();
      Core.perspectiveTransform(new MatOfPoint2f(object_corners),
              scene_corners2f, H);
  
      Point[] scene_corners = scene_corners2f.toArray();
      Point[] scene_corners_norm = new Point[4];
      scene_corners_norm[0] = new Point(scene_corners[0].x
              + img_object.cols(), scene_corners[0].y);
      scene_corners_norm[1] = new Point(scene_corners[1].x
              + img_object.cols(), scene_corners[1].y);
      scene_corners_norm[2] = new Point(scene_corners[2].x
              + img_object.cols(), scene_corners[2].y);
      scene_corners_norm[3] = new Point(scene_corners[3].x
              + img_object.cols(), scene_corners[3].y);
      Core.line(img_matches, scene_corners_norm[0], scene_corners_norm[1],
              new Scalar(0, 255, 0), 4);
      Core.line(img_matches, scene_corners_norm[1], scene_corners_norm[2],
              new Scalar(0, 255, 0), 4);
      Core.line(img_matches, scene_corners_norm[2], scene_corners_norm[3],
              new Scalar(0, 255, 0), 4);
      Core.line(img_matches, scene_corners_norm[3], scene_corners_norm[0],
              new Scalar(0, 255, 0), 4);
  
      bmp = Bitmap.createBitmap(img_matches.cols(), img_matches.rows(),
              Bitmap.Config.ARGB_8888);
  
  
       Intent resultIntent = new Intent("com.example.capturetest.Result");
       startActivity(resultIntent);
  
  }
  
  private volatile Mat cachedObjectDescriptor = null;
  private volatile MatOfKeyPoint cachedObjectKeyPoints = null;
  private volatile Mat cachedObjectImage = null;
  
  private Mat calculateObjectDescriptor()
  {
      Mat objectDescriptor = this.cachedObjectDescriptor;
      if (objectDescriptor == null)
      {
          Mat objectImage = getObjectImage();
          MatOfKeyPoint objectKeyPoints = detectObjectKeyPoints();
          objectDescriptor = newEmptyMat();
          this.extractor.compute(objectImage, objectKeyPoints,
                  objectDescriptor);
          this.cachedObjectDescriptor = objectDescriptor;
      }
      return objectDescriptor;
  }
  
  private MatOfKeyPoint detectObjectKeyPoints()
  {
      MatOfKeyPoint objectKeyPoints = this.cachedObjectKeyPoints;
      if (objectKeyPoints == null)
      {
          Mat objectImage = getObjectImage();
          objectKeyPoints = new MatOfKeyPoint();
          this.detector.detect(objectImage, objectKeyPoints);
          this.cachedObjectKeyPoints = objectKeyPoints;
      }
      return objectKeyPoints;
  }
  
  private Mat getObjectImage()
  {
      Mat objectImage = this.cachedObjectImage;
      if (objectImage == null)
      {
          objectImage = newEmptyMat();
          Bitmap bitmap = ((BitmapDrawable) iv.getDrawable()).getBitmap();
          Bitmap img = bitmap.copy(Bitmap.Config.ARGB_8888, false);
          Utils.bitmapToMat(img, objectImage);
  
          this.cachedObjectImage = objectImage;
      }
      return objectImage;
  }
  
  private Mat newEmptyMat()
  {
      return new Mat();
  }
  </code></pre>
  
  <p>After this line matcher.match(descriptors_object, descriptors_scene, matches); i tried to convert the three Mat img_object, img_scene and matches to bitmaps and saved them to my android device just for checking. They all look as they are supposed to look, so until this point everthing is fine.</p>
  
  <p>But after this part...</p>
  
  <pre><code> Mat img_matches = newEmptyMat();
      Features2d.drawMatches(
              img_object,
              keyPoints_object,
              img_scene,
              keyPoints_scene,
              new MatOfDMatch(good_matches.toArray(new DMatch[good_matches
                      .size()])), img_matches, Scalar.all(-1),
              Scalar.all(-1), new MatOfByte(),
              Features2d.NOT_DRAW_SINGLE_POINTS);
  </code></pre>
  
  <p>... i tried to convert the Mat img_matches (which is supposed to have all the information of the two input pictures if i get it right), to a bitmap and save it on my android device, but the picture looks like the picture in the link above (black pictures with lines instead of card-pictures with lines).</p>
  
  <p>Does any of you know what im doing wrong here? I seem to be stuck at the moment.</p>
  
  <p>Thanks in advance guys.</p>
  
  <p>Edit:</p>
  
  <p>Just wanted to let you know that i got the same code running and WORKING as a normal java program on my desktop. The picture is taken from the webcam there. The result image is diplayed absolutely correct in the desktop program, with cards and lines instead of black and lines ;)</p>