因為最近准備看特征點檢查方面的源碼,而其中最著名的算法就是sift和surf。因此這次主要是學會怎樣使用opencv中的sift和surf函數來檢測特征點和描述特征點,以及怎樣使用其算法來進行特征點匹配。慶幸的是,sift算法雖然是專利,但是在opencv的努力下也獲得了作者的允許,將其加入了新版本的opencv中了。
使用環境:opencv2.3.1+vs2010
功能:找出2幅圖中特征點,並將其描述出來,且在2幅中進行匹配。2幅圖內容相同,但是經過了曝光,旋轉,縮放處理過。
首先來看sift算法函數的使用。
工程代碼:
// sift_test.cpp : 定義控制台應用程序的入口點。
#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include "opencv2/core/core.hpp"//因為在屬性中已經配置了opencv等目錄,所以把其當成了本地目錄一樣
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/highgui/highgui.hpp"
using namespace cv;
using namespace std;
void readme();
int main(int argc,char* argv[])
{
Mat img_1=imread("./image/query.png",CV_LOAD_IMAGE_GRAYSCALE);//宏定義時CV_LOAD_IMAGE_GRAYSCALE=0,也就是讀取灰度圖像
Mat img_2=imread("./image/rs_query.png",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
waitKey(0);
return 0;
}
運行結果如下:
下面看surf算法函數的使用(和sift基本一樣,就是函數名改了下而已):
工程代碼:
// surf_test.cpp : 定義控制台應用程序的入口點。
//
#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include "opencv2/core/core.hpp"//因為在屬性中已經配置了opencv等目錄,所以把其當成了本地目錄一樣
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/highgui/highgui.hpp"
using namespace cv;
using namespace std;
void readme();
int main(int argc,char* argv[])
{
Mat img_1=imread("./image/query.png",CV_LOAD_IMAGE_GRAYSCALE);//宏定義時CV_LOAD_IMAGE_GRAYSCALE=0,也就是讀取灰度圖像
Mat img_2=imread("./image/rs_query.png",CV_LOAD_IMAGE_GRAYSCALE);//一定要記得這里路徑的斜線方向,這與Matlab里面是相反的
if(!img_1.data || !img_2.data)//如果數據為空
{
cout<<"opencv error"<<endl;
return -1;
}
cout<<"open right"<<endl;
//第一步,用SURF算子檢測關鍵點
int minHessian=400;
SurfFeatureDetector detector(minHessian);
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("surf_keypoints_1",img_keypoints_1);
imshow("surf_keypoints_2",img_keypoints_2);
//計算特征向量
SurfDescriptorExtractor 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("surf_Matches",img_matches);//顯示的標題為Matches
waitKey(0);
return 0;
}
其運行結果如下:
從這個實驗可以知道,在opencv中使用這2個算法是多么的簡單!只需要簡單的幾個參數就可以達到很好的效果。但這只是opencv的低級應用,我們應該在最好能用opencv一些內部函數來幫助實現自己的算法和想法。這就是分析opencv源碼的主要目的。
另外,從實驗的過程中可以感覺出來surf算法的運行時間比sift快很多,且特征點的數目檢測得比較多,其它的暫時還沒區別出來。歡迎交流,謝謝!