在功能检测后,OpenCV Android提取匹配Mat

时间:2014-01-13 09:34:26

标签: android opencv

我想提取与我的参考图像匹配的图像部分。 我尝试使用Calib3d.findHomography方法转换图像。 当我这样做时,我使用Imgproc.warpPerspective进行转换但没有很好的结果。我在这里想念一下吗? 我是否需要使用perspectiveTransform做一些事情?我试过这个但到目前为止没有任何运气。

这是我的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);

}

1 个答案:

答案 0 :(得分:2)

我认为您应该使用WARP_INVERSE_MAP作为warpPerspective的标记:Imgproc.warpPerspective(src, quad, homography, quad.size(),WARP_INVERSE_MAP);

我没有完全使用你的代码,但只是单片后的部分,我已经看到图像在镜子中扭曲,而不是我们想要的(使用更大的显示图像来确切地看到那里有什么)。事实上,在您发布的页面上,使用10张卡片时,会使用该标志,抱歉我之前没有想过提及此内容。