4

我正在创建一个小程序来检测大图像中的对象(小图像),我正在使用 OpenCV java。由于我必须考虑旋转和缩放,我使用了 FeatureDetector.BRISK 和 DescriptorExtractor.BRISK。

以下方法用于过滤匹配结果以仅获得最佳匹配。

我有两个问题

  1. 有没有办法用我用过的循环找到下面的 min_dist 和 max_dist ?
  2. 最重要的问题 - 现在的问题是我需要使用这些匹配来确定是否找到了对象(模板)。如果有人在这里帮助我,那就太好了。

提前致谢。

    FeatureDetector  fd = FeatureDetector.create(FeatureDetector.BRISK); 
    final MatOfKeyPoint keyPointsLarge = new MatOfKeyPoint();
    final MatOfKeyPoint keyPointsSmall = new MatOfKeyPoint();

    fd.detect(largeImage, keyPointsLarge);
    fd.detect(smallImage, keyPointsSmall);

    System.out.println("keyPoints.size() : "+keyPointsLarge.size());
    System.out.println("keyPoints2.size() : "+keyPointsSmall.size());

    Mat descriptorsLarge = new Mat();
    Mat descriptorsSmall = new Mat();

    DescriptorExtractor extractor = DescriptorExtractor.create(DescriptorExtractor.BRISK);
    extractor.compute(largeImage, keyPointsLarge, descriptorsLarge);
    extractor.compute(smallImage, keyPointsSmall, descriptorsSmall);

    System.out.println("descriptorsA.size() : "+descriptorsLarge.size());
    System.out.println("descriptorsB.size() : "+descriptorsSmall.size());

    MatOfDMatch matches = new MatOfDMatch();

    DescriptorMatcher matcher = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE_HAMMINGLUT);
    matcher.match(descriptorsLarge, descriptorsSmall, matches);

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

    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 < 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);

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

    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;
        System.out.println(String.format(i + " match distance best : %s", dist));
        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());

编辑

编辑我的代码如下。我知道这仍然不是根据没有最佳匹配来得出是否找到对象的最佳方法。所以请分享你的观点。

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

    if(min_dist > 50 )
    {
        System.out.println("No match found");
        System.out.println("Just return ");
        return false;
    }

    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");
        return true;
    }
    else
    {
        return false;
    }
4

2 回答 2

1

您编辑的代码对我来说运行良好,并且运行良好,

以下是我在您的代码中为检测大图像中的对象(小图像)所做的更改:

  1. 使用 SURF 方法进行特征检测和特征提取。(SURF 在适用于 Android 及更早版本的 opencv 4.1.1 中可用,之后已将其删除,所以这里我使用了 opencv 4.1.1)

  2. 在下一行中将匹配或不匹配的图像阈值从 1 更改为 4

    if(matchesFiltered.rows() >= 1)

if(matchesFiltered.rows() >= 4)

只有这些更改对我来说效果很好,确保对象/小图像具有丰富的纹理(至少应该有可以匹配的关键点)

于 2014-01-08T11:18:17.863 回答
0

有几种方法可以检测图像中的对象。只需在此处放置一些链接:

最后一个链接显示了一种计算最小值和最大值的方法,在 Java 中应该几乎相同。希望所有链接都显示一些如何匹配对象的想法。

我还认识到您的代码中有很多神奇的数字。也许您可以将它们放入变量中以减少出错的可能性并获得更好的概览。

于 2013-07-27T23:03:42.197 回答