關於特徵檢測,OpenCV提供了通用的接口FeatureDetector
類。簡單調用其成員函數create()
和detect()
即可完成角點檢測。
選取8個以角點爲特徵的特徵檢測器進行測試,包括:
- HARRIS
- GFTT (Shi-tomasi)
- SIFT
- SURF
- FAST
- STAR
- ORB (oriented BRIEF)
- BRISK
測試代碼
#include <stdio.h>
#include <iostream>
#include "opencv2/core/core.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/nonfree/features2d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/nonfree/nonfree.hpp"
using namespace cv;
using namespace std;
int main()
{
string imgPath = "road.jpg";
Mat img = imread(imgPath, CV_LOAD_IMAGE_COLOR);
vector<string> detectorNames{"HARRIS","GFTT","SIFT",
"SURF","FAST","STAR","ORB","BRISK"};
cout << "DetectorName" << '\t'
<< "Number of corners" << '\t'
<< "Time used" <<'\t'<<"efficiency"
<< endl << endl;
for (string detectorName:detectorNames)
{
cout <<detectorName<<+"\t\t";
double t = (double)getTickCount();
//--detect keypoints
Ptr<FeatureDetector> detector
= FeatureDetector::create(detectorName);
vector<KeyPoint> keyPoints;
detector->detect(img, keyPoints, Mat());
cout << keyPoints.size() << "\t\t\t";
//--draw keypoints
Mat imgKeyPoints;
drawKeypoints(img, keyPoints, imgKeyPoints,
Scalar::all(-1), DrawMatchesFlags::DEFAULT);
imshow(detectorName+" KeyPoints", imgKeyPoints);
//time used
t = ((double)getTickCount() - t) / getTickFrequency();
cout << t << "\t";
//Number of coners detected per unit time(ms)
double efficiency = keyPoints.size() / t / 1000;
cout << efficiency << endl<<endl;
}
waitKey(0);
return 0;
}
運行結果
HARRIS
GFTT
SIFT
SURF
FAST
STAR
ORB
BRISK
比較
可以看出,FAST特徵檢測算法的檢測速度是超羣的,難怪敢自稱“FAST”。從特徵點檢測效率以及檢測出的特徵點數來看,SURF表現也不錯。以上各個角點檢測器,使用的都是OpenCV默認的封裝,可能會因爲具體的參數設置差異,檢測出的角點數有不同,可比性沒法說明。但是,特徵點檢測效率還是很客觀的。由以上結果得知,檢測效率最高的前3個檢測器分別爲FAST,ORB,SURF。