OpenCV的feature2d module中提供了从局部图像特征(Local image feature)的检测、特征向量(feature vector)的提取,到特征匹配的实现。其中的局部图像特征包括了常用的几种局部图像特征检测与描述算子,如FAST、SURF、SIFT、以及ORB。对于高维特征向量之间的匹配,OpenCV主要有两种方式:1)BruteForce穷举法;2)FLANN近似K近邻算法(包含了多种高维特征向量匹配的算法,例如随机森林等)。
feature2d module: http://docs.opencv.org/modules/features2d/doc/features2d.html
OpenCV FLANN: http://docs.opencv.org/modules/flann/doc/flann.html
FLANN: http://www.cs.ubc.ca/~mariusm/index.php/FLANN/FLANN
下面的这段代码实现了基于OpenCV的局部图像特征检测、特征向量提取、以及高维特征向量的匹配功能。
版本:OpenCV2.4.2
LocalFeature.h
// 局部图像特征提取与匹配
// Author: www.icvpr.com
// Blog : http://blog.youkuaiyun.com/icvpr
#ifndef _FEATURE_H_
#define _FEATURE_H_
#include <iostream>
#include <vector>
#include <string>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
class Feature
{
public:
Feature();
~Feature();
Feature(const string& detectType, const string& extractType, const string& matchType);
public:
void detectKeypoints(const Mat& image, vector<KeyPoint>& keypoints); // 检测特征点
void extractDescriptors(const Mat& image, vector<KeyPoint>& keypoints, Mat& descriptor); // 提取特征向量
void bestMatch(const Mat& queryDescriptor, Mat& trainDescriptor, vector<DMatch>& matches); // 最近邻匹配
void knnMatch(const Mat& queryDescriptor, Mat& trainDescriptor, vector<vect