16

我正在创建一个应用程序来查找两个图像之间的匹配。我无法正确找到匹配结果。

匹配方法给了我与输入关键点相同数量的描述符,我也无法得出这个结果。我在工作区中使用 OpenCV 作为库。

这是我的代码。

  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();
4

1 回答 1

1

如果解决方案被锁定,我建议您使用 Android NDK 和 C++ 原生代码。它工作正常。本教程很好地解释了为 android 做 jni-opencv 项目的步骤

于 2013-11-13T12:02:57.003 回答