#include <stdio.h>
#include <iostream>
#include <opencv2/core/core.hpp>//因爲在屬性中已經配置了opencv等目錄,所以把其當成了本地目錄一樣
#include <opencv2/features2d/features2d.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/ml/ml.hpp>
#include <opencv2/nonfree/nonfree.hpp>
#include <opencv2/legacy/legacy.hpp>
using namespace cv;
using namespace std;
int main(int argc,char* argv[])
{
Mat img_1=imread("../1.jpg",CV_LOAD_IMAGE_GRAYSCALE);//宏定義時CV_LOAD_IMAGE_GRAYSCALE=0,也就是讀取灰度圖像
Mat img_2=imread("../2.jpg",CV_LOAD_IMAGE_GRAYSCALE);//一定要記得這裏路徑的斜線方向,這與Matlab裏面是相反的
if(!img_1.data || !img_2.data)//如果數據爲空
{
cout<<"opencv error"<<endl;
return -1;
}
cout<<"open right"<<endl;
//第一步,用SIFT算子檢測關鍵點
SiftFeatureDetector detector;//構造函數採用內部默認的
std::vector<KeyPoint> keypoints_1,keypoints_2;//構造2個專門由點組成的點向量用來存儲特徵點
detector.detect(img_1,keypoints_1);//將img_1圖像中檢測到的特徵點存儲起來放在keypoints_1中
detector.detect(img_2,keypoints_2);//同理
//在圖像中畫出特徵點
Mat img_keypoints_1,img_keypoints_2;
drawKeypoints(img_1,keypoints_1,img_keypoints_1,Scalar::all(-1),DrawMatchesFlags::DEFAULT);//在內存中畫出特徵點
drawKeypoints(img_2,keypoints_2,img_keypoints_2,Scalar::all(-1),DrawMatchesFlags::DEFAULT);
imshow("sift_keypoints_1",img_keypoints_1);//顯示特徵點
imshow("sift_keypoints_2",img_keypoints_2);
//計算特徵向量
SiftDescriptorExtractor extractor;//定義描述子對象
Mat descriptors_1,descriptors_2;//存放特徵向量的矩陣
extractor.compute(img_1,keypoints_1,descriptors_1);//計算特徵向量
extractor.compute(img_2,keypoints_2,descriptors_2);
//用burte force進行匹配特徵向量
BruteForceMatcher<L2<float> >matcher;//定義一個burte force matcher對象
vector<DMatch>matches;
matcher.match(descriptors_1,descriptors_2,matches);
//繪製匹配線段
Mat img_matches;
drawMatches(img_1,keypoints_1,img_2,keypoints_2,matches,img_matches);//將匹配出來的結果放入內存img_matches中
//顯示匹配線段
imshow("sift_Matches",img_matches);//顯示的標題爲Matches
char key = (char)waitKey();
if(key==27 || key =='q' || key == 'Q'){
return 0;
}
return 0;
}
Linux Ubuntu 12.04
轉自: http://blog.csdn.net/lee_cv/article/details/8804578
做了一點點修改,原版的有一些問題。設置不同的seed,頭文件修改,weikey的方式