OpenCV - Java - 使用DescriptorMatcher与2个相反的图像不匹配

Avi*_*sho 6 java opencv

我试图使用OpenCV的DescriptorMatcher匹配2个相反的图像而没有运气.图像是:http://i61.tinypic.com/28whu0g.jpg(从左到右)和http://i61.tinypic.com/x35vte.jpg(从右到左).

我的代码很像我在StackOverflow和Web中看到的很多例子,但我总是得不到匹配.

        String firstImageSourcePath = "RTL_IMAGE_PATH";
        String secondImageSourcePath = "LTR_IMAGE_PATH";

        Mat firstImageSrcImgMat = Highgui.imread(firstImageSourcePath);
        Mat secondImageSrcImgMat = Highgui.imread(firstImageSourcePath);

        if (firstImageSrcImgMat.empty() || secondImageSrcImgMat.empty()) {
            System.out.println("Failed to load images");
            return;
        }

        System.out.println("Loaded image at " + firstImageSourcePath + " and " + secondImageSourcePath);

        FeatureDetector featureDetector = FeatureDetector.create(FeatureDetector.BRISK);

        MatOfKeyPoint firstImgMatOfKeyPoints = new MatOfKeyPoint();
        MatOfKeyPoint secondImgMatOfKeyPoints = new MatOfKeyPoint();

        featureDetector.detect(firstImageSrcImgMat, firstImgMatOfKeyPoints);
        featureDetector.detect(secondImageSrcImgMat, secondImgMatOfKeyPoints);

        System.out.println("Detected " + firstImgMatOfKeyPoints.size() + " and " + secondImgMatOfKeyPoints + " blobs in the images");

        List<KeyPoint> firstImgKeyPoints = firstImgMatOfKeyPoints.toList();
        List<KeyPoint> secondImgKeyPoints = secondImgMatOfKeyPoints.toList();

        System.out.println("First Image key points: " + firstImgKeyPoints);
        System.out.println("Second Image key points: " + secondImgKeyPoints);

        Mat firstImgDescriptors = new Mat();
        Mat secondImgDescriptors = new Mat();

        DescriptorExtractor extractor = DescriptorExtractor.create(DescriptorExtractor.BRISK); 
        extractor.compute(firstImageSrcImgMat, firstImgMatOfKeyPoints, firstImgDescriptors);
        extractor.compute(secondImageSrcImgMat, secondImgMatOfKeyPoints, secondImgDescriptors);

        System.out.println("descriptorsA.size() : " + firstImgDescriptors.size());
        System.out.println("descriptorsB.size() : " + secondImgDescriptors.size());

        MatOfDMatch matches = new MatOfDMatch();

        DescriptorMatcher matcher = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE_HAMMINGLUT); // BRUTEFORCE_HAMMINGLUT
        matcher.match(firstImgDescriptors, secondImgDescriptors, matches);

        System.out.println("matches.size() : " + matches.size());
        System.out.println("matches : " + matches);

        MatOfDMatch matchesFiltered = new MatOfDMatch();

        List<DMatch> matchesList = matches.toList();
        List<DMatch> bestMatches = new ArrayList<DMatch>();

        Double max_dist = 0.0;
        Double min_dist = 100.0;

        for (int i = 0; i < matchesList.size(); i++) {
            Double dist = (double) matchesList.get(i).distance;

            if (dist > 0)
                System.out.println("dist : " + dist);

            if (dist < min_dist && dist != 0) {
                min_dist = dist;
            }

            if (dist > max_dist) {
                max_dist = dist;
            }

        }

        System.out.println("max_dist : " + max_dist);
        System.out.println("min_dist : " + min_dist);

        if (min_dist > 50) {
            System.out.println("No match found, min_dist under minimum value");
            return;
        }

        double threshold = 3 * min_dist;
        double threshold2 = 2 * min_dist;

        if (threshold > 75) {
            threshold = 75;
        } else if (threshold2 >= max_dist) {
            threshold = min_dist * 1.1;
        } else if (threshold >= max_dist) {
            threshold = threshold2 * 1.4;
        }

        System.out.println("Threshold : " + threshold);

        for (int i = 0; i < matchesList.size(); i++) {
            Double dist = (double) matchesList.get(i).distance;

            if (dist < threshold) {
                bestMatches.add(matches.toList().get(i));
                System.out.println(String.format(i + " best match added : %s", dist));
            }
        }

        matchesFiltered.fromList(bestMatches);

        System.out.println("matchesFiltered.size() : " + matchesFiltered.size());

        if (matchesFiltered.rows() >= 1) {
            System.out.println("match found");
        } else {
            System.out.println("match not found");
        }
Run Code Online (Sandbox Code Playgroud)

什么提示我做错了什么?

zed*_*edv 5

正如@ Iwillnotexist-Idonotexist所说,第一个问题是你申请的门槛.尝试使用不依赖于描述符之间距离的阈值,因为某些描述符比其他描述符更具辨别力.我认为这会给你更好的结果.我建议你使用D. Lowe在SIFT论文中提出的比率测试.请查看第7.1节:http://cs.ubc.ca/~lowe/papers/ijcv04.pdf

第二个问题是您使用BRISK来检测图像中的功能.这个OpenCV实现有bug(你可以在这里查看:http://code.opencv.org/issues/3976)所以尝试使用另一个FeatureDetector,如FAST,ORB等...(描述符很好所以你可以继续使用它)

我最终测试了你的照片,我设法用不同的探测器/描述符获得了一些结果:(没有匹配的关键点 - >黄色)

BRISK探测器和描述符: BRISK/BRISK

  • 左图像关键点:74
  • 正确的图像关键点:86
  • 匹配:3(即使有破碎的探测器,我得到了匹配)

带有BRISK作为描述符的ORB检测器: ORB/BRISK

  • 左图像关键点:499
  • 右图关键点:500
  • 比赛:26

ORB检测器和描述符 使用ORB

  • 左图像关键点:841
  • 右图关键点:907
  • 比赛:43

使用比率测试获得所有结果以去除错误匹配.我希望这有帮助!

编辑:

BruteForceMatcher<Hamming> matcher;
vector< vector<DMatch> > matches;
vector <DMatch> goodMatches;
matcher.knnMatch(imgDescriptors1, imgDescriptors2, matches, 2);
// Ratio Test
for (unsigned int matchIdx = 0; matchIdx < matches.size(); ++matchIdx) 
{
    const float ratio = 0.8; // As in Lowe's paper (can be tuned)
    if (matches[matchIdx][0].distance < ratio * matches[matchIdx][1].distance)
    {
        goodMatches.push_back(matches[matchIdx][0]);
    }
}
Run Code Online (Sandbox Code Playgroud)