SURF特征提取

来源:互联网 发布:帝国cms目录结构 编辑:程序博客网 时间:2024/06/05 05:32

(文章借鉴《OpenCV3编程入门》)

SURF特征点提取(图像为灰度图)

一,BruteForce进行特征点匹配

【1】使用SURF算子检测关键点

int minHessian = 700;//SURF算法中的hessian阈值
SurfFeatureDetector detector(minHessian);//定义一个SurfFeatureDetector(SURF) 特征检测类对象
vector<KeyPoint> keyPoint1, keyPoints2;//vector模板类,存放任意类型的动态数组
【2】调用detect函数检测出SURF特征关键点,保存在vector容器中
detector.detect(srcImage1, keyPoint1);
detector.detect(srcImage2, keyPoints2);
【3】计算描述符(特征向量)
SurfDescriptorExtractor extractor;
Mat descriptors1, descriptors2;
extractor.compute(srcImage1, keyPoint1, descriptors1);
extractor.compute(srcImage2, keyPoints2, descriptors2);
【4】使用BruteForce进行匹配
// 实例化一个匹配器
BruteForceMatcher< L2<float> > matcher;//(Flann描述符匹配对象为FlannBaseMatcher matcher )
vector< DMatch > matches;
//匹配两幅图中的描述子(descriptors)
matcher.match(descriptors1, descriptors2, matches);
【5】绘制从两个图像中匹配出的关键点
Mat imgMatches;

drawMatches(srcImage1, keyPoint1, srcImage2, keyPoints2, matches, imgMatches);//进行绘制

二,使用FLANN进行特征点识别

前提:已获取图像描述符(特征向量)

//创建基于FLANN的描述符匹配对象

FlannBaseMatcher matcher;

vector<Mat>train_desc_collection(1,KeyPoint1);

match.add(train_desc_collection);//添加描述符集

matcher.train();//训练一个描述符匹配算子

//匹配训练和测试描述符

vector< DMatch > matches;

match.knnMatch(keyPoint2,matches,2);

//根据劳氏算法得到优秀的匹配点

matches[i][0].distance<0.6*matches[i][1].distance;