我在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;
Run Code Online (Sandbox Code Playgroud)
VS2010无错误地构建和编译代码.我的问题是,在某些(而不是所有)情况下,当执行进入该行时
matcher.knnMatch(descriptors1, descriptors2, matches, 2);
cmd停止并返回如下消息:"Assertion Failed(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断言错误,但遗憾的是没有答案)但我没有找到任何有用的东西.有谁知道如何解决这个问题?
这很奇怪,它在本教程页面中缝合了相同的代码......
也许您可以尝试使用 cv::DescriptorMatcher 接口(文档此处)。
用法是:
cv::DescriptorMatcher matcher = cv::DescriptorMatcher::create("FlannBased");
Run Code Online (Sandbox Code Playgroud)
然后您可以直接在代码中使用它,无需任何更改。