【问题标题】:FlannBased Matcher Assertion Failed Error基于 Flann 的匹配器断言失败错误
【发布时间】:2012-06-21 09:58:55
【问题描述】:

我在 Windows 7 中使用 OpenCV2.2。我想做的是使用以下代码检测另一个图像中定义的对象:

// Read the two image files
Mat image1 = imread( argv[1], CV_LOAD_IMAGE_GRAYSCALE );
Mat image2 = imread( argv[2], CV_LOAD_IMAGE_GRAYSCALE );

Mat finalImage = imread(argv[2]);

if( !image1.data || !image2.data ) {
    std::cout << " --(!) Error reading images " << std::endl;
    return -10;
}

//Construct the SURF Detector
int minHessian = 400;
SurfFeatureDetector detector( minHessian );

//Extract the keypoints for each image
std::vector<KeyPoint> keypoints1, keypoints2;
detector.detect(image1,keypoints1);
detector.detect(image2,keypoints2);


//Calculate descriptors (feature vectors)
SurfDescriptorExtractor extractor;
Mat descriptors1, descriptors2;
extractor.compute(image1,keypoints1,descriptors1);
extractor.compute(image2,keypoints2,descriptors2);

//Define the Matcher
FlannBasedMatcher matcher;

std::cout << "matcher constructed!" << std::endl;

std::vector<vector<DMatch >> matches;
matcher.knnMatch(descriptors1, descriptors2, matches, 2);

std::cout << "matches: " << matches.size() << std::endl;

std::vector<DMatch > good_matches;

    //THIS LOOP IS SENSITIVE TO SEGFAULTS
for (int i = 0; i < min(descriptors2.rows-1,(int) matches.size()); i++) 
    {
        if((matches[i][0].distance < 0.8*(matches[i][1].distance)) && ((int) matches[i].size()<=2 && (int) matches[i].size()>0))
        {
            good_matches.push_back(matches[i][0]);
        }
    }

std::cout << "good_matches: " << good_matches.size() << std::endl;

VS2010 构建和编译代码没有错误。我的问题是,在某些(而不是全部)情况下,当执行进入该行时

matcher.knnMatch(descriptors1, descriptors2, matches, 2);

cmd 停止并返回如下消息:“Assertion Failed (dataset.Type() == CvType(T)::type()) in unknown function etc. end to flann.hpp line 105”

这发生在图像之间没有相似性的情况下(并非在所有情况下),尽管从两个图像中正确提取了描述符(匹配所需)。如果我使用 BruteForce 匹配器,代码可以正常工作。

我也试过OpenCV的代码:

http://opencv.itseez.com/doc/tutorials/features2d/feature_homography/feature_homography.html

并且有同样的问题。即使我使用 Opencv 示例中的简单匹配器,执行也会失败。

std::vector&lt; DMatch &gt; matches;

matcher.match( descriptors_object, descriptors_scene, matches );

我搜索了解决方案(在OpenCV flann.h assertion Error 找到了一个类似的问题,但不幸的是没有答案),但我没有找到任何有用的东西。有谁知道如何解决这个问题?

【问题讨论】:

  • 我不确定它是否能解决您的问题,但是如果您尝试将描述符转换为 CV_32F 类型会怎样。
  • 检查描述符是否为空。 if(!descriptors.empty()){//做一些 flann baby}
  • 还将描述符更改为 CV_32F 格式

标签: opencv matching


【解决方案1】:

这很奇怪,它在this tutorial page中缝合了相同的代码...

也许您可以尝试使用 cv::DescriptorMatcher 接口(文档here)。

用法是:

cv::DescriptorMatcher matcher = cv::DescriptorMatcher::create("FlannBased");

然后您可以直接将其与您的代码一起使用而无需任何更改。

【讨论】:

    猜你喜欢
    • 2015-04-25
    • 2013-03-17
    • 2012-08-29
    • 2018-06-09
    • 2012-11-28
    • 1970-01-01
    • 1970-01-01
    • 2021-10-11
    相关资源
    最近更新 更多