java - OpenCV 安卓 : How to draw matching key points over the compared images?

标签 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();
}

这只是我创建的一个简单的“沙盒”应用程序,用于测试和使用该库。如果我比较两张图片,上述代码的结果会产生以下结果:

Result of comparing two images

如您所见,火柴的背景是黑色的。我怎样才能在左边的图像上绘制这些火柴?我希望我的结果看起来像一个例子:https://stackoverflow.com/a/14909358/3779845

最佳答案

我不确定这是否对你有任何帮助,但我解决黑色背景问题的方法不是使用我使用的 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);

关于java - OpenCV 安卓 : How to draw matching key points over the compared images?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29212847/

相关文章:

android - 在按钮内制作进度条

python-3.x - cv2.waitKey 为所有键返回 255

python - 使用 OpenCV 和 Python 多处理进行持续相机抓取

java - 当包在 pom.xml 中列为依赖项时,为什么我收到 ClassNotFoundException

java - 使用 foreach 循环更改二维数组中的值

java - 如何允许 gradle 插件访问项目依赖项中的类

java - 没有可用的 'org.springframework.data.rest.core.util.Java8PluginRegistry' 类型的合格 bean

android - Gradle 产品调味品未创建包装

Android 文本转语音 - 突出显示说出的单词

python - MacPorts 安装后,Python 中的 OpenCV 模块失败(退出代码 139)