无法将两个 SIFT 描述符与 OpenCV 匹配

Can't match two SIFT descriptors with OpenCV

我试图用我能想到的最简单的代码匹配两个 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: error: (-215) _queryDescriptors.type() == trainDescType in function cv::BFMatcher::knnMatchImpl

OpenCV Error: Assertion failed (type == src2.type() && src1.cols == src2.cols && (type == CV_32F || type == CV_8U)) in cv::batchDistance, file C:\buil ds\master_PackSlave-win32-vc12-shared\opencv\modules\core\src\stat.cpp, line 3608 Exception: C:\builds\master_PackSlave-win32-vc12-shared\opencv\modules\core\src\stat.cpp:3608: error: (-215) type == src2.type() && src1.cols == src2. cols && (type == CV_32F || type == CV_8U) in function cv::batchDistance

谢谢

非常有趣,下面的代码片段很适合我:

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);

你能查一下吗?你能提供描述符的大小和类型吗?最后,您是否在 release/debug 模式下都尝试过?

p.s.: 你用的是哪个版本?您应该尝试用最新版本覆盖并重新编译 matchers.cpp:https://github.com/Itseez/opencv/commits/master/modules/features2d/src/matchers.cpp