17

I am creating an application for finding matches between two images. I am not able to properly find match results.

Matching methods give me the same number of descriptors as the input keypoints and I am also unable to draw this result. I am using OpenCV as a library in the workspace.

Here is my code.

  Bitmap mBitmap1 = mimage1.copy(Bitmap.Config.ARGB_8888, false); 
  Bitmap mBitmap2 = mimage2.copy(Bitmap.Config.ARGB_8888, false); 

  Mat s_image1 = Utils.bitmapToMat(mBitmap1);
  Mat s_image2 = Utils.bitmapToMat(mBitmap2);

  Mat rgb1 = new Mat();
  Mat rgb2 = new Mat();
  Mat rgb3 = new Mat();
  Mat temp = new Mat();

  Mat o_image1 = new Mat();
  Mat o_image2 = new Mat();
  Mat o_image3 = new Mat();

  List<KeyPoint> points1 = new ArrayList<KeyPoint>();
  List<KeyPoint> points2 = new ArrayList<KeyPoint>();
  List<DMatch> matches = new ArrayList<DMatch>();

  FeatureDetector surf = FeatureDetector.create(FeatureDetector.SURF);
  surf.detect(s_image1, points1);
  surf.detect(s_image2, points2);

  Scalar color1 = new Scalar(0,255,0);
  Scalar color2 = new Scalar(255,0,0);

  Imgproc.cvtColor(s_image1, rgb1, Imgproc.COLOR_RGBA2RGB);
  Imgproc.cvtColor(s_image2, rgb2, Imgproc.COLOR_RGBA2RGB);

  Mat descriptors1 = new Mat(), descriptors2 = new Mat();
  Features2d.drawKeypoints(rgb1, points1, rgb1, color2);
  Features2d.drawKeypoints(rgb2, points2, rgb2, color2);
  DescriptorExtractor extracter = DescriptorExtractor.create(DescriptorExtractor.SURF);

  extracter.compute(rgb1, points1, descriptors1);
  extracter.compute(rgb2, points2, descriptors2);
  int k = 5;
  DescriptorMatcher matcher = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE);
  matcher.match(descriptors2, descriptors1, matches);
  Features2d.drawMatches(rgb1, points1, rgb2, points2, matches, rgb3, color1, color2);
  Imgproc.cvtColor(rgb1, o_image1, Imgproc.COLOR_RGB2RGBA);
  Imgproc.cvtColor(rgb2, o_image2, Imgproc.COLOR_RGB2RGBA);

  Utils.matToBitmap(o_image1, mBitmap1);
  mimageview1.setImageBitmap(mBitmap1);
  Utils.matToBitmap(o_image2, mBitmap2);
  mimageview2.setImageBitmap(mBitmap2);
  Utils.matToBitmap(o_image3, mBitmap3);
  mimageview3.setImageBitmap(mBitmap3);
  s_image1.release();
  s_image2.release(); 
  o_image1.release();
  o_image2.release();
Alexis King
  • 43,109
  • 15
  • 131
  • 205
user1296460
  • 171
  • 5
  • Your program looks fine, have you tried to debug? Just to see that every step is right, to see the number of keypoints in each image. I use vector instead of list. – Jav_Rock Oct 02 '12 at 14:44
  • Which opencv version are you using, I do not see that sequence of parameters with Features2d.drawMatches() with opencv 2.4.1. I use List, so that is not an issue. – Rui Marques Oct 02 '12 at 14:59
  • 2.4.x either has Features2d.drawMatches(img1, keypoints1, img2, keypoints2, matches1to2, outImg) OR Features2d.drawMatches(img1, keypoints1, img2, keypoints2, matches1to2, outImg, matchColor, singlePointColor, matchesMask, flags). – Rui Marques Oct 02 '12 at 14:59
  • use Config.RGB_565 rather Bitmap.Config.ARGB_8888 and try. – Biraj Zalavadia Oct 23 '13 at 05:12

1 Answers1

0

In case of locked solution, I suggest you to use Android NDK and C++ native codes. it works properly. This tutorial is well explaining the step to do a jni-opencv project for android

Y.AL
  • 1,808
  • 13
  • 27