如何在OpenCV 3.0中使用带有c ++的SIFT?

lei*_*paC 12 c++ opencv sift opencv3.0

我有OpenCV 3.0,我已经使用opencv_contrib模块编译和安装它,所以这不是问题.不幸的是,以前版本的示例不能与当前版本一起使用,因此虽然这个问题已经被多次询问 过,但我想要一个更实际的例子,我可以实际使用它.即使是官方示例也不适用于此版本(功能检测有效但其他功能示例无效),无论如何它们都使用SURF.

那么,我如何在C++上使用OpenCV SIFT?我想抓住两个图像中的关键点并匹配它们,类似于这个例子,但即使只是得到点和描述符也足够了.救命!

ber*_*rak 40

  1. 获取opencv_contrib repo
  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 );
Run Code Online (Sandbox Code Playgroud)

另外,别忘了链接opencv_xfeatures2d!

  • 同样为了将来参考,您可以使用[drawMatches()](http://docs.opencv.org/trunk/modules/features2d/doc/drawing_function_of_keypoints_and_matches.html?highlight=drawmatches#drawmatches)来实际查看匹配项. (3认同)
  • 编辑有帮助.经过一些研究之后,我想补充一点,你也可以使用`detectAndCompute(img,mask,keypoints,descriptors)`,如果你不想要一个掩码,可以使用`noArray()`. (2认同)