opencv实现简单SIFT代码

来源:互联网 发布:中国最帅的程序员 编辑:程序博客网 时间:2024/05/18 06:27

运行环境:opencv2.3.1+vs2010,在opencv中有封装好的sift类库,可直接调用,实现sift只需简单几行代码,有详细注释。

//SIFT#include <tchar.h>#include "highgui.h"#include "features2d/features2d.hpp"#include <iostream>using namespace std;using namespace cv;void _tmain(int argc, _TCHAR* argv[]){/*1. input images*/Mat input1=imread("church01.jpg",1);//read image1Mat input2=imread("church02.jpg",1);//read image2/*2. feature detector*/SiftFeatureDetector detector;//define sift detectorvector<KeyPoint> keypoint1,keypoint2;//define kepoint vectordetector.detect(input1,keypoint1);//extract features of image1Mat output1;drawKeypoints(input1,keypoint1,output1);//draw features of image1imshow("sift_result1.jpg",output1);imwrite("sift_result1.jpg",output1);detector.detect(input2,keypoint2);//extract features of image2Mat output2;drawKeypoints(input2,keypoint2,output2);//draw features of image2imshow("sift_result2.jpg",output2);imwrite("sift_result2.jpg",output2);/*3. feature descriptor*/SiftDescriptorExtractor extractor;Mat descriptor1,descriptor2;//define sift descriptorBruteForceMatcher<L2<float>> matcher;vector<DMatch> matches;Mat img_matches;extractor.compute(input1,keypoint1,descriptor1);//create descriptor1extractor.compute(input2,keypoint2,descriptor2);//create descriptor2/*4. image matching*/matcher.match(descriptor1,descriptor2,matches);//match image1 to image2drawMatches(input1,keypoint1,input2,keypoint2,matches,img_matches);imshow("matches",img_matches);imwrite("matches.jpg",img_matches);waitKey();}

运行结果



0 0