You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
1.8 KiB

#ifndef FEATURE_MATCHER_H
#define FEATURE_MATCHER_H
#pragma once
#include <opencv2/opencv.hpp>
#include <vector>
#include <stdexcept>
#ifdef HAVE_OPENCV_XFEATURES2D
#include <opencv2/xfeatures2d.hpp>
#endif
// 特征检测器类型
enum DetectorType
{
SIFT,
SURF,
ORB
};
// 匹配器类型
enum MatcherType
{
FLANN,
BF
};
class FeatureMatcher
{
public:
// 构造函数
FeatureMatcher(DetectorType detectorType, MatcherType matcherType);
// 提取特征点和描述符
void extractFeatures(cv::Mat& image, std::vector<cv::KeyPoint>& keypoints, cv::Mat& descriptors);
// 匹配特征点
void matchFeatures(cv::Mat& descriptors1, cv::Mat& descriptors2, std::vector<cv::DMatch>& matches);
// K近邻匹配
void matchKnn(cv::Mat& descriptors1, cv::Mat& descriptors2, std::vector<std::vector<cv::DMatch>>& matches, int k);
// 带初始H监督的匹配
void matchFeatures_WithH(std::vector<cv::KeyPoint> keypoints1, cv::Mat& descriptors1, std::vector<cv::KeyPoint> keypoints2, cv::Mat& descriptors2,
cv::Mat H1, cv::Mat H2, std::vector<cv::DMatch>& matches);
// 计算单应性矩阵
cv::Mat computeHomography(std::vector<cv::KeyPoint>& keypoints1, std::vector<cv::KeyPoint>& keypoints2,
std::vector<cv::DMatch>& matches, double ransacReprojThreshold = 3.0);
private:
// 初始化特征检测器
void initDetector();
// 初始化匹配器
void initMatcher();
DetectorType detectorType_; // 特征检测器类型
MatcherType matcherType_; // 匹配器类型
cv::Ptr<cv::Feature2D> detector_; // 特征检测器
cv::Ptr<cv::DescriptorMatcher> flannMatcher_; // FLANN匹配器
cv::Ptr<cv::DescriptorMatcher> bfMatcher_; // 暴力匹配器
};
#endif // FEATURE_MATCHER_H