简体   繁体   English

如何在opencv中使用SIFT

[英]how to use SIFT in opencv

I am learning C++ and OpenCV these days.这些天我正在学习 C++ 和 OpenCV。 Given an image, I want to extract its SIFT features.给定一个图像,我想提取它的 SIFT 特征。 From http://docs.opencv.org/modules/nonfree/doc/feature_detection.html , we can know that OpenCV 2.4.8 has the SIFT module.http://docs.opencv.org/modules/nonfree/doc/feature_detection.html ,我们可以知道 OpenCV 2.4.8 有 SIFT 模块。 See here:看这里:在此处输入图片说明

But I do not know how to use it.但我不知道如何使用它。 Currently, to use SIFT, I need to first call the class SIFT to get a SIFT instance.目前,要使用 SIFT,我需要先调用 SIFT 类来获取 SIFT 实例。 Then, I need to use SIFT::operator()() to do SIFT.然后,我需要使用SIFT::operator()()来做 SIFT。

But what is OutputArray , InputArray , KeyPoint ?但是什么是OutputArrayInputArrayKeyPoint Could anyone give a demo to show how to use SIFT class to do SIFT?谁能给出一个演示来展示如何使用SIFT类来做 SIFT?

See the example from Sift implementation with OpenCV 2.2请参阅使用 OpenCV 2.2 实现 Sift的示例

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/nonfree/features2d.hpp> //Thanks to Alessandro

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

    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("sift_result.jpg", output);

    return 0;
}

Tested on OpenCV 2.4.8在 OpenCV 2.4.8 上测试

Update for OpenCV 4.2.0 (don't forget to link opencv_xfeatures2d420.lib, of course) OpenCV 4.2.0 更新(当然不要忘记链接 opencv_xfeatures2d420.lib)

#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/xfeatures2d.hpp>

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

    cv::Ptr<cv::xfeatures2d::SIFT> siftPtr = cv::xfeatures2d::SIFT::create();
    std::vector<cv::KeyPoint> keypoints;
    siftPtr->detect(input, keypoints);

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

    return 0;
}

update for OpenCV3 OpenCV3 的更新

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/nonfree/features2d.hpp> //Thanks to Alessandro

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

    cv::Ptr<cv::SiftFeatureDetector> detector = cv::SiftFeatureDetector::create();
    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("sift_result.jpg", output);

    return 0;
}

I was having the same question for opencv3 but i found this .我对 opencv3 有同样的问题,但我发现了这个 It explains why SIFT and SURF removed from the default install of OpenCV 3.0 and how to use SIFT and SURF in OpenCV 3.它解释了为什么从 OpenCV 3.0 的默认安装中删除了 SIFT 和 SURF,以及如何在 OpenCV 3 中使用 SIFT 和 SURF。

The algorithms and associated implementations in opencv_contrib are not installed by default and you need to explicitly enable them when compiling and installing OpenCV to obtain access to them. opencv_contrib中的算法和相关实现不是默认安装的,你需要在编译和安装 OpenCV 时显式启用它们才能访问它们。

They are move to xfeatures2d library.他们正在转移到xfeatures2d库。
#include <opencv2/xfeatures2d.hpp>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM