二、FAST特征点
harris特征在算法复杂性上比较高,在大的复杂的目标识别或匹配应用上效率不能满足要求,OpenCV提供了一个快速检测角点的类FastFeatureDetector,而实际上FAST并不是快的意思,而是Features from Accelerated Segment Test,但这个算法效率确实比较高,下面我们来看看这个类的用法。
OpenCV里为角点检测提供了统一的接口,通过类下面的detect方法来检测对应的角点,而输出格式都是vector<KeyPoint>。
|
1
2
3
4
5
6
|
vector<KeyPoint> keypoints;FastFeatureDetector fast( // 定义检测类40); //40是检测的阈值fast.detect(image,keypoints);drawKeypoints(image,keypoints,image,Scalar(255,0,0),DrawMatchesFlags::DRAW_OVER_OUTIMG); |
其中drawKeypoints是OpenCV提供的在图像上画角点的函数。它的参数可以让我们选择用不同的方式标记出特征点。
三、尺度不变的SURF特征
surf特征是类似于SIFT特征的一种尺度不变的特征点,它的优点在于比SIFT效率要高,在实际运算中可以达到实时性的要求,关于SURF的原理这里就不过多的介绍,网络上这类的文章很多。
类似于FAST特征点的求法,SURF也可以使用通用接口求得,而SURF特征的类为SurfFeatureDetector,类似的SIFT特征点的检测类为SiftFeatureDetector。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
#include <opencv2/core/core.hpp>#include <opencv2/highgui/highgui.hpp>#include <opencv2/nonfree/features2d.hpp>using namespace cv;int main(){Mat image=imread("../buliding.png");vector<KeyPoint> keypoints;SurfFeatureDetector surf(2500.);surf.detect(image,keypoints);drawKeypoints(image,keypoints,image,Scalar(255,0,0),DrawMatchesFlags::DRAW_RICH_KEYPOINTS);namedWindow("result");imshow("result",image);waitKey();return 0;} |
这里有一个值得说明的问题是:OpenCV2.4版本后好像把SurfFeatureDetector这个类的定义移到了头文件nonfree/features2d.hpp
中,所以头文件中要加入该文件,并且要把opencv_nonfree24xd.lib加入属性表的链接器熟悉的输入中,其中x换成你当前opencv的版本号。
最终的显示效果如下:
四、SURF特征的描述
在图像配准中,特征点的描述往往不是位置这么简单,而是使用了一个N维向量来描述一个特征点,这些描述子之间可以通过定义距离公式来比较相近程度。
SurfDescriptorExtractor 是一个提取SURF特征点以及其描述的类。
下面是一个宽景图像的拼接配准的例子:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
#include <opencv2/core/core.hpp>#include <opencv2/highgui/highgui.hpp>#include <opencv2/nonfree/features2d.hpp>#include <opencv2/legacy/legacy.hpp>using namespace cv;int main(){Mat image1=imread("../b1.png");Mat image2=imread("../b2.png");// 检测surf特征点vector<KeyPoint> keypoints1,keypoints2; SurfFeatureDetector detector(400);detector.detect(image1, keypoints1);detector.detect(image2, keypoints2);// 描述surf特征点SurfDescriptorExtractor surfDesc;Mat descriptros1,descriptros2;surfDesc.compute(image1,keypoints1,descriptros1);surfDesc.compute(image2,keypoints2,descriptros2);// 计算匹配点数BruteForceMatcher<L2<float>>matcher;vector<DMatch> matches;matcher.match(descriptros1,descriptros2,matches);std::nth_element(matches.begin(),matches.begin()+24,matches.end());matches.erase(matches.begin()+25,matches.end());// 画出匹配图Mat imageMatches;drawMatches(image1,keypoints1,image2,keypoints2,matches,imageMatches,Scalar(255,0,0));namedWindow("image2");imshow("image2",image2);waitKey();return 0;} |
程序中我们选择了25个配准点,得到最后的匹配如下:




