歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> OpenCV_局部圖像特征的提取與匹配_源代碼

OpenCV_局部圖像特征的提取與匹配_源代碼

日期:2017/3/1 10:02:54   编辑:Linux編程

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: http://blog.csdn.net/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<vector<DMatch>>& matches, int k); // K近鄰匹配

void saveKeypoints(const Mat& image, const vector<KeyPoint>& keypoints, const string& saveFileName = ""); // 保存特征點
void saveMatches(const Mat& queryImage,
const vector<KeyPoint>& queryKeypoints,
const Mat& trainImage,
const vector<KeyPoint>& trainKeypoints,
const vector<DMatch>& matches,
const string& saveFileName = ""); // 保存匹配結果到圖片中

private:
Ptr<FeatureDetector> m_detector;
Ptr<DescriptorExtractor> m_extractor;
Ptr<DescriptorMatcher> m_matcher;

string m_detectType;
string m_extractType;
string m_matchType;

};


#endif

Copyright © Linux教程網 All Rights Reserved