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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27533203/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 20:48:58  来源:igfitidea点击:

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

c++opencvsiftopencv3.0

提问by leinaD_natipaC

I have OpenCV 3.0, and I have compiled & installed it with the opencv_contrib module so that's not a problem. Unfortunately the examples from previous versions do not work with the current one, and so although this question has already been askedmore than onceI would like a more current example that I can actually work with. Even the official examplesdon't work in this version (feature detection works but not other feature examples) and they use SURF anyway.

我有 OpenCV 3.0,我已经用 opencv_contrib 模块编译并安装了它,所以这不是问题。不幸的是,以前版本中的示例不适用于当前版本,因此尽管已经不止一次地问过这个问题,但我想要一个我可以实际使用的更当前的示例。甚至官方示例在此版本中也不起作用(特征检测有效,但其他特征示例无效),无论如何他们都使用 SURF。

So, how do I use OpenCV SIFT on C++? I want to grab the keypoints in two images and match them, similar to this example, but even just getting the points and descriptors would be enough help. Help!

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

回答by berak

  1. get the opencv_contrib repo
  2. take your time with the readme there, add it to your mainopencv cmake settings
  3. rerun cmake /make / install in the main opencv repo
  1. 获取opencv_contrib 仓库
  2. 花点时间阅读那里的自述文件,将其添加到您的主要opencv cmake 设置中
  3. 在主 opencv 存储库中重新运行 cmake /make / install

then:

然后:

   #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 );

also, don't forget to link opencv_xfeatures2d !

另外,不要忘记链接 opencv_xfeatures2d !

回答by Okan Barut

There are useful answers, but I'll add my version (for OpenCV 3.X) just in case the above ones aren't clear (tested and tried):

有一些有用的答案,但我会添加我的版本(对于 OpenCV 3.X),以防万一上述内容不清楚(经过测试和尝试):

  1. Clone opencv from https://github.com/opencv/opencvto home dir
  2. Clone opencv_contrib from https://github.com/opencv/opencv_contribto home dir
  3. Inside opencv, create a folder named build
  4. Use this CMake command, to activate non-free modules: cmake -DOPENCV_EXTRA_MODULES_PATH=/home/YOURUSERNAME/opencv_contrib/modules -DOPENCV_ENABLE_NONFREE:BOOL=ON ..(Please notice that we showed where the contrib modules resides and also activated the nonfree modules)
  5. Do makeand make installafterwards
  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之后

The above steps should work out for OpenCV 3.X

上述步骤应该适用于 OpenCV 3.X

After that, you may run the below code using g++ with the appropriate flags:

之后,您可以使用带有适当标志的 g++ 运行以下代码:

g++ -std=c++11 main.cpp `pkg-config --libs --cflags opencv` -lutil -lboost_iostreams -lboost_system -lboost_filesystem -lopencv_xfeatures2d -o surftestexecutable

The important thing not to forget is to link the xfeatures2D library with -lopencv_xfeatures2das shown on the command. And the main.cppfile is:

重要的是不要忘记将 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;
}

This should create and save an image with surf keypoints.

这应该创建并保存带有冲浪关键点的图像。