sift開發記錄2

SIFT算法的c++實現(VS2010+OpenCV2.3.1)  


Sift算法的c++實現代碼,使用OpenCV中提供的sift類庫,OpenCV2.3.1在VS2010中的配置參見日誌Opencv2.3.1在VS2008和VS2010平臺上安裝配置圖解。

方法一:

建立一個控制檯應用程序代碼如下:

#include "stdafx.h"

#include "highgui.h"

#include "features2d/features2d.hpp"
#include <iostream>
using namespace std;
using namespace cv;

void _tmain(int argc, _TCHAR* argv[])
{
Mat input1=imread("image1.jpg",1);
Mat input2=imread("image2.jpg",1);
SiftFeatureDetector detector;
vector<KeyPoint> keypoint1,keypoint2;
detector.detect(input1,keypoint1);

Mat output1;
drawKeypoints(input1,keypoint1,output1);
imshow("sift_result1.jpg",output1);
imwrite("sift_result1.jpg",output1);

Mat output2;
SiftDescriptorExtractor extractor;
Mat descriptor1,descriptor2;
BruteForceMatcher<L2<float>> matcher;

vector<DMatch> matches;
Mat img_matches;
detector.detect(input2,keypoint2);
drawKeypoints(input2,keypoint2,output2);

imshow("sift_result2.jpg",output2);
imwrite("sift_result2.jpg",output2);

extractor.compute(input1,keypoint1,descriptor1);
extractor.compute(input2,keypoint2,descriptor2);

matcher.match(descriptor1,descriptor2,matches);

drawMatches(input1,keypoint1,input2,keypoint2,matches,img_matches);
imshow("matches",img_matches);
imwrite("matches.jpg",img_matches);

waitKey();


}


2.
#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);



3

<pre name="code" class="cpp">#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;

}

namedWindow("Image"); imshow("Image", output_img); waitKey(0); destroyWindow("Image"); return 0;}





發佈了95 篇原創文章 · 獲贊 29 · 訪問量 34萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章