如何在opencv中使用SIFT

时间:2014-03-28 20:38:28

标签: c++ opencv sift

这些天我正在学习C ++和OpenCV。鉴于图像,我想提取其SIFT功能。从http://docs.opencv.org/modules/nonfree/doc/feature_detection.html开始,我们可以知道OpenCV 2.4.8具有SIFT模块。 看这里: enter image description here

但我不知道如何使用它。目前,要使用SIFT,我需要先调用类SIFT来获取SIFT实例。然后,我需要使用SIFT::operator()()来做SIFT。

但是OutputArrayInputArrayKeyPoint是什么?任何人都可以举一个演示来展示如何使用SIFT类来做SIFT吗?

4 个答案:

答案 0 :(得分:16)

请参阅Sift implementation with OpenCV 2.2

中的示例
#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;
}

在OpenCV 2.4.8上测试

答案 1 :(得分:3)

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;
}

答案 2 :(得分:1)

更新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;
}

答案 3 :(得分:0)

我对opencv3有同样的问题,但我找到了this 。它解释了为什么SIFT和SURF从OpenCV 3.0的默认安装中删除以及如何在OpenCV 3中使用SIFT和SURF。