Una*_*dra 33
以下是一个最小的例子:
#include <opencv/cv.h>
#include <opencv/highgui.h>
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;
}
Run Code Online (Sandbox Code Playgroud)
在OpenCV 2.3上测试过
lig*_*ist 30
您可以通过多种方式获得SIFT检测器和基于SIFT的提取器.正如其他人已经提出了更直接的方法,我将提供更多的"软件工程"方法,可以使您的代码更灵活地更改(即更容易更改为其他检测器和提取器).
首先,如果您希望使用内置参数获取检测器,最好的方法是使用OpenCV的工厂方法来创建它.这是如何:
#include <opencv2/core/core.hpp>
#include <opencv2/features2d/features2d.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <vector>
using namespace std;
using namespace cv;
int main(int argc, char *argv[])
{
Mat image = imread("TestImage.jpg");
// Create smart pointer for SIFT feature detector.
Ptr<FeatureDetector> featureDetector = FeatureDetector::create("SIFT");
vector<KeyPoint> keypoints;
// Detect the keypoints
featureDetector->detect(image, keypoints); // NOTE: featureDetector is a pointer hence the '->'.
//Similarly, we create a smart pointer to the SIFT extractor.
Ptr<DescriptorExtractor> featureExtractor = DescriptorExtractor::create("SIFT");
// Compute the 128 dimension SIFT descriptor at each keypoint.
// Each row in "descriptors" correspond to the SIFT descriptor for each keypoint
Mat descriptors;
featureExtractor->compute(image, keypoints, descriptors);
// If you would like to draw the detected keypoint just to check
Mat outputImage;
Scalar keypointColor = Scalar(255, 0, 0); // Blue keypoints.
drawKeypoints(image, keypoints, outputImage, keypointColor, DrawMatchesFlags::DEFAULT);
namedWindow("Output");
imshow("Output", outputImage);
char c = ' ';
while ((c = waitKey(0)) != 'q'); // Keep window there until user presses 'q' to quit.
return 0;
}
Run Code Online (Sandbox Code Playgroud)
使用工厂方法的原因很灵活,因为现在您可以通过更改传递给"create"工厂方法的参数来更改为不同的关键点检测器或特征提取器,例如SURF,如下所示:
Ptr<FeatureDetector> featureDetector = FeatureDetector::create("SURF");
Ptr<DescriptorExtractor> featureExtractor = DescriptorExtractor::create("SURF");
Run Code Online (Sandbox Code Playgroud)
有关传递以创建其他检测器或提取器的其他可能参数,请参阅:http: //opencv.itseez.com/modules/features2d/doc/common_interfaces_of_feature_detectors.html#featuredetector-create
现在,使用工厂方法意味着您不必猜测一些合适的参数传递给每个探测器或提取器.这对于刚接触它们的人来说非常方便.但是,如果要创建自己的自定义SIFT检测器,可以将使用自定义参数创建的SiftDetector对象包装并将其包装到智能指针中,并使用上面的featureDetector智能指针变量引用它.
在opencv 2.4中使用SIFT非自由特征检测器的简单示例
#include <opencv2/opencv.hpp>
#include <opencv2/nonfree/nonfree.hpp>
using namespace cv;
int main(int argc, char** argv)
{
if(argc < 2)
return -1;
Mat img = imread(argv[1]);
SIFT sift;
vector<KeyPoint> key_points;
Mat descriptors;
sift(img, Mat(), key_points, descriptors);
Mat output_img;
drawKeypoints(img, key_points, output_img);
namedWindow("Image");
imshow("Image", output_img);
waitKey(0);
destroyWindow("Image");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
56243 次 |
最近记录: |