OpenCV-将FLANN与ORB描述符配合使用以匹配功能 [英] OpenCV - Use FLANN with ORB descriptors to match features
问题描述
我正在使用OpenCV 3.2
I am using OpenCV 3.2
我正在尝试使用FLANN以比蛮力更快的方式匹配特征描述符.
I am trying to use FLANN to match features descriptors in a faster way than brute force.
// Ratio to the second neighbor to consider a good match.
#define RATIO 0.75
void matchFeatures(const cv::Mat &query, const cv::Mat &target,
std::vector<cv::DMatch> &goodMatches) {
std::vector<std::vector<cv::DMatch>> matches;
cv::Ptr<cv::FlannBasedMatcher> matcher = cv::FlannBasedMatcher::create();
// Find 2 best matches for each descriptor to make later the second neighbor test.
matcher->knnMatch(query, target, matches, 2);
// Second neighbor ratio test.
for (unsigned int i = 0; i < matches.size(); ++i) {
if (matches[i][0].distance < matches[i][1].distance * RATIO)
goodMatches.push_back(matches[i][0]);
}
}
此代码可用于SURF和SIFT描述符,但不适用于ORB.
This code is working me with SURF and SIFT descriptors, but not with ORB.
OpenCV Error: Unsupported format or combination of formats (type=0) in buildIndex
正如在此处所述,FLANN需要描述符为CV_32F类型,因此我们需要转换它们.
As it's said here, FLANN needs the descriptors to be of type CV_32F so we need to convert them.
if (query.type() != CV_32F) query.convertTo(query, CV_32F);
if (target.type() != CV_32F) target.convertTo(target, CV_32F);
但是,这种假定的修复方法使我在convertTo
函数中返回了另一个错误.
However, this supposed fix is returning me another error in convertTo
function.
OpenCV Error: Assertion failed (!fixedType() || ((Mat*)obj)->type() == mtype) in create
此断言在opencv/modules/core/src/matrix.cpp
文件的第2277行中.
This assertion is in opencv/modules/core/src/matrix.cpp
file, line 2277.
发生了什么事?
用于复制问题的代码.
#include <opencv2/opencv.hpp>
int main(int argc, char **argv) {
// Read both images.
cv::Mat image1 = cv::imread(argv[1], cv::IMREAD_GRAYSCALE);
if (image1.empty()) {
std::cerr << "Couldn't read image in " << argv[1] << std::endl;
return 1;
}
cv::Mat image2 = cv::imread(argv[2], cv::IMREAD_GRAYSCALE);
if (image2.empty()) {
std::cerr << "Couldn't read image in " << argv[2] << std::endl;
return 1;
}
// Detect the keyPoints and compute its descriptors using ORB Detector.
std::vector<cv::KeyPoint> keyPoints1, keyPoints2;
cv::Mat descriptors1, descriptors2;
cv::Ptr<cv::ORB> detector = cv::ORB::create();
detector->detectAndCompute(image1, cv::Mat(), keyPoints1, descriptors1);
detector->detectAndCompute(image2, cv::Mat(), keyPoints2, descriptors2);
// Match features.
std::vector<cv::DMatch> matches;
matchFeatures(descriptors1, descriptors2, matches);
// Draw matches.
cv::Mat image_matches;
cv::drawMatches(image1, keyPoints1, image2, keyPoints2, matches, image_matches);
cv::imshow("Matches", image_matches);
}
推荐答案
您是否调整了FLANN参数?
Did you adjust the FLANN parameters?
来自 http://docs.opencv. org/3.0-beta/doc/py_tutorials/py_feature2d/py_matcher/py_matcher.html
在使用ORB时,您可以传递以下内容.根据文档建议使用带注释的值,但在某些情况下并不能提供必需的结果.其他值工作正常.
While using ORB, you can pass the following. The commented values are recommended as per the docs, but it didn’t provide required results in some cases. Other values worked fine.:
index_params = dict(算法= FLANN_INDEX_LSH, table_number = 6,#12 key_size = 12,#20 multi_probe_level = 1)#2
index_params= dict(algorithm = FLANN_INDEX_LSH, table_number = 6, # 12 key_size = 12, # 20 multi_probe_level = 1) #2
可能您可以将其转换为C ++ API?
Probably you can convert that to C++ api?
根据评论,C ++方式为:
According to the comment, the C++ way is:
cv::FlannBasedMatcher matcher = cv::FlannBasedMatcher(cv::makePtr<cv::flann::LshIndexParams>(12, 20, 2));
这篇关于OpenCV-将FLANN与ORB描述符配合使用以匹配功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!