学习OpenCV——KeyPoint Matching 优化方式
来源:互联网 发布:vb语言基本代码 编辑:程序博客网 时间:2024/06/14 04:19
今天读Mastering OpenCV with Practical Computer Vision Projects 中的第三章里面讲到了几种特征点匹配的优化方式,在此记录。
在图像特征点检测完成后(特征点检测参考:学习OpenCV——BOW特征提取函数(特征点篇)),就会进入Matching procedure。
1. OpenCV提供了两种Matching方式:
• Brute-force matcher (cv::BFMatcher)
• Flann-based matcher (cv::FlannBasedMatcher)
Brute-force matcher就是用暴力方法找到点集一中每个descriptor在点集二中距离最近的descriptor;
Flann-based matcher 使用快速近似最近邻搜索算法寻找(用快速的第三方库近似最近邻搜索算法)
一般把点集一称为 train set (训练集)对应模板图像,点集二称为 query set(查询集)对应查找模板图的目标图像。
为了提高检测速度,你可以调用matching函数前,先训练一个matcher。训练过程可以首先使用cv::FlannBasedMatcher来优化,为descriptor建立索引树,这种操作将在匹配大量数据时发挥巨大作用(比如在上百幅图像的数据集中查找匹配图像)。而Brute-force matcher在这个过程并不进行操作,它只是将train descriptors保存在内存中。
2. 在matching过程中可以使用cv::DescriptorMatcher的如下功能来进行匹配:
- 简单查找最优匹配:void match(const Mat& queryDescriptors, vector<DMatch>& matches,const vector<Mat>& masks=vector<Mat>() );
- 为每个descriptor查找K-nearest-matches:void knnMatch(const Mat& queryDescriptors, vector<vector<DMatch> >& matches, int k,const vector<Mat>&masks=vector<Mat>(),bool compactResult=false );
- 查找那些descriptors间距离小于特定距离的匹配:void radiusMatch(const Mat& queryDescriptors, vector<vector<DMatch> >& matches, maxDistance, const vector<Mat>& masks=vector<Mat>(), bool compactResult=false );
3. matching结果包含许多错误匹配,错误的匹配分为两种:
- False-positive matches: 将非对应特征点检测为匹配(我们可以对他做文章,尽量消除它)
- False-negative matches: 未将匹配的特征点检测出来(无法处理,因为matching算法拒绝)
- Cross-match filter:
- Ratio test
- void PatternDetector::getMatches(const cv::Mat& queryDescriptors, std::vector<cv::DMatch>& matches)
- {
- matches.clear();
- if (enableRatioTest)
- {
- // To avoid NaNs when best match has
- // zero distance we will use inverse ratio.
- const float minRatio = 1.f / 1.5f;
- // KNN match will return 2 nearest
- // matches for each query descriptor
- m_matcher->knnMatch(queryDescriptors, m_knnMatches, 2);
- for (size_t i=0; i<m_knnMatches.size(); i++)
- {
- const cv::DMatch& bestMatch = m_knnMatches[i][0];
- const cv::DMatch& betterMatch = m_knnMatches[i][1];
- float distanceRatio = bestMatch.distance /
- betterMatch.distance;
- // Pass only matches where distance ratio between
- // nearest matches is greater than 1.5
- // (distinct criteria)
- if (distanceRatio < minRatio)
- {
- matches.push_back(bestMatch);
- }
- }
- }
- else
- {
- // Perform regular match
- m_matcher->match(queryDescriptors, matches);
- }
- }
void PatternDetector::getMatches(const cv::Mat& queryDescriptors, std::vector<cv::DMatch>& matches){ matches.clear(); if (enableRatioTest) { // To avoid NaNs when best match has // zero distance we will use inverse ratio. const float minRatio = 1.f / 1.5f; // KNN match will return 2 nearest // matches for each query descriptor m_matcher->knnMatch(queryDescriptors, m_knnMatches, 2); for (size_t i=0; i<m_knnMatches.size(); i++) { const cv::DMatch& bestMatch = m_knnMatches[i][0]; const cv::DMatch& betterMatch = m_knnMatches[i][1]; float distanceRatio = bestMatch.distance / betterMatch.distance; // Pass only matches where distance ratio between // nearest matches is greater than 1.5 // (distinct criteria) if (distanceRatio < minRatio) { matches.push_back(bestMatch); } } } else { // Perform regular match m_matcher->match(queryDescriptors, matches); }}
为了进一步提升匹配精度,可以采用随机样本一致性(RANSAC)方法。
- bool PatternDetector::refineMatchesWithHomography
- (
- const std::vector<cv::KeyPoint>& queryKeypoints,
- const std::vector<cv::KeyPoint>& trainKeypoints,
- float reprojectionThreshold,
- std::vector<cv::DMatch>& matches,
- cv::Mat& homography
- )
- {
- const int minNumberMatchesAllowed = 8;
- if (matches.size() < minNumberMatchesAllowed)
- return false;
- // Prepare data for cv::findHomography
- std::vector<cv::Point2f> srcPoints(matches.size());
- std::vector<cv::Point2f> dstPoints(matches.size());
- for (size_t i = 0; i < matches.size(); i++)
- {
- srcPoints[i] = trainKeypoints[matches[i].trainIdx].pt;
- dstPoints[i] = queryKeypoints[matches[i].queryIdx].pt;
- }
- // Find homography matrix and get inliers mask
- std::vector<unsigned char> inliersMask(srcPoints.size());
- homography = cv::findHomography(srcPoints,
- dstPoints,
- CV_FM_RANSAC,
- reprojectionThreshold,
- inliersMask);
- std::vector<cv::DMatch> inliers;
- for (size_t i=0; i<inliersMask.size(); i++)
- {
- if (inliersMask[i])
- inliers.push_back(matches[i]);
- }
- matches.swap(inliers);
- return matches.size() > minNumberMatchesAllowed;
- }
bool PatternDetector::refineMatchesWithHomography(const std::vector<cv::KeyPoint>& queryKeypoints,const std::vector<cv::KeyPoint>& trainKeypoints, float reprojectionThreshold,std::vector<cv::DMatch>& matches,cv::Mat& homography){const int minNumberMatchesAllowed = 8;if (matches.size() < minNumberMatchesAllowed)return false;// Prepare data for cv::findHomographystd::vector<cv::Point2f> srcPoints(matches.size());std::vector<cv::Point2f> dstPoints(matches.size());for (size_t i = 0; i < matches.size(); i++){srcPoints[i] = trainKeypoints[matches[i].trainIdx].pt;dstPoints[i] = queryKeypoints[matches[i].queryIdx].pt;}// Find homography matrix and get inliers maskstd::vector<unsigned char> inliersMask(srcPoints.size());homography = cv::findHomography(srcPoints, dstPoints, CV_FM_RANSAC, reprojectionThreshold, inliersMask);std::vector<cv::DMatch> inliers;for (size_t i=0; i<inliersMask.size(); i++){if (inliersMask[i])inliers.push_back(matches[i]);}matches.swap(inliers);return matches.size() > minNumberMatchesAllowed;}
经过单应性变换的过滤结果
- 学习OpenCV——KeyPoint Matching 优化方式
- 学习OpenCV——KeyPoint Matching 优化方式
- 【Compute Vision】学习OpenCV——KeyPoint Matching 优化方式
- 学习OpenCV——KeyPoint Matching 优化方式
- opencv Keypoint
- opencv的KeyPoint
- OpenCV中KeyPoint类
- opencv中的KeyPoint和DMatch
- opencv中keypoint数据结构分析
- opencv中keypoint数据结构分析
- opencv中keypoint数据结构分析
- 【杂谈opencv】OpenCV中的KeyPoint与DMatch
- Keypoint
- opencv如何获得keypoint的坐标
- BRISK:Binary Invariant Scalable Keypoint——配置与运行
- SIFT算法:KeyPoint找寻、定位与优化
- 树莓派学习笔记—— 源代码方式安装opencv
- 树莓派学习笔记——apt方式安装opencv
- js 内存管理
- Extjs3.2第一篇:formPanel的getForm().getValues()提交与处理
- 用java做导出excel的万能方法
- spark常用的transformation
- Java安全之消息摘要
- 学习OpenCV——KeyPoint Matching 优化方式
- 棋盘覆盖问题
- Servlet API中包装类装饰模式的应用
- rediskey的辅助类备忘
- LeetCode 257. Binary Tree Paths
- solvepnp详解
- Python实现删除当前目录下除当前脚本以外的文件和文件夹实例
- java中“或”(||)条件的执行次序
- com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure