1. 程式人生 > >OpenCV中feature2D學習——SURF和SIFT運算元實現特徵點檢測

OpenCV中feature2D學習——SURF和SIFT運算元實現特徵點檢測

概述

       在opencv的features2d中實現了SIFT和SURF演算法,可以用於影象特徵點的自動檢測。具體實現是採用SurfFeatureDetector/SiftFeatureDetector類的detect函式檢測SURF/SIFT特徵的關鍵點,並儲存在vector容器中,最後使用drawKeypoints函式繪製出特徵點。

       實驗所用環境是opencv2.4.0+vs2008+win7,測試圖片是:



SURF特徵點檢測

實驗程式碼如下。這裡需要注意SurfFeatureDetector是包含在opencv2/nonfree/features2d.hpp中,所以應該include這個標頭檔案,並且在“專案屬性->連結器->輸入->附加依賴項”中加入庫檔案:opencv_nonfree240d.lib。

/**
* @SURF特徵點檢測並繪製特徵點
* @author holybin
*/

#include <stdio.h>
#include <iostream>
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
//#include "opencv2/features2d/features2d.hpp"
#include "opencv2/nonfree/features2d.hpp"	//SurfFeatureDetector實際在該標頭檔案中
using namespace cv;
using namespace std;

int main( int argc, char** argv )
{
	Mat src = imread( "D:\\opencv_pic\\cat3d120.jpg", 0 );
	//Mat src = imread( "D:\\opencv_pic\\cat0.jpg", 0 );

	if( !src.data )
	{
		cout<< " --(!) Error reading images "<<endl;
		return -1;
	}

	//1--初始化SURF檢測運算元
	int minHessian = 400;
	SurfFeatureDetector detector( minHessian );

	//2--使用SURF運算元檢測特徵點
	vector<KeyPoint> keypoints;
	detector.detect( src, keypoints );

	//3--繪製特徵點
	Mat keypointImg;
	drawKeypoints( src, keypoints, keypointImg, Scalar::all(-1), DrawMatchesFlags::DEFAULT );
	imshow("SURF keypoints", keypointImg );
	cout<<"keypoint number: "<<keypoints.size()<<endl;

	waitKey(0);
	return 0;
}

檢測結果:




SIFT特徵點檢測

同樣的,使用SIFT特徵描述子進行特徵點檢測的過程類似,只不過換成了SiftFeatureDetector類,實驗程式碼如下:

/**
* @SIFT特徵點檢測並繪製特徵點
* @author holybin
*/

#include <stdio.h>
#include <iostream>
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
//#include "opencv2/features2d/features2d.hpp"
#include "opencv2/nonfree/features2d.hpp"	//SiftFeatureDetector實際在該標頭檔案中
using namespace cv;
using namespace std;

int main( int argc, char** argv )
{
	Mat src = imread( "D:\\opencv_pic\\cat3d120.jpg", 0 );
	//Mat src = imread( "D:\\opencv_pic\\cat0.jpg", 0 );

	if( !src.data )
	{
		cout<< " --(!) Error reading images "<<endl;
		return -1;
	}

	//1--初始化SIFT檢測運算元
	//int minHessian = 400;
	SiftFeatureDetector detector;//( minHessian );

	//2--使用SIFT運算元檢測特徵點
	vector<KeyPoint> keypoints;
	detector.detect( src, keypoints );

	//3--繪製特徵點
	Mat keypointImg;
	drawKeypoints( src, keypoints, keypointImg, Scalar::all(-1), DrawMatchesFlags::DEFAULT );
	imshow("SIFT keypoints", keypointImg );
	cout<<"keypoint number: "<<keypoints.size()<<endl;

	waitKey(0);
	return 0;
}

檢測結果:



從檢測結果可以看出,SURF運算元檢測到的特徵點遠遠多於SIFT運算元,至於檢測的精確度如何,後面試試利用SIFT和SURF運算元進行特徵點匹配來區分。