簡體   English   中英

如何使用 C++ 在 OpenCV 3.0 中使用 SIFT?

[英]How do I use SIFT in OpenCV 3.0 with c++?

我有 OpenCV 3.0,我已經用 opencv_contrib 模塊編譯並安裝了它,所以這不是問題。 不幸的是,以前版本中的示例不適用於當前版本,因此盡管已經不止一次地問過這個問題,但我想要一個我可以實際使用的更當前的示例。 甚至官方示例在此版本中也不起作用(特征檢測有效,但其他特征示例無效),無論如何他們都使用 SURF。

那么,如何在 C++ 上使用 OpenCV SIFT? 我想抓取兩個圖像中的關鍵點並匹配它們,類似於這個例子,但即使只是獲取點和描述符也足夠了。 幫助!

  1. 獲取opencv_contrib 倉庫
  2. 花點時間閱讀那里的自述文件,將其添加到您的主要opencv cmake 設置中
  3. 在主 opencv 存儲庫中重新運行 cmake /make / install

然后:

   #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 ),以防萬一上述內容不清楚(經過測試和嘗試):

  1. 將 opencv 從https://github.com/opencv/opencv克隆到主目錄
  2. 將 opencv_contrib 從https://github.com/opencv/opencv_contrib克隆到主目錄
  3. 在 opencv 中,創建一個名為build的文件夾
  4. 使用此 CMake 命令激活非自由模塊: cmake -DOPENCV_EXTRA_MODULES_PATH=/home/YOURUSERNAME/opencv_contrib/modules -DOPENCV_ENABLE_NONFREE:BOOL=ON ..請注意我們顯示了 contrib 模塊所在的位置並激活了非自由模塊
  5. 之后makemake 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM