今天读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::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;
}
经过单应性变换的过滤结果

本文介绍了OpenCV中特征点匹配的优化方法,包括Brute-force和Flann-based两种匹配方式,以及如何通过Cross-match filter和Ratiotest减少False-positive matches。此外还探讨了使用RANSAC方法进行Homography estimation以提高匹配精度。

675

被折叠的 条评论
为什么被折叠?



