2012-06-19 59 views
5

我在Windows 7中使用OpenCV2.2我所试图做的是使用此代码来检测其他图像定义的目标:FlannBased匹配器断言失败错误

// 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停止,并返回一个消息,如:“断言失败(dataset.Type() == CvType(T)::type())的未知功能等等等等结束于flann.hpp行105“

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

我也曾尝试代码的OpenCV:

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

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

std::vector<DMatch> matches;

matcher.match(descriptors_object, descriptors_scene, matches);

我搜索的解决方案(在OpenCV flann.h assertion Error发现了类似的问题,但遗憾的是没有答案),但我没有发现任何有用的东西。有没有人知道如何解决这个问题?

+0

我不确定它是否可以解决您的问题,但如果您尝试将描述符转换为CV_32F类型,该怎么办。 – andriy

+0

检查描述符为空。 if(!descriptors.empty()){//做一些flann宝贝} – madLokesh

+0

也会将描述符更改为CV_32F格式 – madLokesh

回答

0

真奇怪,它的接缝同样的代码this tutorial page ...

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

用法是:

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

然后你就可以与您的代码直接使用它没有任何的变化。