2012-06-19 60 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"); 

然後你就可以與您的代碼直接使用它沒有任何的變化。