android - 特征检测后OpenCV Android提取匹配Mat

标签 android opencv

我想提取与我的引用图像匹配的图像部分。
我尝试使用 Calib3d.findHomography 方法转换图像。
完成此操作后,我使用 Imgproc.warpPerspective 进行转换,但没有好的结果。我在这里想念什么吗?
我需要对透视变换做些什么吗?我已经尝试过了,但到目前为止没有任何运气。

这是我的 findSceneCorners 方法:

 private void findSceneCorners(Mat src) {
    mFeatureDetector.detect(src, mSceneKeypoints);
    mDescriptorExtractor.compute(src, mSceneKeypoints, mSceneDescriptors); 
    mDescriptorMatcher.match(mSceneDescriptors, mReferenceDescriptors, mMatches);

    List<DMatch> matchesList = mMatches.toList();
    if (matchesList.size() < 4) {
        // There are too few matches to find the homography.
        return;
    }

    List<KeyPoint> referenceKeypointsList =
            mReferenceKeypoints.toList();
    List<KeyPoint> sceneKeypointsList =
            mSceneKeypoints.toList();

    // Calculate the max and min distances between keypoints.
    double maxDist = 0.0;
    double minDist = Double.MAX_VALUE;
    for(DMatch match : matchesList) {
        double dist = match.distance;
        if (dist < minDist) {
            minDist = dist;
        }
        if (dist > maxDist) {
            maxDist = dist;
        }
    }

    // The thresholds for minDist are chosen subjectively
    // based on testing. The unit is not related to pixel
    // distances; it is related to the number of failed tests
    // for similarity between the matched descriptors.
    if (minDist > 50.0) {
        // The target is completely lost.
        // Discard any previously found corners.
        mSceneCorners.create(0, 0, mSceneCorners.type());
        return;
    } else if (minDist > 20.0) {
        // The target is lost but maybe it is still close.
        // Keep any previously found corners.
        return;
    }


    // Identify "good" keypoints based on match distance.
    ArrayList<Point> goodReferencePointsList =
            new ArrayList<Point>();
    ArrayList<Point> goodScenePointsList =
            new ArrayList<Point>();

    double maxGoodMatchDist = 1.75 * minDist;
    for(DMatch match : matchesList) {
        if (match.distance < maxGoodMatchDist) {
           goodReferencePointsList.add(
                   referenceKeypointsList.get(match.trainIdx).pt);
           goodScenePointsList.add(
                   sceneKeypointsList.get(match.queryIdx).pt);
        }
    }

    if (goodReferencePointsList.size() < 4 ||
            goodScenePointsList.size() < 4) {
        // There are too few good points to find the homography.
        return;
    }

    Log.i(TAG, "Match found");

    MatOfPoint2f goodReferencePoints = new MatOfPoint2f();
    goodReferencePoints.fromList(goodReferencePointsList);

    MatOfPoint2f goodScenePoints = new MatOfPoint2f();
    goodScenePoints.fromList(goodScenePointsList);

    homography = Calib3d.findHomography(goodReferencePoints, goodScenePoints);

    Mat quad = new Mat(mReferenceImage.size(), CvType.CV_32F);
    Imgproc.warpPerspective(src, quad, homography, quad.size());
    objectDetectedListener.objectDetected(quad);

}

最佳答案

我认为你应该使用 WARP_INVERSE_MAP作为warpPerspective的旗帜如:Imgproc.warpPerspective(src, quad, homography, quad.size(),WARP_INVERSE_MAP); .

我没有完全使用您的代码,而只是使用了单应性之后的部分,而且我已经看到镜像中的图像被扭曲了,而不是我们想要的(使用更大的显示图像来确切地查看那里的内容)。实际上,在您发布的页面上,使用 10 卡,它使用了那个标志,抱歉我之前没有想到要提到这个。

关于android - 特征检测后OpenCV Android提取匹配Mat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21087804/

相关文章:

c++ - 如何在 C++ 的 OpenCV 中初始化轮廓

android - 如何在谷歌地图中的折线上方添加文字?

android - 显示自定义适配器内部的进度条或对话框

java - 用于复制 url-webview 的按钮

opencv - 什么最适合您的视频跟踪?为什么?

python - Python 中的图像注视点

android - 在 ProgressDialog 之后显示长任务的结果

android - 如何将 TextView 置于中心?

opencv - opencv中有求解xA=b的函数吗?

python - opencv 在 osx 上安装没有合适的图像错误