OpenCV Android:如何在比较图像上绘制匹配的关键点?

时间:2015-03-23 14:29:31

标签: java android opencv

目前我正在使用OpenCV来比较两张图片,看看它们在Android中是否相似。我正在使用ORB特征检测器和描述符提取器。这是我到目前为止所拥有的。我找到第一张图像中的所有特征关键点,然后找到第二张图像中的所有特征关键点。然后我找到这些关键点的描述符,然后在两个图像之间进行匹配。

private void matchImages() {
    Mat refMat = new Mat();
    Mat srcMat = new Mat();

    Bitmap refBitmap = ((BitmapDrawable) mRefImg.getDrawable()).getBitmap();
    Bitmap srcBitmap = ((BitmapDrawable) mSrcImg.getDrawable()).getBitmap();

    Utils.bitmapToMat(refBitmap, refMat);
    Utils.bitmapToMat(srcBitmap, srcMat);

    MatOfDMatch matches = new MatOfDMatch();
    MatOfDMatch goodMatches = new MatOfDMatch();

    LinkedList<DMatch> listOfGoodMatches = new LinkedList<>();

    LinkedList<Point> refObjectList = new LinkedList<>();
    LinkedList<Point> srcObjectList = new LinkedList<>();

    MatOfKeyPoint refKeypoints = new MatOfKeyPoint();
    MatOfKeyPoint srcKeyPoints = new MatOfKeyPoint();

    Mat refDescriptors = new Mat();
    Mat srcDescriptors = new Mat();

    MatOfPoint2f reference = new MatOfPoint2f();
    MatOfPoint2f source = new MatOfPoint2f();

    FeatureDetector orbFeatureDetector = FeatureDetector.create(FeatureDetector.ORB);
    orbFeatureDetector.detect(refMat, refKeypoints);
    orbFeatureDetector.detect(srcMat, srcKeyPoints);

    DescriptorExtractor descriptorExtractor = DescriptorExtractor.create(DescriptorExtractor.ORB);
    descriptorExtractor.compute(refMat, refKeypoints, refDescriptors);
    descriptorExtractor.compute(srcMat, srcKeyPoints, srcDescriptors);

    DescriptorMatcher matcher = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE_HAMMING);
    matcher.match(refDescriptors, srcDescriptors, matches);

    double max_dist = 0;
    double min_dist = 100;
    List<DMatch> matchesList = matches.toList();

    for (int i = 0; i < refDescriptors.rows(); i++) {
        Double distance = (double) matchesList.get(i).distance;
        if (distance < min_dist) min_dist = distance;
        if (distance > max_dist) max_dist = distance;
    }

    for (int i = 0; i < refDescriptors.rows(); i++) {
        if (matchesList.get(i).distance < 3 * min_dist) {
            listOfGoodMatches.add(matchesList.get(i));
        }
    }

    goodMatches.fromList(listOfGoodMatches);

    List<KeyPoint> refObjectListKeypoints = refKeypoints.toList();
    List<KeyPoint> srcObjectListKeypoints = srcKeyPoints.toList();

    for (int i = 0; i < listOfGoodMatches.size(); i++) {
        refObjectList.addLast(refObjectListKeypoints.get(listOfGoodMatches.get(i).queryIdx).pt);
        srcObjectList.addLast(srcObjectListKeypoints.get(listOfGoodMatches.get(i).trainIdx).pt);
    }

    reference.fromList(refObjectList);
    source.fromList(srcObjectList);

    String result;
    if(listOfGoodMatches.size() > MIN_MATCH_THRESHOLD && listOfGoodMatches.size() < MAX_MATCH_THRESHOLD) {
        result = "They MATCH!";
    } else {
        result = "They DON'T match!";
    }

    AlertDialog alert = new AlertDialog.Builder(this)
            .setMessage(result)
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // close
                }
            }).create();
    alert.show();

    Mat outputImage = new Mat();
    Bitmap comboBmp = combineImages(refBitmap, srcBitmap);
    Utils.bitmapToMat(comboBmp, outputImage);

    Features2d.drawMatches(refMat, refKeypoints, srcMat, srcKeyPoints, goodMatches, outputImage);

    Bitmap bitmap = Bitmap.createBitmap(outputImage.cols(), outputImage.rows(), Bitmap.Config.ARGB_8888);

    Utils.matToBitmap(outputImage, bitmap);
    mRefImg.setImageBitmap(comboBmp);
    mRefImg.invalidate();
    mSrcImg.setImageBitmap(bitmap);
    mSrcImg.invalidate();
}

这只是一个简单的沙盒&#39;我创建的应用程序只是为了测试和使用这个库。如果我比较两个图像,上面代码的结果会产生以下结果:

Result of comparing two images

如您所见,比赛的背景是黑色的。如何在左侧图像上绘制这些匹配?我希望我的结果看起来像一个例子:https://stackoverflow.com/a/14909358/3779845

1 个答案:

答案 0 :(得分:8)

我不确定这对你是否仍然有任何帮助,但我如何修复黑色背景问题而不是使用我使用过的RGBA图像

Imgproc.cvtColor(gabarito, gabaritoRgb, Imgproc.COLOR_RGBA2RGB, 1);
Imgproc.cvtColor(prova, provaRgb, Imgproc.COLOR_RGBA2RGB, 1);

将我的图像转换为RGB,然后我在drawMatched函数中使用了新图像!

Features2d.drawMatches(gabaritoRgb, keypointsGabarito, provaRgb, keypointsProva, matches, 
                    imagemDeSaida);