33

有人知道 SIFT 实施示例与 OpenCV 2.2 的链接吗?问候,

4

6 回答 6

35

下面是一个最小的例子:

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

在 OpenCV 2.3 上测试

于 2011-07-15T10:44:50.543 回答
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;

}

使用工厂方法的原因是灵活的,因为现在您可以更改为不同的关键点检测器或特征提取器,例如 SURF,只需更改传递给“创建”工厂方法的参数,如下所示:

Ptr<FeatureDetector> featureDetector = FeatureDetector::create("SURF");
Ptr<DescriptorExtractor> featureExtractor = DescriptorExtractor::create("SURF");

有关传递以创建其他检测器或提取器的其他可能参数,请参见: http: //opencv.itseez.com/modules/features2d/doc/common_interfaces_of_feature_detectors.html#featuredetector-create

http://opencv.itseez.com/modules/features2d/doc/common_interfaces_of_descriptor_extractors.html?highlight=descriptorextractor#descriptorextractor-create

现在,使用工厂方法意味着您无需猜测一些合适的参数即可传递给每个检测器或提取器。这对于刚开始使用它们的人来说很方便。但是,如果您想创建自己的自定义 SIFT 检测器,您可以将使用自定义参数创建的 SiftDetector 对象包装起来,并将其包装成智能指针,并使用上面的 featureDetector 智能指针变量引用它。

于 2011-12-16T02:23:26.427 回答
6

在 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;
}
于 2012-06-02T10:47:05.317 回答
5

OpenCV 提供开箱即用的SIFTSURF这里也是)和其他特征描述符。
请注意,SIFT算法已获得专利,因此它可能与常规 OpenCV 使用/许可不兼容。

于 2011-04-11T15:38:04.563 回答
3

另一个在opencv 2.4中使用SIFT非自由特征检测器的简单例子一定要添加opencv_nonfree240.lib依赖

#include "cv.h"
#include "highgui.h"
#include <opencv2/nonfree/nonfree.hpp>

int main(int argc, char** argv)
{
   cv::Mat img = cv::imread("image.jpg");

   cv::SIFT sift(10);   //number of keypoints

   cv::vector<cv::KeyPoint> key_points;

   cv::Mat descriptors, mascara;
   cv::Mat output_img;

   sift(img,mascara,key_points,descriptors);
   drawKeypoints(img, key_points, output_img);

   cv::namedWindow("Image");
   cv::imshow("Image", output_img);
   cv::waitKey(0);

   return 0;
}
于 2012-06-17T01:59:17.490 回答
1

如果有人想知道如何使用 2 张图片来做到这一点:

import numpy as np
import cv2

print ('Initiate SIFT detector')
sift = cv2.xfeatures2d.SIFT_create()
print ('find the keypoints and descriptors with SIFT')
gcp1, des1 = sift.detectAndCompute(src_img,None)
gcp2, des2 = sift.detectAndCompute(trg_img,None)

# create BFMatcher object
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)


matches = bf.match(des1,des2)
# Sort them in the order of their distance.
matches = sorted(matches, key = lambda x:x.distance)

#print only the first 100 matches
img3 = drawMatches(src_img, gcp1, trg_img, gcp2, matches[:100])
于 2019-04-19T19:45:12.017 回答