[英]How do I use SIFT in OpenCV 3.0 with c++?
然后:
#include "opencv2/xfeatures2d.hpp"
//
// now, you can no more create an instance on the 'stack', like in the tutorial
// (yea, noticed for a fix/pr).
// you will have to use cv::Ptr all the way down:
//
cv::Ptr<Feature2D> f2d = xfeatures2d::SIFT::create();
//cv::Ptr<Feature2D> f2d = xfeatures2d::SURF::create();
//cv::Ptr<Feature2D> f2d = ORB::create();
// you get the picture, i hope..
//-- Step 1: Detect the keypoints:
std::vector<KeyPoint> keypoints_1, keypoints_2;
f2d->detect( img_1, keypoints_1 );
f2d->detect( img_2, keypoints_2 );
//-- Step 2: Calculate descriptors (feature vectors)
Mat descriptors_1, descriptors_2;
f2d->compute( img_1, keypoints_1, descriptors_1 );
f2d->compute( img_2, keypoints_2, descriptors_2 );
//-- Step 3: Matching descriptor vectors using BFMatcher :
BFMatcher matcher;
std::vector< DMatch > matches;
matcher.match( descriptors_1, descriptors_2, matches );
另外,不要忘記鏈接 opencv_xfeatures2d !
有一些有用的答案,但我會添加我的版本(對於 OpenCV 3.X ),以防萬一上述內容不清楚(經過測試和嘗試):
build
的文件夾cmake -DOPENCV_EXTRA_MODULES_PATH=/home/YOURUSERNAME/opencv_contrib/modules -DOPENCV_ENABLE_NONFREE:BOOL=ON ..
(請注意我們顯示了 contrib 模塊所在的位置並激活了非自由模塊)make
和make install
上述步驟應該適用於 OpenCV 3.X
之后,您可以使用帶有適當標志的 g++ 運行以下代碼:
g++ -std=c++11 main.cpp `pkg-config --libs --cflags opencv` -lutil -lboost_iostreams -lboost_system -lboost_filesystem -lopencv_xfeatures2d -o surftestexecutable
重要的是不要忘記將 xfeatures2D 庫與-lopencv_xfeatures2d鏈接,如命令所示。 而main.cpp
文件是:
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include "opencv2/xfeatures2d.hpp"
#include "opencv2/xfeatures2d/nonfree.hpp"
using namespace cv;
using namespace std;
int main(int argc, const char* argv[])
{
const cv::Mat input = cv::imread("surf_test_input_image.png", 0); //Load as grayscale
Ptr< cv::xfeatures2d::SURF> surf = xfeatures2d::SURF::create();
std::vector<cv::KeyPoint> keypoints;
surf->detect(input, keypoints);
// Add results to image and save.
cv::Mat output;
cv::drawKeypoints(input, keypoints, output);
cv::imwrite("surf_result.jpg", output);
return 0;
}
這應該創建並保存帶有沖浪關鍵點的圖像。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.