Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java OpenCV Android:如何在比较的图像上绘制匹配的关键点?_Java_Android_Opencv - Fatal编程技术网

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

Java OpenCV Android:如何在比较的图像上绘制匹配的关键点?,java,android,opencv,Java,Android,Opencv,目前,我正在使用OpenCV比较两幅图像,看看它们在Android中是否相似。我正在使用ORB特征检测器和描述符提取器。以下是我到目前为止的情况。我在第一幅图像中找到所有特征关键点,然后在第二幅图像中找到所有特征关键点。然后我找到这些关键点的描述符,然后在两幅图像之间进行匹配 private void matchImages() { Mat refMat = new Mat(); Mat srcMat = new Mat(); Bitmap refBitmap = ((

目前,我正在使用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();
}
private void matchImages(){
材料参考材料=新材料();
Mat srcMat=新Mat();
位图参考位图=((BitmapDrawable)mRefImg.getDrawable()).getBitmap();
位图srcBitmap=((BitmapDrawable)mSrcImg.getDrawable()).getBitmap();
位图编辑器(refBitmap,refMat);
bitmapToMat(srcBitmap,srcMat);
MatOfDMatch matches=新的MatOfDMatch();
MatOfDMatch GOODMATCHS=新的MatOfDMatch();
LinkedList listOfGoodMatches=新建LinkedList();
LinkedList重新对象列表=新建LinkedList();
LinkedList srcObjectList=新建LinkedList();
MatOfKeyPoint refKeypoints=新的MatOfKeyPoint();
MatOfKeyPoint srcKeyPoints=新的MatOfKeyPoint();
材料参考描述符=新材料();
Mat srcDescriptors=新Mat();
MatOfPoint2f参考=新的MatOfPoint2f();
MatOfPoint2f源=新的MatOfPoint2f();
FeatureDetector或FeatureDetector=FeatureDetector.create(FeatureDetector.ORB);
orbFeatureDetector.detect(参考材料、参考关键点);
orbFeatureDetector.detect(srcMat、srcKeyPoints);
DescriptorExtractor DescriptorExtractor=DescriptorExtractor.create(DescriptorExtractor.ORB);
descriptor.compute(refMat、refKeypoints、refdescriptor);
descriptorExtractor.compute(srcMat、srcKeyPoints、srcDescriptor);
DescriptorMatcher matcher=DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE_HAMMING);
matcher.match(refdescriptor,srcsdescriptor,matches);
双最大距离=0;
双最小距离=100;
List matchesList=matches.toList();
for(int i=0;i最大距离)最大距离=距离;
}
for(int i=0;iMIN\u MATCH\u阈值和listOfGoodMatches.size()
这只是我创建的一个简单的“沙盒”应用程序,只是为了测试和使用这个库。如果比较两幅图像,上述代码的结果将产生以下结果:


如你所见,比赛的背景是黑色的。如何在左侧的图像上绘制这些匹配?我希望我的结果是什么样的示例如下:

我不确定这是否对您仍然有帮助,但我是如何修复黑色背景问题的,而不是使用我使用的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);