首页 > 解决方案 > 想要检测特征并在 2 个不同的帧中匹配特征

问题描述

我目前在 QT creator 中使用 OpenCV 3.4.0、c++。我已经尝试了此页面中的示例代码 https://docs.opencv.org/2.4/doc/tutorials/features2d/feature_description/feature_description.html

int minHessian = 400;
cv::xfeatures2d::SurfFeatureDetector detector(minHessian);
vector<KeyPoint> keypoints1, keypoints2;
detector.detect(img1, keypoints1);
detector.detect(img2, keypoints2);

// computing descriptors
cv::xfeatures2d::SurfDescriptorExtractor extractor;
Mat descriptors1, descriptors2;
extractor.compute(img1, keypoints1, descriptors1);
extractor.compute(img2, keypoints2, descriptors2);

// matching descriptors
BFMatcher matcher(NORM_L2);
vector<DMatch> matches;
matcher.match(descriptors1, descriptors2, matches);

// drawing the results
namedWindow("matches", 1);
Mat img_matches;
drawMatches(img1, keypoints1, img2, keypoints2, matches, img_matches);
imshow("matches", img_matches);
waitKey(0);

但代码一直返回错误

no matching function for call to 'cv::xfeatures2d::SURF::SURF(int&)(2nd line)

cannot declare variable 'detector' to be of abstract type 'cv::xfeatures2d::SURF'(2nd line)

cannot declare variable 'extractor' to be of astract type 'cv::xfeatures2d::SURF'(7th line)

我已经导入了我认为的所有必要模块,包括 xfeatures2d

问题是什么?

还有其他我可以尝试的示例代码吗?

标签: c++opencvfeature-extractionfeature-detection

解决方案


您的编码反映了与 OpenCV 3.4.0 不兼容的旧版本 OpenCV。你可以这样试试。为了更好的匹配,最好将模板图像转换为灰度图像:

cv::Ptr<Feature2D> detector = cv::xfeatures2d::SurfFeatureDetector::create();
detector->detect(img1, keypoints1);

cv::Ptr<DescriptorExtractor> extractor = cv::xfeatures2d::SurfFeatureDetector::create();
extractor->compute(img1, keypoints1, descriptors1);

推荐阅读