无法使用OpenCV匹配两个SIFT描述符

3

我尝试用最简单的代码匹配两个 SIFT 描述符,但是 OpenCV 3 不断抛出异常。

这是我的代码:

cv::Mat img1 = imread(...); // Shortened for the example
cv::Mat img2 = imread(...); // Shortened for the example

std::vector<KeyPoint> keypoints1, keypoints2;
Ptr<SIFT> ptrSift = SIFT::create(200, 3, 0.07, 15);
Mat descriptors1, descriptors2;
ptrSift->detectAndCompute(img1, Mat(), keypoints1, descriptors1, false);
ptrSift->detectAndCompute(img2, Mat(), keypoints2, descriptors2, false);

上面的代码给我带来了良好的结果,我可以使用drawKeypoints函数进行可视化。

然后我使用以下代码来匹配描述符:

BFMatcher matcher;
std::vector< DMatch > matches;
matcher.match(descriptors1, descriptors2, matches);

但是它一直抛出以下错误信息:
C:\builds\master_PackSlave-win32-vc12-shared\opencv\modules\features2d\src\matchers.cpp:722: 错误:(-215) _queryDescriptors.type() == trainDescType 在函数 cv::BFMatcher::knnMatchImpl 中 OpenCV错误:断言失败 (type == src2.type() && src1.cols == src2.cols && (type == CV_32F || type == CV_8U)),位于文件 C:\builds\master_PackSlave-win32-vc12-shared\opencv\modules\core\src\stat.cpp 的第 3608 行。 异常: C:\builds\master_PackSlave-win32-vc12-shared\opencv\modules\core\src\stat.cpp:3608: 错误:(-215) type == src2.type() && src1.cols == src2.cols && (type == CV_32F || type == CV_8U) 在函数 cv::batchDistance 中。
谢谢。

我建议使用:http://robwhess.github.io/opensift/。它非常快速且易于使用。 - qqibrow
在调用match()之前,你应该检查descriptors1或descriptors2是否为空。 - Vit
谢谢您的评论,但它们不是空的。 - Itay
你的描述矩阵中是否存在零向量的描述符或行?我曾经遇到过这种情况。尝试使用FlannBasedMatcher,据我所知,它可以处理这种情况。 - gfkri
1
仔细查看错误。检查两个图像:(1)是否为CV_32FCV_8U类型,(2)它们是相同的类型,(3)每个图像的两列是否相同。 - rayryeng
@rayryeng 即使是 matcher.match(descriptors, descriptors, matches); 也失败了... @gfkri 尝试了 FlannBrute 两种方式... - Itay
1个回答

2

很有趣,下面的代码片段对我来说很有效:

cv::BFMatcher matcher;
std::vector<cv::DMatch> matches;

cv::Mat descriptors1 = cv::Mat::eye(10, 10, CV_32F);
cv::Mat descriptors2 = cv::Mat::eye(10, 10, CV_32F);
matcher.match(descriptors1, descriptors2, matches);

你能检查一下吗?你能提供描述符的大小和类型吗?最后,你尝试过在发布/调试模式下都测试了吗?
附:你使用的是哪个版本?你应该尝试使用最新版本覆盖并重新编译matchers.cpp:https://github.com/Itseez/opencv/commits/master/modules/features2d/src/matchers.cpp

好像这个方法可行。我猜矩阵不是像之前评论中有人建议的那样CV_32F。现在我遇到了我的SiftDescriptorExtractor的问题...代码说没有实现...无论如何还是谢谢。 - Itay

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接