how to use SIFT in opencv
Asked Answered
C

5

15

I am learning C++ and OpenCV these days. Given an image, I want to extract its SIFT features. From http://docs.opencv.org/modules/nonfree/doc/feature_detection.html, we can know that OpenCV 2.4.8 has the SIFT module. See here: enter image description 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. Then, I need to use SIFT::operator()() to do SIFT.

But what is OutputArray , InputArray, KeyPoint? Could anyone give a demo to show how to use SIFT class to do SIFT?

Catastrophism answered 28/3, 2014 at 20:38 Comment(3)
maybe one of the tutorials is helpful ?Curbstone
@Curbstone it is SurfFeatureDetector , not SIFT. But, what I want to use is class SIFT. I show it in the link. Thanks !Catastrophism
yea, i know. but the procedure is similarCurbstone
C
18

See the example from 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;
}

Tested on OpenCV 2.4.8

Cutie answered 28/3, 2014 at 22:3 Comment(1)
Don't you need #include "opencv2/nonfree/features2d.hpp" ?Michale
T
7

Update for OpenCV 4.2.0 (don’t forget to link opencv_xfeatures2d420.lib, of course)

#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;
}
Troglodyte answered 3/4, 2020 at 14:10 Comment(2)
I am using xfeatures2d version 451 and it has no member SIFT, it has SURF, DAISY, FREAK and a bunch of others but not SIFT :/ is it deprecated or something? Later edit: Apparently it is under features2d and available directly as a class in cv now: docs.opencv.org/2.4/modules/nonfree/doc/…Favela
Sorry for two comments, the above link is for version 2.4; for version 4.5 use this link docs.opencv.org/master/d7/d60/classcv_1_1SIFT.htmlFavela
W
5

update for 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;
}
Washtub answered 24/5, 2018 at 11:9 Comment(0)
M
0

I was having the same question for opencv3 but i found this. 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.

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.

They are move to xfeatures2d library.
#include <opencv2/xfeatures2d.hpp>

Marshland answered 1/12, 2016 at 18:50 Comment(0)
P
0

Update for OpenCv 4.5 and those who (like me) do not know quickly translate @mcy 's comment to code.

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

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

    cv::Ptr<cv::SIFT> siftPtr = cv::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);

    return 0;
}
Pickup answered 20/5 at 8:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.