C++ - SiftFeatureDetector 未加载

C++ - SiftFeatureDetector is not loading

我目前正在尝试使用 OpenCV 的 SiftFeatureDetector。然而,这发生了: Click This Link For Image

我看到我需要非自由文件。这是我从中获取文件的地方:

features2d.hpp: sourceforge.net/p/emgucv/opencv/ci/3ad471d9c187b6509ca4aab439290bc76c7a258f/tree/modules/nonfree/include/opencv2/nonfree/features2d.hpp

nonfree.hpp: sourceforge.net/p/emgucv/opencv/ci/3ad471d9c187b6509ca4aab439290bc76c7a258f/tree/modules/nonfree/include/opencv2/nonfree/nonfree.hpp

这些是我的进口商品: Click This Link For Image

有人可以告诉我这是什么问题吗?

您正在从 EmguCV 站点获取文件。 EmguCV 是为 .net 框架开发的 OpenCV 的包装器。如果您打算将 OpenCV 与 C++ 一起使用,那么您应该使用可从以下网址下载的 OpenCV 库和头文件:http://opencv.org.

似乎 SIFT 从版本 3.0 中移除了 OpenCV 的默认安装。所以答案取决于您使用的 OpenCV 版本。

注意:如果您不想自己构建 OpenCV,您应该考虑使用 2.4.13.2 或低于 3.0 的版本.

在 Windows 10 和 Visual Studio 2015 年测试。


OpenCV 2.4.13.2

您可以从此 link 下载预建库。预建库包括 SIFT。 (就我而言,因为我在 Windows,所以我使用了 opencv-2.4.13.2-vc14.exe)您可以像下面的代码一样使用 SIFT。

#include <opencv2/opencv.hpp>
#include <opencv2/nonfree/features2d.hpp>

int main(int argc, const char* argv[])
{
    const cv::Mat input = cv::imread("D:/lenna.png", 0); //Load as grayscale

    // Detect
    cv::SiftFeatureDetector detector;
    std::vector<cv::KeyPoint> keypoints;
    detector.detect(input, keypoints);

    // Add results to image and save.
    cv::Mat output;
    cv::drawKeypoints(input, keypoints, output);
    cv::imwrite("D:/lenna_sift.jpg", output);

    return 0;
}

original image / feature detected image

(从 here 复制了这个最小的例子)


OpenCV 3.2

您可以从 this link, but this version doesn't include SIFT as a default installed feature. (I've checked opencv-3.2.0-vc14.exe and it doesn't have it) It seems that the OpenCV community decided to remove patented algorithms like SIFT and SURF from default install starting from version 3.0(link). To use these, you have to download the opencv and opencv_contrib 源代码下载此版本的预构建库并自行构建库。您必须配置构建设置,以便可以在启用 SIFT 的情况下进行构建。然后你可以像下面那样做SIFT。

#include <opencv2/opencv.hpp>
#include <opencv2/xfeatures2d.hpp>

int main(int argc, const char* argv[])
{
    const cv::Mat input = cv::imread("D:/lenna.png", 0); //Load as grayscale

    // Detect
    cv::Ptr<cv::Feature2D> f2d = cv::xfeatures2d::SiftFeatureDetector::create();
    std::vector<cv::KeyPoint> keypoints;
    f2d->detect(input, keypoints);

    // Add results to image and save.
    cv::Mat output;
    cv::drawKeypoints(input, keypoints, output);
    cv::imwrite("D:/lenna_sift.jpg", output);

    return 0;
}

original image / feature detected image


我希望在 3.0 之前与其他版本一起使用 SIFT 应该与 2.4.13.2 相同,并且包括 3.0 和之后的版本与 3.2 相同。如果不是,请告诉我,以便我改进 post。