C++ 如何在opencv中使用SIFT
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22722772/
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
how to use SIFT in opencv
提问by tqjustc
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:
这些天我正在学习 C++ 和 OpenCV。给定一个图像,我想提取它的 SIFT 特征。从http://docs.opencv.org/modules/nonfree/doc/feature_detection.html,我们可以知道 OpenCV 2.4.8 有 SIFT 模块。看这里:
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.
但我不知道如何使用它。目前,要使用 SIFT,我需要先调用 SIFT 类来获取 SIFT 实例。然后,我需要SIFT::operator()()
用来做 SIFT。
But what is OutputArray
, InputArray
, KeyPoint
? Could anyone give a demo to show how to use SIFT
class to do SIFT?
但是,什么是OutputArray
,InputArray
, KeyPoint
?谁能给出一个演示来展示如何使用SIFT
类来做 SIFT?
回答by Liam McInroy
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 上测试
回答by lbsweek
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;
}
回答by Julien Busset
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;
}
回答by Yirga
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.
我对 opencv3 有同样的问题,但我发现了这个。它解释了为什么从 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.
#include <opencv2/xfeatures2d.hpp>
他们正在搬到xfeatures2d
图书馆。
#include <opencv2/xfeatures2d.hpp>